Shell Scripting Notes
To print random number you can call keyword RANDOM
$ echo $RANDOM
9920 or any number between 0 to 32767, we cannot define to get a random number between two
numbers, But you can still write a shell program to guess the number for 5 times. You can practice it.
I have taught the following in the class and you have written the program too.
1. Reading a file from top or from bottom (head or tail)
2. For loop using fruits
3. Getting user number and add them. And display it.
4. Showing data and time in different formats
5. Read a file and print each line separately I have made you do it.
6. Selecting from a menu
7. Salary calculation
8. Use $RANDOM to generate random number and guess the correct number.
9. Multiplication table
Multiplication Table
#!/bin/bash
echo "This scripts adds numbers entered from the "
echo "keyboard. "
echo "key ^d (eof) to see the total"
sum=0
echo "Enter the table number : "
read num
count=1
while [ $count -lt 11 ]
do
sum=`expr $num \* $count`
#echo $sum
echo "$num"x"$count" = $sum
count=`expr $count + 1`
#echo $count
Done
Display file based on r w or x.
#!/bin/bash
echo "enter the directory name"
read dir
if [ -d $dir ]; then
cd $dir
ls > f
exec < f
while read line
do
echo $line
if [ -f $line ]
then
if [ -r $line -a -w $line -a -x $line ]
then
echo "$line has all permissions"
else
echo "files not having all permissions"
fi
fi
done
fi
Menu/select
#!/bin/bash
select item in uno dos threas quartro punto
do
if [ ! -z "$item" ]; then
echo "You chose option number $REPLY which is \"$item\""
else
echo "$REPLY is not valid."
fi
done
Loop Add
#!/bin/bash
echo "This scripts adds numbers entered from the "
echo "keyboard. "
echo "key ^d (eof) to see the total"
sum=0
echo "Enter a number : "
while read data
do
((sum = sum+data))
echo "Enter next number: "
done
echo " Sum is : " $sum
Read lines from file
#!/bin/bash
while read msg
do
echo $msg
done < teeOut
List Write files
#!/bin/bash
echo -n "Enter the filename to check: "
read filename
if [ -w "$filename" ]; then
echo "$filename is writable file"
elif [ -x "$filename" ]; then
echo "$filename is executable file also"
fi