Bash Scripting Flashcards
What do you put at the beginning of bash scripts?
!/bin/bash
Where should you place a script if you want it to be executable from anywhere?
/home/user/bin
What do you have to do to the script myscript before you can run it?
chmod u+x myscript
myvar = $1
Will assign the value of the first argument to myvar
$#
Refers to the total number of arguments
How to do a numerical comparison with an if statement in a bash script
if (( $var == 0 ))
What does an if block end with?
fi
if test -f file
Tests if $file is a file
if [ -f $file ]
Tests if $file is a file
if [ -z “$string” ]
Checks if $string is empty
if statement flag for greater than
-gt
if statement flag for less than
-lt
if statement flag for greater than or equal
-ge
if statement flag for less than or equal
-le
if statement flag for equal
-eq
if statement flag for not equal
-ne
if statement flag for testing if directory
-d
if statement flag for testing if symlink
-l
What does putting a command between ` ` do in a bash script?
Executes the command in a new shell then immediately closes the shell
Which brackets must you use in order to use regular expressions and && and stuff?
[[ ]]
if statement flag for &&
-a
if statement flag for ||
-o
for statement syntax
for a in b
do
blahblah
done
while statement syntax
while [some condition];
do
blahblah;
done
case statement syntax
case $var in 1) first do this thing;; 2) second do this other thing;; *) this is the default esac
read -n2 var
will read in 2 characters and put them in var
read -t
If input is not given within a certain time frame the script will move on
if [ -x $file ]
If $file exists and is executable
seq 10
Will print 1 through 10 in sequential order
seq 1 5 20
Will print 1-20 in increments of 5