Difference between revisions of "OPS435 Lecture 4 - Bash"
m (Andrew moved page OPS435 Lecture 4 to OPS435 Lecture 4 - Bash) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
** test, [ | ** test, [ | ||
** [[ | ** [[ | ||
− | |||
− | |||
* Parameter expansion examples | * Parameter expansion examples | ||
** echo $1 | ** echo $1 | ||
Line 14: | Line 12: | ||
* 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> |
Latest revision as of 12:40, 22 August 2017
- 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