Chapter 14 -------------------------------------- #!/bin/bash # func # A simple function repeat() { echo -n “I don’t know $1 $2 “ } repeat Your Name -------------------------------------- #!/bin/bash # nested # Calling one function from another number_one () { echo “This is the first function speaking...” number_two } number_two () { echo “This is now the second function speaking...” } number_one -------------------------------------- #!/bin/bash # scope # dealing with local and global variables scope () { local lclVariable=1 gblVariable=2 echo “lclVariable in function = $lclVariable” echo “gblVariable in function = $gblVariable” } scope # We now test the two variables outside the function block to see what happens echo “lclVariable outside function = $lclVariable” echo “gblVariable outside function = $gblVariable” exit 0 -------------------------------------- scope () { local lclVariable=1 gblVariable=2 echo “lclVariable in function = $lclVariable” echo “gblVariable in function = $gblVariable” } another_scope_function() { echo “This is another_scope_function...” } yet_another_function() { echo “This is yet_another_function...” } -------------------------------------- #!/bin/bash # get # A script for demonstrating getopts while getopts “xy:z:” name do echo “$name” $OPTIND $OPTARG done -------------------------------------- if [ -r writeFile && -x writeFile ] then echo “writeFile exists, is readable, and executable!” fi and if [ -r writeFile || -w writeFile ] then echo “writeFile exists and is readable or writable!” fi -------------------------------------- #!/bin/bash # sigtrap # A small script to demonstrate signal trapping tmpFile=/tmp/sigtrap$$ cat > $tmpFile function removeTemp() { if [ -f “$tmpFile” ] then echo “Sorting out the temp file... “ rm -f “$tmpFile” fi } trap removeTemp 1 2 exit 0 -------------------------------------- #!/bin/bash # array # A script for populating an array from a file populated=( `cat ~/tmp/sports.txt | tr ‘\n’ ‘ ‘`) echo ${populated[@]} exit 0 -------------------------------------- #!/bin/bash -r # This script won’t work cd /some/other/directory set +r exit 0 #!/bin/bash # This script won’t work cd /some/other/directory echo “`pwd`” set -r cd echo “`pwd`” set +r exit 0 -------------------------------------- #!/bin/bash # echobug1 # Faulty echo shell set -x ech “Guess the secret color” set +x