Simple Script

I had not written , something colorful like this from a long time , have tried to tie up all the basics in a single code.
Say i name it as fun.sh , and execute it , lets see what happens :O)


#!/bin/bash


# Variable(s)
user="";

# Function,can be defined without the function keyword !!
# Simple function to "read" user input

read_info()
{
echo "What is your name ? "
# read data from keyboard to user variable
read user
# Say hi to the user
echo "Hi $user"
}
ask()
{
echo "Which OS do you like ?"
echo "1) Linux"
echo "2) Windows"
echo "3) MAC"
echo "4) None"
echo "5) You wont tell me"

read case;

case $case in
1) echo "The best , Happy Hacking :)";;
2) echo "Who needs Gates and Windows , when you have freedom ";;
3) echo "You need an Apple?";;
4) echo"None!!!, What are you using now then!";;
5) exit
esac

}

# Note the spaces in after if condition and within [ ] , if you are using
# if [ $# -eq 0 ] or better go for if test $# -eq 0
# $# gives number fo command line args

if test $# -eq 0
then
echo "Number of command line arguments is $# !!"
else
# loop till the end of arguments and echo themall using "for" loop

echo "There are $# number of agrs they are {using for loop} : "
for arg in $*
do
echo $arg
done

# We can also do this:
# for args in "$@"
# do
# echo ${args}
# done

#
# loop till the end of arguments and echo themall using "while" loop


echo "There are $# number of agrs they are {using while loop} : "
while [ -n "$1" ]
do
echo $1
shift
# Yes,shift can be used to parse through the command line array
done

fi

read_info
ask

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ ./fun 1 2 3

Output :

There are 3 number of agrs they are {using for loop} :
1
2
3
There are 3 number of agrs they are {using while loop} :
1
2
3
Waht is your name ?
hemanth
Hi hemanth
Which OS do you like ?
1) Linux
2) Windows
3) MAC
4) None
5) You wont tell me
1
The best , Happy Hacking :) Share this