Shell Scripting Crash course Shell scripting is fun for the whole family. And even mildly useful in day to day linuxing. * Basic Commandline stuff (stolen from bash manpage) line editing: ctrl-a: jump to begining of line ctrl-e: jump to end of line ctrl-k: delete to end of line ctrl-t: transpose two chars history: ctrl-p: previous command in history, sorta equilvent to up-arrow ctrl-n: next command in history ctrl-r: reverse search history very useful... allows you to search thorugh your command history for something meta-.: insert the last word from the previous command at the cursort meta-ctrl-y: insert the first arg of the last command at cursor !!: subsitute/expand the previous command here !string: use the command in histyr starting with string completion: tab: complete command or filename meta-@: try to complete hostname (only uses /etc/hosts) * the always useful "for" loop Perhaps the most useful in day to day use. basic syntax: for some_varabile in `something that outputs stuff` do whatever_you_want_to_do $some_variable done For each item in the list that `somehing that outputs stuff`, whatever_you_want_to_do gets called on it. Note: all bash syntax can be used on the command line or in a script. * variable use, substituion, etc Basically, any string can be a variable. Their are no datatypes. dir_listing=`ls -la` echo $dir_listing echo dir_listing when setting variables, their is no "$", when using them to get their value, you use the "$" Variables can be used within strings and as input to other commands: md5sum $dir_listing echo "This is the dir listing: $dir_listing" for i in $dir_listing do file $i done Variables can also be combined together. foo="test" bar="fail" echo $foo$bar echo $foo/$bar To print any "keyword" or reserved char, just escape it with "\" echo \$foo/$bar echo \\ echo $PATH echo $PATH Variabls can also be used as commands: SLOTH="ls -lart /" $SLOTH General guidelines are too make all shell variables upper case. Just helps in keeping everything straight. * quoting rules Eeek. shell quoting rules are a bit obtuse at times. Basically. Everything inside of "" quotes is interpreted directly except for shell variables ( stuff with $foo) and stuff in back ticks echo this is a test echo "this is a test" echo "$foo this is a test" echo "\$foo this is a test at `date`" Dont try to use `` backticks as quote characters. it doesnt work. '' apostphres can also be used as quote charachters This can get pretty hairy very quickly. * use of external commands The point of shell scripting is to use to tye together external unix commands in a useful fashion. So feel free to abuse this at every given oporunity. * differences between internal "builtins" and external commands Bash in particular, has builtin equilivents for some of the more common commands used in shell scripting. This can be useful as it provides some degree of platform indepence, but it can also be very confusing. Some common ones include: cd echo read set exec alias To see a whole list, type: enable The thing to keep in mind is that when typing some command in a bash shell, or a script. It may not be the command you think it is. It could be another version elsewhere in the path (/bin/vi vs /usr/bin/vi for example), a builtin (echo vs /bin/echo), an alias (/bin/rm va the rm alias), or a bash function. * various useful "test" uses Program flow is dependend on test condictions. The bash built "test" is used to do this, though its typicall used in its "[","]" syntax if [ -f /etc/passwd ] ; then echo "yes, passwd exists" fi [ -f /etc/passwd ] evaluates to TRUE if /etc/passwd exists. There are various version of this for differenct circumstances. You can test to see if a file is a dir, a symlink, nonzero, is readable, is executable, etc. There are also some string and variable based tests. if [ -z $foo ] ; then echo "$foo is set, and exists" fi if [ $foo = $foo2 ] ; then echo "$foo and $foo2 are equal" fi * program flow NOTE: spacing is important if statement: if [ some_test_condition_here ] ; then do_something elif [ some_other_test_condition ] ; then do_something_else else do_the_last_thine fi case statement: case $some_value in foo) # if $some_value is foo then do_something_here break; ;; bar) do_something_here_bar break; ;; esac while statement: while $foo do do_somthing_here done * using find find is probabaly one of the most useful shell commands. Though lots of it dont neccesarily need any shell scripting, this is a good place to cover it. Find all files and dirson the system: find / -print just dirs: find / -type d all files owned by the cuurent user in questin find / -user `whoami` md5sum all files owned by wil find / -type f -user wil -exec mds5sum {} \; * commandline arguments $0 : the name the file was started as $1, $2,etc: first arg, second arg, etc * Math math in bash sucks. FOO=2 BAR=3 BLEAH=1234 echo $[FOO+BAR] echo $[FOO*BAR] echo $[BLEAH/BAR] echo $((FOO+BLEAH)) echo $(($[FOO*BLEAH]/100)) * examples and useful idioms renaming a *.file to some*.file Say you have a directory full of some files, say oh, i dunno, jpegs. You want to rename all .jpeg to .jpg for i in `ls *.jpeg` do mv $i `basename $i .jpeg`.jpg done basename is a standard text util used to strip filename extensions off of stuff. throming around backticks --using dialog for a "gui" see "songchoose" quick way to make a TUI/GUI interface for an app. replacing dialog/whiptail with gdialog lets you add a quick gtk frontend to a shell script --using "seq" for i in `seq 1 10` do touch image_$i.jpeg done * other examples There are lots of shell scripts in a standard install. For example: cd /usr/bin file * | grep Bourne Good sources to check include the init scripts, and/or configure scripts and friends. Something like the gimp configure script probabaly has just about every weird shell thing in it you can imagine.