Shell 编程简单例子:
1. How to write shell script that will add two numbers, which are supplied as command line argument, and if this two numbers are not given show error and its usage?
#!/bin/bash
# sum to nos
if [ $# -ne 2 ]
then
echo "Usage - $0 x y"
echo " Where x and y are two nos for which I will print sum"
exit 1
fi
echo "Sum of $1 and $2 is `expr $1 + $2`"
|
2. Write Script to find out biggest number from given three numbers. Numbers are supplies as command line argument. Print error if sufficient arguments are not supplied?
#!/bin/bash
# find out biggest number
if [ $# -ne 3 ]
then
echo "$0: number1 number2 number3 are not given" >&2
exit 1
fi
n1=$1
n2=$2
n3=$3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
echo "$n1 is Bigest number"
elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
echo "$n2 is Bigest number"
elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]
then
echo "$n3 is Bigest number"
elif [ $1 -eq $2 ] && [ $1 -eq $3 ] && [ $2 -eq $3 ]
then
echo "All the three numbers are equal"
else
echo "I can not figure out which number is biger"
fi |
3. Write script to print numbers as 5,4,3,2,1 using while loop?
#!/bin/bash
# print numbers as 5,4,3,2,1 using while loop
i=5
while test $i != 0
do
echo "$i"
i=`expr $i - 1`
done
|
4. Write Script, using case statement to perform basic math operation. The name of script must be 'q4' which works as follows $ ./q4 20 / 3, Also check for sufficient command line arguments?
#!/bin/bash
# using case statement to perform basic math operation
if test $# = 3
then
case $2 in
+) let z=$1+$3;;
-) let z=$1-$3;;
/) let z=$1/$3;;
x|X) let z=$1*$3;;
*) echo Warning - $2 invalied operator, only +,-,x,/ operator allowed
exit;;
esac
echo Answer is $z
else
echo "Usage - $0 value1 operator value2"
echo " Where, value1 and value2 are numeric values"
echo " operator can be +,-,/,x (For Multiplication)"
fi
|
5. Write Script to see current date, time, username, and current directory?
#!/bin/bash
# see current date, time, username, and current directory
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current direcotry `pwd`"
|
6. Write script to print given number in reverse order, for eg, if number is 123 it must print as 321?
#!/bin/bash
# print given number in reverse order if [ $# -ne 1 ] then echo "Usage: $0 number" echo " I will find reverse of given number" echo " For eg. $0 123, I will print 321" exit 1 fi
n=$1 rev=0 sd=0
while [ $n -gt 0 ] do sd=`expr $n % 10` rev=`expr $rev /* 10 + $sd` n=`expr $n / 10` echo "$sd $rev $n" done echo "Reverse number is $rev" |
7. Write script to print given numbers sum of all digit, For eg. If number is 123 it's sum of all digit will be 1+2+3 = 6?
#!/bin/bash
# print given numbers sum of all digit if [ $# -ne 1 ]
then
echo "Usage: $0 number"
echo " I will find sum of all digit for given number"
echo " For eg. $0 123, I will print 6 as sum of all digit (1+2+3)"
exit 1
fi
n=$1
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Sum of digit for number is $sum"
|
8. How to perform real number calculation in shell script and store result to
third variable , lets say a=5.66, b=8.67, c=a+b?
#!/bin/bash
# perform real number calculation a=5.66
b=8.67
c=`echo $a + $b | bc`
echo "$a + $b = $c"
|
9. Write script to determine whether given file exist or not, file name is supplied as command line argument, also check for sufficient number of command line argument?
#!/bin/bash # determine whether given file exist or not if [ $# -ne 1 ] then echo "Usage - $0 file-name" exit 1 fi
if [ -f $1 ] then echo "$1 file exist" else echo "Sorry, $1 file does not exist" fi |
10. Write script to determine whether given command line argument ($1) contains "*" symbol or not, if $1 does not contains "*" symbol add it to $1, otherwise show message "Symbol is not required"?
#!/bin/bash # determine whether given command line argument ($1) contains "*"
cat "$1" > /tmp/file.$$ 2>/tmp/file0.$$
grep "*" /tmp/file.$$ >/tmp/file0.$$
if [ $? -eq 1 ]
then
echo "Required i.e. $1/*"
else
echo "Symbol is Not required"
fi
rm -f /tmp/file.$$ rm -f /tmp/file0.$$ |
11. Write script to print contains of file from given line number to next given number of lines?
#!/bin/bash # # Print error / diagnostic for user if no arg's given # if [ $# -eq 0 ] then echo "$0:Error command arguments missing!" echo "Usage: $0 start_line uptoline filename" echo "Where start_line is line number from which you would like to print file" echo "uptoline is line number upto which would like to print" echo "For eg. $0 5 5 myfile" echo "Here from myfile total 5 lines printed starting from line no. 5 to" echo "line no 10." exit 1 fi
# # Look for sufficent arg's # if [ $# -eq 3 ]; then if [ -e $3 ]; then tail +$1 $3 | head -n$2 else echo "$0: Error opening file $3" exit 2 fi else echo "Missing arguments!" fi |
12. Write script to implement getopts statement, your script should understand following command line argument called this script Q12, Where options work as
-c clear the screen
-d show list of files in current working directory
-m start mc (midnight commander shell) , if installed
-e { editor } start this { editor } if installed?
#!/bin/bash # implement getopts statement # # Function to clear the screen # cls() { clear echo "Clear screen, press a key . . ." read return }
# # Function to show files in current directory # show_ls() { ls echo "list files, press a key . . ." read return }
# # Function to start mc # start_mc() { if which mc > /dev/null ; then mc echo "Midnight commander, Press a key . . ." read else echo "Error: Midnight commander not installed, Press a key . . ." read fi return }
# # Function to start editor # start_ed() { ced=$1 if which $ced > /dev/null ; then $ced echo "$ced, Press a key . . ." read else echo "Error: $ced is not installed or no such editor exist, Press a key . . ." read fi return }
# # Function to print help # print_help_uu() { echo "Usage: $0 -c -d -m -v {editor name}"; echo "Where -c clear the screen"; echo " -d show dir"; echo " -m start midnight commander shell"; echo " -e {editor}, start {editor} of your choice"; return }
# # Main procedure start here # # Check for sufficent args #
if [ $# -eq 0 ] ; then print_help_uu exit 1 fi
# # Now parse command line arguments # while getopts cdme: opt do case "$opt" in c) cls;; d) show_ls;; m) start_mc;; e) thised="$OPTARG"; start_ed $thised ;; /?) print_help_uu; exit 1;; esac done |
13. Write script called sayHello, put this script into your startup file called .bash_profile, the script should run as soon as you logon to system, and it print any one of the following message in infobox using dialog utility, if installed in your system, If dialog utility is not installed then use echo statement to print message : Good Morning,Good Afternoon,Good Evening , according to system time?
#!/bin/bash # sayHello using dialog utility
temph=`date | cut -c12-13`
dat=`date +"%A %d in %B of %Y (%r)"`
if [ $temph -lt 12 ]
then
mess="Good Morning $LOGNAME, Have nice day!"
fi
if [ $temph -gt 12 -a $temph -le 16 ]
then
mess="Good Afternoon $LOGNAME"
fi
if [ $temph -gt 16 -a $temph -le 18 ]
then
mess="Good Evening $LOGNAME"
fi
if which dialog > /dev/null
then
dialog --backtitle "Linux Shell Script Tutorial"/
--title "(-: Welcome to Linux :-)"/
--infobox "/n$mess/nThis is $dat" 6 60
echo -n " Press a key to continue. . . "
read
clear
else
echo -e "$mess/nThis is $dat"
fi
|