Top Banner
Shell scripts Special variables Conditionals Conditional operators Case Loops for while Functions Extras Lab 4: Shell scripting Comp Sci 1585 Data Structures Lab: Tools for Computer Scientists
28

Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

May 30, 2018

Download

Documents

hoangthu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Lab 4: Shell scripting

Comp Sci 1585Data Structures Lab:

Tools for Computer Scientists

Page 2: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 3: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

What is shell scripting good for?

Shell scripts are the duct tape and bailing wire of computerprogramming.

You can use them:

• To automate repeated tasks

• For jobs that require a lot of interaction with files

• To set up the environment for big, complicated programs

• When you need to stick a bunch of programs together intosomething useful

• To add customizations to your environment

Page 4: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

A practical example $ runit1.sh

#!/bin/bash

g++ *.cpp

./a.out

Page 5: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 6: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Special Variables

• $? Exit code of the last command run

• $0 Name of command that started this script (almostalways the script’s name)

• $1, $2, ..., $9 Comand line arguments 1-9

• $@ All command line arguments except $0

• $# The number of command line arguments in $@

And now, a brief message from our sponsors:

• Bash really likes splitting things up into words.

• $ for arg in $@ will NOT do what you want.

• $ for arg in "$@" correctly handles args with spaces.

• In general, when using the value of a variable you don’tcontrol, it is wise to put " s around the variable.

Page 7: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

A Spiffier Example $ runit2.sh

#!/bin/bash

g++ *.cpp -o "$1"

./"$1"

Page 8: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 9: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Conditional Statements $ if.sh

#!/bin/bash

# Emit the appropriate greeting for various people

if [[ $1 = "Jeff" ]]; then

echo "Hi, Jeff"

elif [[ $1 == "Maggie" ]]; then

echo "Hello, Maggie"

elif [[ $1 == *.txt ]]; then

echo "You’re a text file, $1"

elif [ "$1" = "Stallman" ]; then

echo "FREEDOM!"

else

echo "Who in blazes are you?"

fi

Page 10: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 11: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Conditional Operators

• [ ] is shorthand for the $ test command.

• [[ ]] is a bash keyword.

• [ ] works on most shells, but [[ ]] is less confusing.

• (( )) is another bash keyword. It does arithmetic.

Page 12: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

String Comparison Operators for [[ ]]

• =, == String equality OR pattern matching if the RHS isa pattern.

• != String inequality.

• < The LHS sorts before the RHS.

• > The LHS sorts after the RHS.

• -z The string is empty (length is zero).

• -n The string is not empty (e.g. $ [[ -n "$var" ]] ).

Page 13: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Numeric Comparison Operators for [[ ]]

• -eq Numeric equality (e.g. $ [[ 5 -eq 5 ]] ).

• -ne Numeric inequality.

• -lt Less than

• -gt Greater than

• -le Less than or equal to

• -ge Greater than or equal to

Page 14: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

File Operators for $ [[ ]]

• -e True if the file exists (e.g. $ [[ -e story.txt ]] )

• -f True if the file is a regular file

• -d True if the file is a directory

There are a lot more file operators that deal with even fancierstuff.

Page 15: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

General Operators for [[ ]]

• && Logical AND

• || Logical OR

• ! Logical NOT

• You can use parentheses to group statements too.

Page 16: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Shell Arithmetic with (( ))

• This mostly works just like C++ arithmetic does.

• ** does exponentiation

• You can do ternaries! (( 3 < 5 ? 3 : 5 ))

• You don’t need $ on the front of normal variables.

• Shell Arithmetic Manual

Page 17: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Spiffy++ Example $ runit3.sh

#!/bin/bash

if (( $# > 0 ))

then

g++ *.cpp -o "$1"

exe="$1"

else

g++ *.cpp

exe=a.out

fi

if [[ $? -eq 0 ]]

then

./"$exe"

fi

(Could you spiff it up even more with file checks?)

Page 18: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 19: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Case statements#!/bin/bash

case $1 in

a)

echo "a, literally"

;;

b*)

echo "Something that starts with b"

;;

*c)

echo "Something that ends with c"

;;

"*d")

echo "*d, literally"

;;

*)

echo "Everything else"

;;

esac

Page 20: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 21: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 22: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

For Looping $ for.sh

#!/bin/bash

echo C-style:

for (( i=1; i < 9; i++ ))

do

echo $i;

done

echo BASH-style:

for file in *.sh

do

echo $file

done

Page 23: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 24: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

While Looping $ while.sh

#!/bin/bash

input=""

while [[ $input != "4" ]]

do

echo "Please enter the displayed number"

read input

done

Page 25: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 26: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Functions $ function.sh

#!/bin/bash

parrot()

{

while (( $# > 0 ))

do

echo "$1"

shift

done

}

parrot These are "several arguments"

Page 27: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Outline

1 Shell scripts

2 Special variables

3 ConditionalsConditional operatorsCase

4 Loopsforwhile

5 Functions

6 Extras

Page 28: Lab 4: Shell scripting - web.mst.edutaylorpat/Courses_files/DataStructuresLab/...Data Structures Lab: Tools for Computer Scientists. ... complicated programs ... This mostly works

Shell scripts

Specialvariables

Conditionals

Conditionaloperators

Case

Loops

for

while

Functions

Extras

Miscellany

• Escaping characters: use \ on \, `, $, ", ’, #

• $ pushd and $ popd create a stack of directories

• $ dirs lists the stack

• Use these instead of $ cd

• $ set -u gives an error if you try to use an unsetvariable.

• $ set -x prints out commands as they are run.

• $ help COMMAND gives you help with builtins.