Difference between revisions of "OPS435 Lecture 4 - Bash"
Line 14: | Line 14: | ||
* Input from the user | * Input from the user | ||
** read command | ** read command | ||
+ | |||
+ | == In-class examples == | ||
+ | |||
+ | runme.sh: | ||
+ | <source lang="bash"> | ||
+ | #!/bin/bash | ||
+ | |||
+ | echo "First parameter was $1" | ||
+ | echo Second: $2 | ||
+ | echo 'Third parameter: ' $3 | ||
+ | echo "Fourth parameter was $4" | ||
+ | </source> | ||
+ | |||
+ | divide.sh: | ||
+ | <source lang="bash"> | ||
+ | #!/bin/bash | ||
+ | |||
+ | if [[ $# -lt 1 ]] | ||
+ | then | ||
+ | echo "Not enough parameters" | ||
+ | exit | ||
+ | fi | ||
+ | |||
+ | echo $(( $1 / 2 )) | ||
+ | </source> | ||
+ | |||
+ | stringtest.sh: | ||
+ | <source lang="bash"> | ||
+ | #!/bin/bash | ||
+ | |||
+ | COMPARETO=hello | ||
+ | COMPARETO="hello" | ||
+ | COMPARETO='hello' | ||
+ | |||
+ | if [ x$1 = x$COMPARETO ] | ||
+ | then | ||
+ | echo "You guessed the string" | ||
+ | else | ||
+ | echo "Guess again" | ||
+ | fi | ||
+ | </source> | ||
+ | |||
+ | testcommand.sh: | ||
+ | <source lang="bash"> | ||
+ | #!/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 | ||
+ | </source> | ||
+ | |||
+ | ask.sh: | ||
+ | <source lang="bash"> | ||
+ | #!/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 | ||
+ | </source> |
Revision as of 15:01, 2 February 2016
- Writing a script
- Receiving parameters
- Return codes
- true and false commands
- Program control:
- if
- test, [
- [[
- while
- for
- 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