# # > stdout # 2> stderr # &> both stdout & stderr # # # /usr/share/doc/bash-2.05b/startup-files # # # Search order # ------------ # ~/.bash_profile # ~/.bash_login is NOT read if .bash_profile exists ?? # ~/.profile is NOT read if .bash_login exists ?? # # # http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_01.html # # (generic) /etc/profile is read when: # bash --login # sh foo.sh # csh foo.csh # ksh foo.ksh # # ~/.bashrc # if [ -f /etc/bashrc ]; then # . /etc/bashrc # fi # # set -o noclobber # # # ~/.bash_profile # source ~/.bashrc # source ~/.bash_login # # ~/.bash_login # umask 002 # # ~/.login # # # ~/.bash_logout # ~/.logout # # http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_01.html # variables are NOT inherited by child processes # unless they are exported by the parent shell # # sourcing a file applies changes and setting variables in the current shell # # # Variables are case sensitive # ---------------------------- # # env or printenv # ----------------- # Global variables or environment variables are available in all shells # ( and must be exported ?? ) # # # "set LocalVar" to define local variables # ---------------------------------------- # Local variables are only available in the current shell # # # to see what is set on or off # "set -o" # # # Compare Global variables vs local variables # -------------------------------------------- # set | sort > set.sorted # printenv | sort > printenv.sorted # diff set.sorted printenv.sorted | grep "<" | awk '{ print $2 }' # # # Exported Variables # ------------------ # http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html # # A subshell can change variables it inherited from the parent, # but the changes made by the child don't affect the parent ( even if it's exported ?? ) # # # IFS is "''", the default used as delimiters # http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html # # # date +"%y%m%d_%H%M%S" 031128_214136 # # Advanced bash scripting http://www.tldp.org/LDP/abs/html # # http://www.uwsg.iu.edu/UAU/shell/script/test.html http://www.uwsg.iu.edu/UAU/shell/script/trap.html http://www.ussg.iu.edu/UAU/shell/script/control.html # http://www.bagley.org/~doug/shootout/lang/bash/allsrc.shtml # http://www.linux.se/doc/HOWTO/Adv-Bash-Scr-HOWTO/external.html # http://www.introcomp.co.uk/scripts # # # # Check if $DEST responds # Alive=`ping -s 1 -c 1 $DEST > /dev/null; echo $?` # if [ $Alive -eq 0 ]; then echo "$DEST is alive" else echo "$DEST is offline" fi # # ------------------------------------------------------ # # test -d /etc/init.d/SAVE || mkdir /etc/init.d/SAVE # # if [ ! -d foo ]; # then # mkdir foo # elif [ soemthing else ]; # then # do that # fi # function Ack () { if [ $1 -eq 0 ]; then Ack=$[ $2 + 1 ] elif [ $2 -eq 0 ]; then Ack $[ $1 - 1 ] 1 else Ack $1 $[ $2 - 1 ] local SubAck=$Ack Ack $[ $1 - 1 ] $SubAck fi } # # n=${1:-1} Ack 3 $n echo "Ack(3,$n): $Ack" # # # function fib { if [ $1 -lt 2 ] ; then echo 1 else echo $[ `fib $[ $1 - 2 ]` + `fib $[ $1 - 1 ]` ] fi } N=${1:-1} fib $N # # # # #!/bin/sh # # in case things dont expand # shopt -s nullglob if [ -n "$(echo /tmp/*.{jpg,JPG,jpeg,JPEG})" ]; then echo "Files exist" else echo "Files do not exist" fi # # foo=1 while [ $foo -lt 3 ]; do echo "foo is $foo" foo=$(( foo + 1)) done echo "final foo is $foo" results in this output foo is 1 foo is 2 final foo is 3 # # # #!/bin/bash set -x # echo all commands executed set -v # for i in 1 2 3 4 5; do echo foobar${i} ; done # # # ls | while read file do ls -la done # # # RSYNC_COMMAND="rsync /html/ webmaster@second_server:/html/ -e ssh -r --delete --times --modify-window=1 --exclude /serverid.html" $RSYNC_COMMAND -n -v # while echo -n "Proceed with sync ?(y/n)" read response do case "$response" in 'n') echo "Syncronization cancelled" break ;; "y") $RSYNC_COMMAND -v echo "Syncronization completed" break ;; esac done # # # end of file