User login

shell scripting

Bash: if statements

An if statement in shell scripting looks something like this:


if [ "$VAR" = 1 ]
then
#do something
elif [ "$VAR" = 2 ]
then
#do something else
else
#do something by default
fi

Notice we're quoting the variable to protect from shell expansion.

There are a variety of file tests one can use:


if [ -e /tmp/test.file ]
then
#do something
elif [ -d "$VAR" ]
then
#do something else
else
#another default action
fi

Bash: variables

First in a series of notes in reference to shell scripting as I am just learning it myself. I have started on a script that will configure a bridged network interface for VirtualBox.

Working with variables in the shell is very easy.


#set a variable
VAR="Hello World"

#output the same variable
echo $VAR

#assign a new variable
#the value of our old variable
VAR2="$VAR"

echo $VAR2