Bash Data Handling: COMP2101 Winter 2019
Bash Data Handling: COMP2101 Winter 2019
Bash Data Handling: COMP2101 Winter 2019
COMP2101
Winter 2019
Conditional Execution and Testing
Command Lists
Exit Status
Test Command
Command Lists
A command list is a list of one or more commands on a single command line in
bash
Operators also tell bash the rules for running the command sequence
The ; operator between commands tells bash to run the first command and
when it finishes to run the next one without regard for the success or failure of
the first command
echo -n "Number of non-hidden files in this directory: " ; ls | wc -l
echo -n "Process count by username:" ; ps -eo user --no-headers | sort | uniq -c
echo -n "eno1 interface address:" ; ip a s eno1 | grep -w inet | awk '{print $2}'
Bash Conditional Execution
We can use that exit status to tell bash whether or not to run a second command
The exit status of a pipeline is the exit status of the last command that ran in the
pipeline
You can force a script to exit immediately by using the exit command
To set an unsuccessful exit, put a non-zero number on the exit command line e.g.
exit 1
Any time a command might fail and cause problems, your script should be doing
something to recognize and deal with the possible failure
Conditional Command List
Putting the && operator between two commands on one line creates a command list
that only runs the second command if the first command succeeds
Putting the || operator between two commands on one line creates a command list that
only runs the second command if the first command fails
You can build multiple conditional executions on a single command line, but you may
need parentheses to control the order in which they get evaluated
To use both && and || on a command line, put the && first but consider using an if
command instead
cd /flooble || exit 1
grep -q dennis /etc/passwd && echo "User dennis is in the passwd file"
ps -eo user | grep -q dennis || echo "User dennis has no processes running"
sudo find / -user dennis -ls || echo "User dennis owns no files on this system"
Test Command
When you start to use conditional execution and pipelines and more operators, your
command lines can get quite long
You can make these easier to read and debug if you put each command on its own line
Most operators at the end of the line tell bash to continue this command on the next
line, semicolon is a notable exception
When using continuation lines like this, it is good practice to indent the continuation
lines to make it clear to the reader that they are continuation lines
Variables
Dynamic data
User input
Bash DATA Handling
Bash handles special things like quotes, character escapes, substitutions, and
arithmetic
Bash then splits a command line into words (aka tokens), with the first word
being the command to run and the rest being things for that command to work
on
If one or more of these are found in words on the command line, bash
may try to turn those words into filenames - this is called globbing
ls *
echo *
ls .?
Shell Data - Numbers
Bash can use text having only digits and a + or - sign as signed integers, if
you tell it to do that - test it before using it for very large numbers
The (()) syntax turns off file name globbing for * inside the (())
echo 3 + 4
echo $(( 3 + 4 ))
echo $(( 3.6 * 1.7 ))
echo 7 divided by 2 is $(( 7 / 2 )) with a remainder of $(( 7 % 2 ))
echo Rolling dice ... $(( $RANDOM % 6 + 1 )), $(( RANDOM % 6 + 1))
Lab 2 - diceroller.sh
Variables
Every process has memory-based storage to hold named data
Variables can hold any data as their value, but usually hold text data, aka strings
Putting # at the start of a variable name tells bash you want the count of
characters in the variable, not the text from the variable
Sometimes you want to run a command using command line data that may not
be a fixed value - this is called using dynamic data
Getting data from a variable to put on the command line is a way to put
dynamic data on the command line
Bash can run a sub-shell to execute one or more commands and put the output
from the sub-shell onto the command line of another command
today=$(date +%A)
todaymessage="Today is $(date +%A)."
echo "Today is $today."
Lab 2 - welcome-example.sh
User Input
The read command will wait for the user to type out a line of
text and press enter, then put whatever was typed into a
predefined variable named REPLY
Many command line tools are available to parse and manipulate strings
expr index string searchtext will return a non-zero index value if searchtext is in string