Scripting Flashcards
What is the first line of a bash shell script?
!/bin/bash
Tells shell what program to interpret the script with
How do you exit with a specific exit code in a shell script?
exit 0
exit $?
*numbers 0-255, 0 means no errors, $? evaluate last line as exit code
What is the command to load variable into the shell from a shell script?
source scriptname.sh
. scriptname.sh
*. does the same thing
In bash, what is the stored variable containing the number of arguments the script was run with?
$#
In bash, what is the stored variable that contains the scripts own filename?
$0
In bash, what is the stored variable containing script arguments?
$1..$n
How do you access stored script only variables when in quotes?
${}
i.e $# > echo “${#}”
How do you access stored variables when in quotes?
$()
i.e date > echo “$(date)”
How do you write a function in a bash script?
function multiply() { echo 5 * $1 } *call function multiply 2
What is the $PATH variable?
Colon separated listing of directories that commands are found
How do you modify the $PATH variable?
append: PATH=$PATH:/directory
prepend: PATH=/directory:$PATH
How do you record a terminal session?
script
ctrl + d / exit to commit to file
How do you record a terminal session in real time?
script –timing=
scriptreplay -s -t
How do you record the output of a command non-interactively?
script -c ‘ls /’ scriptfilename
*useful for scripting as is non-interactive
What is the command to create aliases?
alias lr=’ls -R’
unalias lr
*unalias removes an alias binding