Friday, 20 February 2015

Sample SHELL program to illustrate - 1) Declaring variables 2) Writing to STDOUT, 3) reading user input from STDIN, 4) Using IF statement 5) Using FOR and WHILE loops

echo -n "What is your name ? "
read name
echo "Hi $name"

echo -n "Enter a number: "
read num

echo -n "Even numbers below $num: "
for((i = 1; i <= $num; i++))
do
  if [ `expr $i % 2` == 0 ]
  then
    echo -n "$i ";
  fi
done
echo -e ""

echo -n "Odd numbers below $num: "
j=1
while [ $j -le $num ]
do
  if [ `expr $j % 2` != 0 ]
  then
    echo -n "$j "
  fi
  j=`expr $j + 1`
done
echo -e ""

No comments:

Post a Comment