bash Data Administrative Shell Scripting COMP2101 Fall 2017
Variables Every process has memory-based storage to hold named data A named data item is referred to as a variable (sometimes called a parameter), all of them together create a simple table with names and values Variables can hold text or binary data as their value Variables are typically created by assigning data to them, using an assignment operator such as = myvar=3 variable2="string data" vowels="aeiou"
Variables Variable content is accessed using the $ symbol prefixed to the variable name Non-trivial variable names must be surrounded by { } echo $myvar echo ${myvar} echo ${myarray[32]} echo ${frank-n["beans"]}
Environment Variables By default variables are created in local process memory, not in the process environment Environment variables are inherited by child processes Environment variables are normally named using capital letters and numbers only Variables can be exported to the environment or removed from the environment VARNAME= Some data for this variable export VARNAME export -n VARNAME export VARNAME= Data
Using Variables Variables have many uses Command line substitution is a very common use, using a variable to provide data used on a command line Variables are deleted using the unset command echo $SHELL ls $HOME for file in $FILES; do if [ $USER!= $LOGNAME ] mypid=$$ unset VAR
Getting Data For Variables A very common way to get dynamic data into a variable is to use the `` or $() syntax to obtain the output of a command Interactive input can be saved in a variable using the read command today=`date +%A` today=$(date +%A) echo $today read myvar read -p "Give me a number " mynum echo "You gave me $mynum"
Exercises Create an environment variable on your command line named MYNAME and assign your name to it as data Create a local variable on your command line named mytitle and assign a title you like to it as data, such as Mister, Miss, Doctor, President, etc. Use the echo command to print out your name preceded by your title, using your variables to supply the data Create a script with the echo command from the previous step and run it Modify your script to print out: Welcome to planet hostname, title name! Today is weekday.
Shell Data - Numbers Data is often found inline in scripts Inline data can be simple numbers or strings of characters Numbers are simply entered as digits and can be signed, but must be integers bash can do basic arithmetic +, -, *, /, % on integers by putting arithmetic statements inside $(( )) Leaving off the $ allows you to test if the result is zero echo 32 echo $(( 3 + 4 )) echo "5 divided by 2 leaves a remainder of $(( 5 % 2 ))" (( $loot % $raiders )) echo "Uh-oh, doesn't divide evenly" echo "$(( $RANDOM % 6 + 1 )) is a random roll of a die"
Working with Numbers The $((expression)) syntax assumes normal order of operations (i.e. first %*/, then +-, all from left to right) Parentheses can be used inside the arithmetic expression to change the order awk can do floating point (e.g. awk 'BEGIN {printf "%.2f\n", 7/3}') So can bc (e.g. bc <<< "6.7*1.2") read -p "Give me 2 numbers: " firstnum secondnum echo "$firstnum plus $secondnum is $((firstnum + secondnum))" echo "$firstnum minus $secondnum is $((firstnum - secondnum))" echo "$firstnum times $secondnum is $((firstnum * secondnum))" echo "$firstnum divided by $secondnum is $((firstnum / secondnum))" echo It is also $(awk "BEGIN{printf \"%.2f\", $firstnum/$secondnum}") echo "The remainder when dividing $firstnum by $secondnum is $((firstnum % secondnum))" echo "$firstnum to the power of $secondnum is $(bc <<< "$firstnum ^ $secondnum")"
Shell Data - Strings Strings are normally entered as single words, or surrounded by quotes for strings containing special characters such as spaces Single quotes turn off all special characters Double quotes turn off most special characters, $ is still special inside "" Special characters can also be preceded with \ to turn off their special meaning cd touch My File ls touch "My File" ls touch My\ Other\ File ls touch 'Terrible"Name"' ls touch -- '-worse\ name' ls
Working With Strings Several cli tools are available to parse and manipulate strings ${#varname} will give the length of the string expr index string searchtext will return a non-zero index value if searchtext is in string [fe]grep, tr, cut, sed, [ng]awk - ref. course resources See http://tldp.org/ldp/abs/html/string-manipulation.html for more pure bash string manipulation techniques and information
bash Prompts Prompts are displayed to help users determine what is going on and what is expected of them bash stores prompt formats in 4 variables and displays them when appropriate PS1 has the prompt to let a user know that bash is expecting a command ($ vs. # convention indicates privilege level) PS2 has the prompt to let a user know more input is expected for the current command line PS3 is used for the select command PS4 is used to prefix an output line during execution tracing See PROMPTING section of bash man page for more info
Exercises Enter an echo command using quotes which start on one line and end on another Enter a pipeline command that starts on one line and ends on another Enter a command with backslash at the end of the line Use a HERE document to create a file named food with four lines of text, each containing a food name
Predefined Variables bash provides a number of variables containing useful information, the env command will display them Some are inherited from ancestor processes (e.g. login, getty, init) More are often set up by user environment files to customize the user experience bash also has some special variables that dynamically retrieve data for realtime uses
Special Variables $ - current process id # - number of parameters on the command line of a script 0-n - command line parameters RANDOM - a random 15-bit integer REPLY - default variable name for the read command? - exit status of most recent command
Complex Variables Arrays can be created using ( ) They allow us to group data items and still operate on them as individual items Elements of an array are uniquely identified by an index integer starting at 0 Elements of an array can be accessed using the varname[index] syntax Negative indices count backwards from the end of the array @ and * can be used as indices to include all array elements # can be used to obtain a count of elements in an array arrayvar=(a b c) echo ${arrayvar[0]} ${arrayvar[-1]} echo ${arrayvar[@]} ${#arrayvar[@]} INDEX DATA 0 a 1 b 2 c
Associative Arrays Associative arrays (sometimes called hashes) can use any string as an index They must be declared before being used They are useful for storing structured data declare A foo foo=([key1]= data1 [key2]= data2 ) echo ${foo[key1]} echo ${foo[@]} echo ${!foo[@]} INDEX key1 key2 DATA data1 data2
Exercises Create an array named colours and give it the following data values: red, green, blue Create a hash named animals and give it the following data elements: key red - value cardinal, key green - value frog, key blue - value lobster Use echo to display all the elements of the colours array, both by using the index values, and by using @ as an index Do the same for the same for the animals array Create a variable named num and give it a value of 0 Use echo to display the data from the colours array corresponding to the value of $num followed by the value from the animals hash which uses the colour from the colours array as a key to find an animal name e.g. red cardinal, green frog, blue lobster Create a script to define the array and hash as above, and ask the user for a number for the num variable to select a combination to print out Test your script to show you can make it print any valid combination from your array
Command Line Arguments Any command can have options and arguments, although some commands do not The command line is accessible in a program or script, using the special variables $0, $1, $2, etc. $0 holds the command itself (there are exceptions to this) $1, $2, $3, etc. hold each of the remaining words of the command line On a bash command line, words are space-separated sequences of characters Quoting and the escape character can be used to turn off the word separation of any particular space on a command line