Open main menu

CDOT Wiki β

Changes

OPS435 Lecture 4 - Bash

1,206 bytes added, 13:40, 22 August 2017
m
* Program control:
** if
** test, [** while[[*Parameter expansion examples** echo $1** echo ${11}* forInput from the user** read command == In-class examples == runme.sh:<source lang="bash">#!/bin/bash echo "First parameter was $1"echo Second: $2echo 'Third parameter: ' $3echo "Fourth parameter was $4"</source> divide.sh:<source lang="bash">#!/bin/bash if [[ $# -lt 1 ]]then echo "Not enough parameters" exitfi echo $(( $1 / 2 ))</source> stringtest.sh:<source lang="bash">#!/bin/bash COMPARETO=helloCOMPARETO="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/nullthen echo "Correct distribution"else echo "Please run this script on linux mint" exitfi 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" exitfi</source> ask.sh:<source lang="bash">#!/bin/bash echo "Script started"echo "Please choose one of the following files: "cd /var/logls syslog* echo -n "Your choice: "read CHOSENNAME echo $CHOSENNAME</source>