Thursday, April 14, 2016

Shell Scripts Examples

Examples:
#!/bin/sh
# This is some secure program that uses security.

VALID_PASSWORD="secret" #this is our password.

echo "Please enter the password:"
read PASSWORD

if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then
        echo "You have access!"
else
        echo "ACCESS DENIED!"
fi

#!/bin/sh
 
# Prompt for a user name...
echo "Please enter your age:"
read AGE
 
if [ "$AGE" -lt 20 ] || [ "$AGE" -ge 50 ]; then
        echo "Sorry, you are out of the age range."
elif [ "$AGE" -ge 20 ] && [ "$AGE" -lt 30 ]; then
        echo "You are in your 20s"
elif [ "$AGE" -ge 30 ] && [ "$AGE" -lt 40 ]; then
        echo "You are in your 30s"
elif [ "$AGE" -ge 40 ] && [ "$AGE" -lt 50 ]; then
        echo "You are in your 40s"
fi

#!/bin/sh

a=10

b=20

if [ $a == $b ]

then

   echo "a is equal to b"

else

   echo "a is not equal to b"

fi
Until Loop:
The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. Sometimes you need to execute a set of commands until a condition is true.
#!/bin/sh

a=10

until [ $a -ge 10 ]

do

   echo $a

   a=`expr $a + 1`

done
__________________________________________________
#!/bin/sh

a=0

until [ ! $a -lt 10 ]

do

   echo $a

   a=`expr $a + 1`

done

Break command: -The break statement is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement
#!/bin/sh

a=0

while [ $a -lt 10 ]

do

   echo $a

   if [ $a -eq 5 ]

   then

      break

   fi

   a=`expr $a + 1`

done

Examples for FOR Loop:
1. Static values for the list after “in” keyword
$ cat for1.sh
i=1
for day in Mon Tue Wed Thu Fri
do
 echo "Weekday $((i++)) : $day"
done
$ cat for1-wrong1.sh
i=1
for day in Mon, Tue, Wed, Thu, Fri
do
 echo "Weekday $((i++)) : $day"
done
$ cat for1-wrong2.sh
i=1
for day in "Mon Tue Wed Thu Fri"
do
 echo "Weekday $((i++)) : $day"
done
$ cat for2.sh
i=1
weekdays="Mon Tue Wed Thu Fri"
for day in $weekdays
do
 echo "Weekday $((i++)) : $day"
done
$ cat for2-wrong.sh
i=1
weekdays="Mon Tue Wed Thu Fri"
for day in "$weekdays"
do
 echo "Weekday $((i++)) : $day"
done
$ cat for3.sh
i=1
for day
do
 echo "Weekday $((i++)) : $day"
done


O/P: - $ ./for3.sh Mon Tue Wed Thu Fri
Weekday 1 : Mon
Weekday 2 : Tue
Weekday 3 : Wed
Weekday 4 : Thu
Weekday 5 : Fri
$ cat for4.sh
i=1
for day in
do
 echo "Weekday $((i++)) : $day"
done


O/P: - $ ./for3.sh Mon Tue Wed Thu Fri
Will get no output for above script.


$ cat for5.sh
i=1
cd ~
for item in *
do
 echo "Item $((i++)) : $item"
done

$ cat for5-1.sh
i=1
for file in /etc/[abcd]*.conf
do
 echo "File $((i++)) : $file"
done

$ cat for6.sh
i=1
for day in Mon Tue Wed Thu Fri
do
 echo "Weekday $((i++)) : $day"
 if [ $i -eq 3 ]; then
   break;
 fi
done

Example for Continue:
$ cat for7.sh
i=1
for day in Mon Tue Wed Thu Fri Sat Sun
do
 echo -n "Day $((i++)) : $day"
 if [ $i -eq 7 -o $i -eq 8 ]; then
   echo " (WEEKEND)"
   continue;
 fi
 echo " (weekday)"
done

$ cat for8.sh
for (( i=1; i <= 5; i++ ))
do
 echo "Random number $i: $RANDOM"
done
Infinite Bash loop script

$ cat for9.sh
i=1;
for (( ; ; ))
do
   sleep $i
   echo "Number: $((i++))"
done

Range of numbers after “in” keyword

$ cat for11.sh
for num in {1..10}
do
 echo "Number: $num"
done

Range of numbers with increments after “in” keyword

$ cat for12.sh
for num in {1..10..2}
do
 echo "Number: $num"
done

No comments:

Post a Comment