经典shell 脚本实例
fi
done
#!/bin/bash
if [ $# -le 0 ]
then
echo "Not enough parameters"
exit 1
fi
#string="2 3 4 5 6"
#set string
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`
shift
印出该数值,然后再次要求用户输入数值。直到用户输入"end"停止。#!/bin/sh
unset var
while [ "$var" != "end" ]
do
echo -n "please input a number: "
ቤተ መጻሕፍቲ ባይዱ
read var
if [ "$var" = "end" ]
then
break
fi
Linux shell 脚本实例
1. 写一个脚本,利用循环计算 10 的阶乘#!/bin/sh
factorial=1
for a in `seq 1 10`
do
factorial=`expr $factorial \* $a`
done
echo "10! = $factorial"
2. 写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然后打
esac done #! /bin/sh a=10 b=20 c=30 value1=`expr $a + $b + $c` echo "The value of value1 is $value1" value2=`expr $c / $b` echo "The value of value2 is $value2" value3=`expr $c * $b` echo "The value of value3 is $value3" value4=`expr $a + $c / $b` echo "The value of value4 is $value4" var4=`expr $value4 - $value2` echo $var4
C) echo "your selection is Copy" ;;
D) echo "your selection is Delete" ;;
B) echo "your selection is Backup" ;;
Q) echo "Exit ..." break ;;
*) echo "invalid selection,please try again"
echo "Not enough parameters" exit 0 fi if [ $1 -eq $2 ]; then echo "$1 equals $2"
elif [ $1 -lt $2 ]; then echo "$1 less than $2"
elif [ $1 -gt $2 ]; then echo "$1 greater than $2"
#break
done
# !/bin/bash
# list a content summary of a number of RPM packages
# USAGE: showrpm rpmfile1 rpmfile2 ...
# EXAMPLE: showrpm /cdrom/Thizlinux/RPMS/*.rpm
for rpmpackage in $*;
do
if [ -r "$rpmpackage" ];
then
echo "=============== $rpmpackage =============="
/bin/rpm -qip $rpmpackage
else
echo "ERROR: cannot read file $rpmpackage"
则循环刚才的过程,否则退出。
#!/bin/bash
# favourite OS.
samli
2004.4.19
echo "What is your favourite OS?"
select var in "Linux" "UNIX" "Windows" "Other"; do
echo "You have selected $var."
# #answer="no" # #e cho "***********************************************************************" #e cho "Please press "Enter" key to continue"
#read #while [ $answer = "no" ] #do # echo # echo "1.Are you a boy ? (yes/no)" # read answer #done # #answer="no" # #until [ $answer = "yes" ] #do # echo # echo "2.Are you foolish ? (yes/no)" # read answer #done # #answer="no" # #until [ $answer = "yes" ] #do # echo # echo "3.Do you love me ? (yes/no)" # read answer #done # #e cho "***********************************************************************" #e cho #e cho "Now ! I know you are a foolish girl and you love me."
#! /bin/sh sum=0 for i in $* do
sum=`expr $sum + $i` done echo $sum abc=123 echo $abc
###定义函数 example1() { abc=456 } ###调用函数 example1 echo $abc abc=234234 example1 echo $abc ###定义函数,使用参数 example2() { echo $1 echo $2 } ###调用函数,向它传递参数 example2 abc bbb example2 dksdfsdfsfaa bbb #!/bin/bash echo "please input a file name:" read file_name if [ -d $file_name ] then echo "$file_name is a directory" elif [ -f $file_name ] then echo "$file_name is a regular file" elif [ -c $file_name -o -b $file_name ] then echo "$file_name is a device file" else echo "$file_name is an unkonwn file" fi #! /bin/sh read first second third echo "the first parameter is $first" echo "the second parameter is $second" echo "the third parameter is $third" #! /bin/sh if [ $# -ne 2 ]; then
read op
case $op in
C)
echo "your selection is Copy"
;;
D)
echo "your selection is Delete"
;;
B)
samli
2004.4.19
echo "your selection is Backup" ;; *)
echo "invalid selection" esac #! /bin/sh while true do
tar jxvf $1
;; gz)
tar zxvf
$1
;; *)
echo "wrong file type"esac6.写一个脚本以方便用户查询 rpm 的
相关信息。这个脚本首先提示用户选择查询依据,比如文件名,包名,全部等。然后提示用户
选择查询信息,比如包名,包里所包含的所有文件,包的信息等。然后询问是否继续查询,是
if [ "$var" = "end" ]
then
break
fi
echo "var is $var"