Shell Script Examples: case, count, for, if, while and User input

If you, like so many other people these days, switch to Linux you will come in contact with something called unux scripts, shell scripts or bash scripts. These scripts are text-files that execute commands just like bat-files in Windows. I prefer shell-scripts because i think they are more powerful then bat-files but that is just my opinion.

Some things you should know:
#!/bin/bash usually is the first line in a bash script. It is called shebang (also called a hashbang, hashpling, or pound bang) and specifies in which shell to execute the code that follows below.

Everything after a # is a comment and does not get executed.

Below you will find examples of a few things that you can do with shell scripts on Linux and other Unixes. If you have questions: have a look at the man page of the command or post a comment and I will try to explain.

case

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
 
MENU="
1   Date and Time
2   Calendar for current month
3   quit
"
 
while true; do
  clear
  echo "$MENU"
  echo -n "Please make your choice: "
  read INPUT # Read user input and assign it to variable INPUT
 
  case $INPUT in
    1)
    date
    echo press ENTER to continue
    read
    ;;
    2)
    cal
    echo press ENTER to continue
    read
    ;;
    3|q|Q) # If user presses 3, q or Q we terminate
    exit 0
    ;;
    *) # All other user input results in an usage message
    clear
    echo Please choose alternatves 1, 2 or 3
    sleep 2
    ;;
  esac
 
done

count

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
BEGIN=1 # Start counting here
END=10 # Stop counting here
 
END=`expr $END + 1`
 
while [ $END -ne $BEGIN ]; do # While END is not equal to BEGIN do ...
  echo This is iteration $BEGIN
  BEGIN=`expr $BEGIN + 1` # Increasing the value of BEGIN by one
done

for

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
 
# Defining Variable
STAFF="Anders Bertil Cesar Denise Emilia Filippa"
 
echo First LOOP
for PERSON in $STAFF; do
  echo $PERSON is a member of the staff.
done
 
echo Second LOOP
echo -n "The staff members are: "
for PERSON in $STAFF; do
  echo -n "$PERSON " # -n instructs echo not to print the trailing newline char
done
echo

if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
VALIDUSER="Thorsten"
 
if [ $VALIDUSER = anders ]; then
  echo $VALIDUSER is a valid user
  exit 0
elif [ $VALIDUSER = bertil ]; then
  echo $VALIDUSER is a valid user
  exit 0
elif [ $VALIDUSER = Thorsten ]; then
  echo $VALIDUSER is a valid user
  exit 0
else
  echo no valid user found
  exit 1
fi

while

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
 
START=1 # The value to begin with
END=10  # When this value is reached we are done
SPEED=1 # Number of seconds between each iteration
while [ $START -le $END ]; do
  echo this is iteration $START of $END
  START=`expr $START + 1`
  sleep $SPEED
done

User Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
 
# Check if there is input to this script
if [ $# -lt 1 ]; then # $# contains the total number of arguments to the script
  echo "Usage: $0 [argument1 ...]" # $0 is the executed command
  exit 1
fi
 
NUMPARAMETERS=`echo $#` # The number of parameters provded to this script
 
echo "This script has $NUMPARAMETERS parameters"
 
COUNT=1
 
while [ $COUNT -le $NUMPARAMETERS ]; do
  echo Parameter $COUNT contains $1 # $1 is the first argument
  COUNT=`expr $COUNT + 1` # Increasing COUNT by one
  shift # Shifts input parameters. $2 becomes $1 and the old $1 is discarded
done
Apr 17th, 2009 | Posted in Computers
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">