Check These Out
Each shell function has its own summary line, as a comment. If there are multiple shell functions with the same name, the function with the highest number of votes is put into the file.
Note: added 'grep -v' to the end of the pipeline, to eliminate extraneous lines containing only '--'. Thanks to matthewbauer for pointing this out.
Sometimes in a script you want to make sure that a directory is in the path, and add it in if it's not already there. In this example, $dir contains the new directory you want to add to the path if it's not already present.
There are multiple ways to do this, but this one is a nice clean shell-internal approach. I based it on http://stackoverflow.com/a/1397020.
You can also do it using tr to separate the path into lines and grep -x to look for exact matches, like this:
$ if ! $(echo "$PATH" | tr ":" "\n" | grep -qx "$dir") ; then PATH=$PATH:$dir ; fi
which I got from http://stackoverflow.com/a/5048977.
Or replace the "echo | tr" part with a shell parameter expansion, like
$ if ! $(echo "${PATH//:/$'\n'}" | grep -qx "$dir") ; then PATH=$PATH:$dir ; fi
which I got from http://www.commandlinefu.com/commands/view/3209/.
There are also other more regex-y ways to do it, but I find the ones listed here easiest to follow.
Note some of this is specific to the bash shell.
watch is a command especially designed for doing this job
host B (you) redirects a modem port (62220) to his local ssh.
host A is a remote machine (the ones that issues the ssh cmd).
once connected port 5497 is in listening mode on host B.
host B just do a
ssh 127.0.0.1 -p 5497 -l user
and reaches the remote host'ssh. This can be used also for vnc and so on.
the block of the loop is useful whenever you have huge junks of similar jobs, e.g., convert high res images to thumbnails, and make usage out of all the SMP power on your compute box without flooding the system.
note: c is used as counter and the random sleep
$ r=`echo $RANDOM%5 |bc`; echo "sleep $r"; sleep $r
is just used as a dummy command.
It is a pain grep-ing/sed-ing/awk-ing plain old df. POSIX it!
Even adds a newline.
Or "tail -r" on Solaris.
Useful when checking MySQL status.