CHAPTER 8 (skip 299-310, 333-354) CHAPTER 10 John Carelli, Instructor Kutztown University carelli@kutztown.edu
Interactive login shell First shell to provide a prompt upon login Executes the following in order /etc/profile .bash_profile .bash_login .profile Interactive nonlogin shell When a new terminal is opened only executes .bashrc source filename execute commands in named file OR execute it as a shell script (next)
Shell Scripts (again) Shell Script: an executable (text) file that contains shell commands. When the shell script is run, the commands in it get executed bash script #!/bin/bash Must be executable # comments execution in current shell with . (dot) changes to variables persist in current shell ex: testvar
Variable name Assignment Display Array variables Declare statement $ Array variables ${name[*]} … or ${name[@]} ${#name[*]} … or ${#name[@]} ex: arrayvars Declare statement declare varname declare –r varname # readonly – like const unset
Single quote ' Double quote “ Backslash \ metacharacter “escape” Back quote ` command substitution ex: quotescript
alias unalias Quotes in aliases only matters if alias contains a command substitution single: substitution occurs when executed double: substitution occurs when created ex: aliases
Arithmetic Expressions Double parenthesis evaluate an arithmetic expression syntax: (( arithmetic expression )) (( z = x + y )) Can drop $ in variable names ex: arith
Operators if – then - fi if-else elif ex: operators Test and [] Arithmetic Relational String comparison -n -z File testing if – then - fi if-else elif ex: operators
Control Statements while for until break continue ex: looping case select ex: menu (see also: getopt) 8
Command-line arguments shift ex: shifting $# Number of positional parameters $$ PID $? Exit status of most recently executed command $0 Program name $1 - $n Positional parameters $* All positional parameters $@
Definition Arguments return export there is no scoping – variables are global except for cmd line args (can’t change those) ex: functest
type read exec trap