shell脚本实例

江枫思渺然 提交于 2021-02-03 07:20:34
1. 写一个脚本,利用循环计算10的阶乘
#!/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"
2. 写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然
后打印出该数值,然后再次要求用户输入数值。直到用户输入 "end"停止。
#!/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    
 
 
5.写一个脚本,可以根据参数文件名,以正确的参数调用tar来解压缩tar.gz或tar.bz2文件。
#!/bin/sh    

    

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

            bz2)    

                    tar jxvf $1    

                    ;;    

            gz)    

                    tar zxvf $1    

                    ;;    

            *)    

                    echo "wrong file type"    

esac
 
 
6.写一个脚本以方便用户查询rpm的相关信息。这个脚本首先提示用户选择查询依据,比如
文件名,包名,全部等。然后提示用户选择查询信息,比如包名,包里所包含的所有文件,
包的信息等。然后询问是否继续查询,是则循环刚才的过
程,否则退出。
#!/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人
了这篇文章
类别: Linux/Unix┆阅读( 0)┆评论( 0) ┆ 返回博主首页返回博客首页
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!