• No results found

Outline. Unix shells Bourne-again Shell (bash) Interacting with bash Basic scripting References

N/A
N/A
Protected

Academic year: 2021

Share "Outline. Unix shells Bourne-again Shell (bash) Interacting with bash Basic scripting References"

Copied!
19
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Outline

• Unix shells

• Bourne-again Shell (bash) • Interacting with bash

• Basic scripting • References

(3)

Unix shells

• This lets users issue commands to the Unix

operating system

• Users can interact with various parts of the Unix

operating system such as

– the input/output system

– the scheduler

– memory management

• The shell is simply a program whose job is to

execute other programs

• Different shells exist such as the C shell (csh),

the Korn shell (ksh), the Bourne shell (sh) and the Bourne-again shell (bash)

(4)

Bourne-again shell (bash)

• bash is the default shell on modern Linux

distributions as well as Mac OS X

• bash is both a command interpreter and a

high-level programming language

• When used as a programming language, bash

processes commands stored in files called shell scripts

• Most system shell scripts are written for bash • Like other programming languages, bash has

variables and control flow commands (i.e. for loops and if statements)

(5)

Active participation

• It is highly encouraged to try all the examples as

we go along

• You may use your own shell from a Linux/Mac

terminal

• You may also use the free linux shell provided

(6)

Shell Variables

• Variables in a bash script are typically written

with all-uppercase letters

– This is not a strict requirement

• Assignment is done using the equals sign

without spaces

VAR1=“test string”

• Referencing the value of a variable is done using

the dollar sign

echo “My assigned variable contains $VAR1”

– An exception to this rule is evaluating arithmetic expressions, covered later

(7)

Documenting bash scripts with

comments

• It is always a good idea to document bash

scripts with comments

• Any text immediately following the # character

will be considered a comment and ignored by the shell

# This is a comment

• The exception to this the shebang #! found at

the top of most scripts

• The shebang specifies which shell to use to

execute the script

(8)

Command substitution

• To assign the value of another bash command/

script to a variable, command substitution is used

TODAYS_DATE=$(date “+%m/%d/%y”) TODAYS_DATE=`date “+%m/%d/%y”` echo $TODAYS_DATE

echo $(TODAYS_DATE)

• Newer scripts will use $(), while older scripts

will use backticks ` `

• The $() convention makes it easier to read and

(9)

•  Visit http://www.simpleshell.com

•  Click ‘Start my session’

•  nano hello_world.sh •  Ctrl-X •  Y to save modified buffer •  Hit return/ enter to accept filename

(10)

Running our first bash script

• We can run our bash script several ways

• We can pass the script to the shell of our choice

/bin/bash ./hello_world.sh /bin/sh ./hello_world.sh

• We can specify which shell to use with the

shebang, and then make the script executable nano hello_world.sh

Insert #!/bin/bash at the top Ctrl x, y, enter to save chmod u+x ./hello_world.sh ./hello_world

(11)

Embedding variable values in text

• Suppose you have the following variable defined

PREFIX=sun

• If you wanted to echo the following words using

this prefix: sunflower, sunshine, sunset you would use

echo ${PREFIX}flower echo ${PREFIX}shine echo ${PREFIX}set

• Note the use of the braces

Without the braces, echo $PREFIXflower would return nothing since the variable PREFIXflower is undefined

(12)

Using parameters in a bash script

• scripts are often called using input parameters •  To reference these parameters use ${position #}

where position # is the specific parameter to use that starts at 1

• Example:

echo “The first input parameter is ${1}” echo “The second input parameter is ${2}”

• You can also get the number of input

parameters passed using ${#}

echo “There were ${#} input parameters passed”

(13)

Setting default values for parameters

•  Many times you would like parameter set to a default

value if none is given

•  Suppose you have a script that is expecting a directory as an input parameter, but needs to default to the $HOME

directory if none is specified

•  default_param.sh:

#!/bin/bash

# List the contents of the 1st argument

# Use the $HOME directory if none is specified TARGET=${1:-$HOME}

(14)

Setting default values for variables

• Just like parameters, variables can also have

default values assigned to them

• default_variable.sh:

#!/bin/bash

# Show the first 5 lines of $TARGET file TARGET=/proc/cpuinfo

head -5 ${TARGET:=./default_variable.sh} echo “TARGET file is $TARGET”

unset TARGET

echo “---”

head -5 ${TARGET:=./default_variable.sh} echo “TARGET file is $TARGET”

(15)

The for control structure

• for loop-index

do

commands

done

• The loop-index takes on the values of each of

the command line arguments

• Example:

# Display all of the command line arguments for INPUT_ARG

do

echo $INPUT_ARG done

(16)

The for…in control structure

• for loop-index in argument-list

do

commands

done

• The loop-index takes on the values of each

argument in the specified argument-list

• Example:

# Display all of the animals in a given list for ANIMAL in lions tigers bears

do

echo $ANIMAL done

(17)

The for control structure with C like

syntax

• bash also allows a syntax for for loops much

like the C programming language

• # count from 1 to 10 on a single line

for (( count=1; count<10; count+=1 )) do

echo –n “$count ” done

(18)

References

•  A Practical Guide to Linux® Commands, Editors, and

Shell Programming by Mark G. Sobell

•  Bash Cookbook: Solutions and Examples for Bash

Users by Carl Albing, JP Vossen, and Cameron Newham

•  http://en.wikibooks.org/wiki/Bash_Shell_Scripting

•  These slides will be posted on

(19)

Questions? / Contact

• Ryan Hulguin

References

Related documents

Having a uniform risk-averse value function for both costs and benefits requires that we use a different discount rate for each, which is contrary to standard practice. On the

Florido Paganelli MNXB01-2018 Bash Scripting Tutorial 3 13/49 The BASH environment: export.. Everytime one opens a terminal, the program bash is executed and a new environment is

34.The City of Manila, claiming that it can impose taxes under the Local Government Code, imposed a tax on banks (in addition to percentage tax on banks imposed in the

preceding 90 days to limit their sales under the rule during any three-month period to the greatest of (1) one percent of the outstanding securities of the class being sold, or (2)

The maximum lift and lowest drag were found for the wing with large flexibility and low mass so the passive deformation by aerodynamic forces creates a favourable shape for

For this purpose, we have proposed a simple star schema to store spatial and non-spatial information in data warehouse.. We have also given a framework of

(So adding pies and adding boys is not actually adding fractions.) A fraction is something different. It is related to pies and boys, but something more subtle. A fraction is

The large-volume Centrifugal fans with stainless steel housing Possible versions: z ATEX z Media temperatures up to 750°C z Increased protection class z Application-orientated