25
bash Data Handling COMP2101 Winter 2019

bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

bash Data HandlingCOMP2101Winter 2019

Page 2: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Conditional Execution and Testing

Command Lists

Conditional Execution Operators

Exit Status

Test Command

Page 3: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Command Lists

A command list is a list of one or more commands on a single command line in bash

Putting more than one command on a line requires placement of an operator between the commands so that bash can tell them apart

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}'

Page 4: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Bash Conditional Execution

When a command is run, it may fail and cause other commands to have problems or become unnecessary

A command may need to be run only under specific circumstances

A command may depend on another command finishing properly before it can be run

In order to automate these things, we need to be able to control the execution of commands in our scripts

Page 5: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Exit Status

Every command we run produces an exit status when it ends

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

Page 6: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

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"

Page 7: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Test Command

The test command evaluates an expression and sets its exit status based on whether the expression evaluates as true or false

We can use the exit status of the test command to control whether other commands run, in effect running commands based on the result of the test

test -d ~ && echo "I have a home directory" test -f myfile || echo "myfile is missing" test -d ~/Downloads || (mkdir ~/Downloads && echo "Made Downloads dir")

Page 8: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Lab 1 - getpics.sh Script

Page 9: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Long command lines

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

mkdir foo && echo "Made foo" || echo "Failed to make foo"

echo -n "eno1 interface address:" ip a s eno1 | grep -w inet | awk '{print $2}'

Page 10: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Working With Data

Data on the command line

Variables

Dynamic data

User input

Page 11: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Bash DATA Handling

Bash generally treats command line data as text, in keeping with UNIX interoperability philosophy

Bash can use any system utility to produce data

Data produced is usually displayed and discarded by the shell

Bash provides variables as a mechanism to temporarily store simple data

Page 12: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Command Line Data

Bash command lines are evaluated by bash before being executed as commands

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

echo "It's fun!" echo A megabyte is $(( 1024 * 1024 )) bytes echo Today is `date +%A`

You can use the echo command to display evaluated text

Page 13: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Shell Data - Text

Commands can look for text on the command line to use, each word on the line is normally treated as a separate thing to work on

You can tell bash to ignore special characters when you need to

You might want one or more spaces as part of a filename, or to use other special characters as plain text, such as using an apostrophe as part of a word

cd touch My File touch 'My File' touch "It's My File" touch Another\ File ls

' ' turn off all special characters except '" " turn off most special characters, $ () and ` is still special inside "" \ turns off any special meanings of only the very next character after the \

Page 14: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

File Name Globbing

As part of substitution, bash looks for *, ?, and [ ] in words on the command line

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

Give extra thought to escaping these characters on your command line

ls * echo * ls .?

Page 15: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

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

Bash can do basic arithmetic +, -, *, /, % on integers by putting arithmetic statements inside $(( )) - there are additional operators, see ARITHMETIC EVALUATION on the bash manual page

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))

Page 16: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Lab 2 - diceroller.sh

Page 17: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Variables

Every process has memory-based storage to hold named data

A named data item is called a variable

Variables can hold any data as their value, but usually hold text data, aka strings

myvar=3 variable2="string data" vowels=a e i o u

Variables are created by assigning data to them, using =

It is important to not leave any spaces around the = sign

The assigned text must be a single token for bash, escape your spaces!

Page 18: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Variables

Since a variable is stored in process memory, it stays around until we get rid of it, or the process ends

To access the data stored in a variable, use $variablename

Non-trivial variable names must be surrounded by { }, ordinary names do not require them

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

myvar=hello echo $myvar echo ${#myvar}

var[2]="silly name" echo ${var[2]} echo ${#var[2]}

var[2]=silly name echo $var[2] echo #var[2]

Page 19: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Lab 2 - improveddice.sh

Page 20: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Dynamic Data on the Command Line

Most commands we run interactively use static data - data which has a fixed value that we enter literally when we type the command

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) echo "Today is $today." todaymessage="Today is $(date +%A)."

Page 21: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Lab 2 - welcome-example.sh

Page 22: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

User Input

Bash can get user input and put it in a variable

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

You can specify a prompt, specify what variable or variables to put the user data into, and there are other options

prompt="Enter 2 numbers " read -p "$prompt" usernumber1 usernumber2 echo "User gave us $usernumber1 and $usernumber2"

read -p "Input? " myvar echo $myvar

Page 23: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Lab 2 - arithmetic-demo.sh

Page 24: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Working With Strings

Many command line tools are available to parse and manipulate strings

grep (used to search for patterns in text)

tr (used to do trivial character substitutions in text)

cut (used to extract portions of text from input text data)

sed, awk - advanced text manipulation tools

expr index string searchtext will return a non-zero index value if searchtext is in string

See http://tldp.org/LDP/abs/html/string-manipulation.html for more pure bash string manipulation techniques and information

Page 25: bash Data Handling - GitHub Pages · 2020-03-10 · Bash Conditional Execution When a command is run, it may fail and cause other commands to have problems or become unnecessary A

Show me your work to get the marks for it