Added environment variable and file handling to bash script example.

This commit is contained in:
Ed Braaten 2015-04-23 22:19:24 -07:00
parent 7636600533
commit 2d6766f0bb

View file

@ -5,11 +5,9 @@
declare -a animals=("camel" "llama" "owl")
declare -A FRUIT_COLOR
FRUIT_COLOR=( ["apple"]="red" ["banana"]="yellow")
VERINFO=`git describe --abbrev=7 --dirty --always --tags`
# Obligatory Hello World with some version info
echo "Hello Bash World! Version: $VERINFO"
@ -39,3 +37,45 @@ for fruit in "${!FRUIT_COLOR[@]}"; do
echo "$fruit = ${FRUIT_COLOR["$fruit"]}";
done | sort --reverse
# Read a non-existent environment variable and supply default value...
SOME_ENV_VAR=${PROBABLY_NOT_THERE-Default Value}
echo ""
echo "PROBABLY_NOT_THERE is set to '$SOME_ENV_VAR'"
echo ""
# Let's read in the PATH variable from the OS and print it out...
PATH_ENV_VAR=$PATH
echo "OS PATH var is set to: '$PATH'"
echo ""
# Let's manipulate PATH variable and then restore it
echo "Adding :/test to PATH variable"
PATH=$PATH:/test
echo "OS PATH var is set to: '$PATH'"
echo ""
echo "Restoring original PATH value."
PATH=$PATH_ENV_VAR
echo "OS PATH var is set to: '$PATH'"
echo ""
# Let's create the file test.out and write animals array to it...
echo ""
echo "Writing test.out file..."
rm -f test.out
for i in ${animals[@]}
do
echo $i >> test.out
done
# Now open the file we wrote and read it back...
echo ""
echo "Contents of test.out file:"
LINECOUNT=1
while read line
do
echo "line $LINECOUNT: $line"
LINECOUNT=$[$LINECOUNT +1]
done <test.out
# All done, finish with normal exit code.
exit 0