OPS435 Lecture 4 - Bash
(Redirected from OPS435 Lecture 4)
- Writing a script
- Receiving parameters
- Return codes
- true and false commands
- Program control:
- if
- test, [
- [[
- Parameter expansion examples
- echo $1
- echo ${11}
- Input from the user
- read command
In-class examples
runme.sh:
#!/bin/bash
echo "First parameter was $1"
echo Second: $2
echo 'Third parameter: ' $3
echo "Fourth parameter was $4"
divide.sh:
#!/bin/bash
if [[ $# -lt 1 ]]
then
echo "Not enough parameters"
exit
fi
echo $(( $1 / 2 ))
stringtest.sh:
#!/bin/bash
COMPARETO=hello
COMPARETO="hello"
COMPARETO='hello'
if [ x$1 = x$COMPARETO ]
then
echo "You guessed the string"
else
echo "Guess again"
fi
testcommand.sh:
#!/bin/bash
if ls /etc/linuxmint > /dev/null 2> /dev/null
then
echo "Correct distribution"
else
echo "Please run this script on linux mint"
exit
fi
FILETYPE=`file -b /etc/linuxmint`
#if [ "$FILETYPE" = "directory " ]
if test "$FILETYPE" = "directory "
then
echo "It's a directory as I expected"
else
echo "It's not a directory"
exit
fi
ask.sh:
#!/bin/bash
echo "Script started"
echo "Please choose one of the following files: "
cd /var/log
ls syslog*
echo -n "Your choice: "
read CHOSENNAME
echo $CHOSENNAME