```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:"，要求用户输入数值，然

"end"停止。

```
#!/bin/sh

unset var

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

do

echo -n "please input a number: "

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"

break

;;

package\ name)

echo -n "please input package name: "

break

;;

*)

;;

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

;;

*)

;;

esac

done

\${RPM}     \$option \$argument

echo "continue? [yes/no]"

if [ answer = "no" ]

then

break

fi

done

0

sucre03