From 2d6766f0bb4f86da98f9dd1332b5301318ec6444 Mon Sep 17 00:00:00 2001 From: Ed Braaten Date: Thu, 23 Apr 2015 22:19:24 -0700 Subject: [PATCH] Added environment variable and file handling to bash script example. --- bash/hello.sh | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/bash/hello.sh b/bash/hello.sh index 032468d..dae53c9 100755 --- a/bash/hello.sh +++ b/bash/hello.sh @@ -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