SLIDE 1
Bash shell SE 2XA3 Term I, 2020/21 Outline Shells Shell scripts - - PowerPoint PPT Presentation
Bash shell SE 2XA3 Term I, 2020/21 Outline Shells Shell scripts - - PowerPoint PPT Presentation
Bash shell SE 2XA3 Term I, 2020/21 Outline Shells Shell scripts Shell variables Command-line arguments For loops Conditionals Shell customization Environment variables Aliases Shells A Unix shell is a command-line interpreter
SLIDE 2
SLIDE 3
Shells
◮ A Unix shell is a command-line interpreter ◮ Interface to Unix/Linux OS ◮ Bourne bash shell; default on many systems ◮ which bash, answer /bin/bash
- nly searches the directories listed in the PATH
variable ◮ Also C shell csh, tcsh, Korn shell ksh, ...
SLIDE 4
Shell scripts ◮ Text file containing shell commands ◮ First line specifies the shell to be used ◮ #!/bin/bash bash shell ◮ #!/bin/sh -x calls bash in debug mode; prints each line ◮ #!/bin/csh C shell
SLIDE 5
Shell variables
◮ name=value no spaces around = ◮ To access the value, use $, e.g. $PATH ◮ Examples ◮ name=/usr/lib/orb echo $name will show /usr/lib/orb ◮ x=‘ls‘ echo $x will show all items in the current directory, as if you typed ls
‘ls‘ – see 3.5.4. command substitution in Bash manual in Help section.
SLIDE 6
Command-line arguments
◮ $0 name of the script/command ◮ $1 first argument ◮ $2 second argument, ... and so on ◮ $# is the number of command line arguments
Try the following script: #!/bin/bash echo "My name is $0" echo "First argument is $1" echo "Second argument is $2"
indicates space (blank)
"My name is $0" – see 3.1.2. quoting in Bash manual in Help
- section. Why 'My name is $0' would not work?
SLIDE 7
For loops
for i in list do # command(s) done # indicates comment, try the following script #!/bin/bash files=‘ls‘ for i in $files do echo "Echoing file name: " $i done
SLIDE 8
For loops
for i do # command(s) done default is the list of command-line arguments
SLIDE 9
Conditionals
if cmd if cmd then then # command(s) # command(s) fi else # command(s) fi ◮ cmd is any command or command sequence ◮ true is when it returns 0 ◮ false is when it returns = 0
SLIDE 10
Nested conditionals
if cmd if cmd then then # command(s) # command(s) else elif cmd if cmd then then # command(s) # command(s) else else # command(s) # command(s) fi fi fi Both notations will produce the same action; they are equivalent.
SLIDE 11
Tests
◮ test evaluates any conditional expression 0 if true, 1 if false ◮ same as [ args ], args is an expression if [ args ] then # command(s) else # command(s) fi
SLIDE 12
Tests
expression true if str1 = str2 str1 equals str2 str1 != str2 str1 does not equal str2
- r file
file exists and is readable
- w file
file exists and is writable
- d file
is a directory
- f file
is a regular file
- s file
is a file of size > 0 expr1 -a expr2 expr1 and expr2 are both true expr1 -o expr2 expr1 or expr2 is true ! expr1 expr1 is not true (logical negation)
SLIDE 13
Numeric tests
expression true if num1 -eq num2 num1 equals num2 num1 -ne num2 num1 does not equal num2 num1 -lt num2 num1 is smaller than num2 num1 -le num2 num1 is smaller or equal to num2 num1 -gt num2 num1 is greater than num2 num1 -ge num2 num1 is greater or equal to num2
SLIDE 14
Examples
◮ #!/bin/bash if [ "$1" = "foo" ] then echo "First argument is foo" else echo "First argument is not foo" fi ◮ if [ -r file.txt ] if file.txt is readable ◮ if [ "$1" = "foo" -a -r file.txt ] if the first argument is foo and file.txt is readable
SLIDE 15
Examples
◮ #!/bin/bash if [ $# -eq 2 ] then echo "Number of arguments is OK" elif [ $# -lt 2 ] then echo "Number of arguments is too small" else echo "Number of arguments is too big" fi
SLIDE 16
Shell customization
◮ .bashrc executes when Unix starts a new Bash shell ◮ .bash_profile executes on login .bashrc runs first ◮ C shell ◮ .cshrc ◮ .login
SLIDE 17
Environment variables
◮ PATH specifies where the shell searches for commands ◮ export defines a variable ◮ to add /usr/local/bin to the path export PATH=$PATH:/usr/local/bin ◮ to add current directory to the path export PATH=$PATH:. ◮ HOME home directory ◮ to see all environment variables, use printenv
SLIDE 18