Changes

Jump to: navigation, search

OPS435 Lecture 4 - Bash

1,134 bytes added, 15:01, 2 February 2016
no edit summary
* Input from the user
** 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>

Navigation menu