Shell Tools

This is a collection of Bash helper functions.

Download

To use this script, add the following to your ${HOME}/.bashrc

export ST_SH=/path/to/shelltools.sh && . ${ST_SH}

This script has its own built-in help system, below is its output:

[berto@bolt][783]$ st_help
Here is a list of all the available development functions.
If you would only like to see a specific function, pass its 
name as an argument, e.g. st_help st_help.

 st_appdir_set 
 inspect the given root directory and prepend paths of typical services
 to shell's environment, e.g. PATH, MANPATH, PYTHONPATH, etc.

 st_appdir_unset 
 inspect the given root directory and prepend paths of typical services
 to shell's environment, e.g. PATH, MANPATH, PYTHONPATH, etc.

 st_common  
 echo the common component of the given strings

 st_delimit   
 take the given string and replace the current delimiter with the new one

 st_echo_up 
 echo out the directory representing the number of directories to go up

 st_envset 
 go through the list of variables to set and export the values

 st_envunset 
 go through the list of variables and unset the given paths

 st_error 
 Write the given message to stderr

 st_is_sourced
 Simply returns 0 to report shelltools has been sourced

 st_in_list  [list]
 Check if the given item already exists in the list. If there is
 something in the list, a 0 will be returned, otherwise 1 (shell
 return status convention).

 st_help [function] [filename] [cmd]
 Print out the help documentation for the given function to the screen.
 If no function was given, all functions are printed
 The filename is the path to the script to extract help from. The
 default is this shell script (i.e. $ST_SH). The command is what
 is displayed in the help output, default is st_help

 st_linefy 
 Takes a space delimited list and makes a carriage-return delimited list

 For example, you can use this in conjunction with st_delimit to perform an
 action on every directory in $PATH:

 for i in $(st_linefy $(st_delimit : " " $PATH)); do echo "dir: $i"; done;

 Input to this function can also be piped in, e.g.: echo "a b c" | st_linefy

 st_path 
 Clean out duplicate entries in the given colon-delimeted path and
 echo the result.

 st_path_remove 
 Remove out the given path from the colon-delimited list.

 st_path_pop  
 If the given path is found in the colon-delimited list, remove it

 st_pop  [list]
 Check to see if the first argument of the function exists in the
 remaining arguments. If it does, remove it from the list and return
 the modified list. Otherwise, return the list unchanged. If
 something is found, this function will return 0, otherwise it will
 return 1.

 st_promote 
 Take the given executable and promote it to the top of the path.
 This does not move the executable's entire directory to the top of the path,
 but rather just moves that particular executable. If you use this, in order
 to keep the promote directory tidy, add st_promote_cleanup to a logout
 script, like ${HOME}/.bash_logout.

 clean up the temporary promote directories if they exist

 st_pup 
 pushd up the given number of directories

 st_pyswitch 
 Go through the PYTHONPATH environment variable and switch out all given
 python version strings with the given version. Then look for the path of
 the given python version and set it to the path specified in
 $ST_PYTHON_LINK. Some paths in PYTHONPATH may no longer exist with the new
 path, but this is intentional in order to preserve all the paths in the
 right order in case st_pyswitch is used to switch back to the original
 version.

 st_repeat [-d ] [-i] [-c ] [-k] 
 Repeat the given command.

 By default, this command will exit upon success, unless -i or -c are given.

 -d (default 2) specifies how long to wait between iterations
 -i will repeat the command indefinitely
 -c will repeat the command the specified number of times
 -k will clear the screen before each iteration

 st_rm 
 If the given file is found remove it, otherwise exit quietly

 st_run  [args... ]
 Run the given command and print out an error message if the command does not
 complete successfully

 st_run_or_exit  [args... ]
 Run the given command and print out an error message if the command does not
 complete successfully

 st_source 
 If the given file[s] exist[s] source. Return 0 if all the given files were
 sourced successfully, non-zero othewise.

 st_status 
 Run the command and echo the status.

 This is a shortcut around the idiom that goes something like this:

 ps -ef | grep -q foo
 status=$?

 Instead you end up with:

 status=$(st_status ps -ef | grep -q foo)

 st_up 
 cd up the given number of directories