#!/bin/sh    
factorial=1    
for a in `seq 1 10`
do    
                factorial=`expr $factorial \* $a`    

done    
echo "10! = $factorial"    

注:上面有一行,for a in `seq 1 10`,其中seq 1 10 , 即列出现1到10之间所有的数字,这一行也可改为:for a in "1 2 3 4 5 6 7 8 9 10"



#!/bin/sh    

    

unset var    

    

while [     "$var" != "end" ]    

do    

            echo -n "please input a number: "    

            read var    

            if [ "$var" = "end" ]    

            then    

                    break    

            fi    

            echo "var is $var"    

done
 
 
3. 写一个脚本,利用循环和continue关键字,计算100以内能被3整除的数之和
#!/bin/sh    

    

sum=0    

for a in `seq 1 100`    

do    

            if [ `expr $a % 3` -ne 0 ]    

            then    

                        continue    

            fi    

            echo $a    

            sum=`expr $sum + $a`    

done    

    

echo "sum = $sum"
 
 
4.一个函数,利用shift计算所有参数乘积,假设参数均为整数( 特殊变量$# 表示包含参数的个数)
#! /bin/sh    

    

result=1    

while [ $# -gt 0 ]    

do    

            result=`expr $result \* $1`    

            shift    

done    

echo $result    
 
 

#!/bin/sh    

    

case ${1##*.tar.} in    

            bz2)    

                    tar jxvf $1    

                    ;;    

            gz)    

                    tar zxvf $1    

                    ;;    

            *)    

                    echo "wrong file type"    

esac
 
 


#!/bin/sh    

RPM=/bin/rpm    

option="-q"    

    

while true    

do    

                echo "what to query?"    

                select var in     "All" "file" "package name"    

                do    

                             case $var in    

                             All)    

                                             option=$option"a"    

                                             break    

                                            ;;    

                             file)    

                                             echo -n "please input file name: "    

                                             option=$option"f"    

                                             read argument    

                                             break    

                                            ;;    

                                package\ name)    

                                             echo -n "please input package name: "    

                                             read argument    

                                             break    

                                            ;;    

                             *)    

                                             echo "please choose between 1-3"    

                                            ;;    

                             esac    

                done    

    

                echo "what do you want to know?"    

                select var in "location" "info" "package name"    

                do    

                             case $var in    

                                location)    

                                             option=$option"l"    

                                             break    

                                            ;;    

                             info)    

                                             option=$option"i"    

                                             break    

                                            ;;    

                                package\ name)    

                                             break    

                                            ;;    

                             *)    

                                             echo "please choose between 1-3"    

                                            ;;    

                             esac    

                done    

    

                ${RPM}     $option $argument    

    

                echo "continue? [yes/no]"    

                read answer    

    

                if [ answer = "no" ]    

                then    

                             break    

                fi    

done    

0

收藏

sucre03

715篇文章,305W+人气,1粉丝