topical media & game development
basic-unix-14.txt / txt
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 = gblVariable”
}
scope
# We now test the two variables outside the function block to see what happens echo “lclVariable outside function = gblVariable”
exit 0
--------------------------------------
scope ()
{
local lclVariable=1
gblVariable=2
echo “lclVariable 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 “OPTIND tmpFile function removeTemp() {
if [ -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
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.