shell脚本实例.docx
《shell脚本实例.docx》由会员分享,可在线阅读,更多相关《shell脚本实例.docx(13页珍藏版)》请在冰点文库上搜索。
shell脚本实例
shell脚本实例---学习的捷径就是练习
写在前面:
1.在linux里面是不在乎后缀名的,但是建议写上后缀名,如test.sh,这样一眼便看出这是shell程序。
2.如果不能运行,一般要执行chmod+xfilename使文件可执行
3.执行格式一般为./test.sh,为了安全起见。
4.写shell脚本时最好要建立良好的习惯。
在每个script的档头处记录好∶(练习的时候免了吧)
∙ script的功能;
∙script的版本资讯;
∙script的作者与联络方式;
∙script的版权宣告方式;
∙script的History(历史纪录);
∙script内较特殊的指令,使用绝对路径的方式来下达;
∙script运作时需要的环境变数预先宣告与设定。
来自:
鸟哥的私房菜
注:
鸟哥的shell用的是bash,不过建议写成#!
/bin/sh这样就可以使用系统默认版本的shell,而不一定就是用bash。
在获取命令的运行结果中,鸟哥用的是`(不是单引号'),建议用$(),更好一些。
#请建立一支script,当你执行该script的时候,该script可以显示∶1.你目前的身份(用whoami)2.你目前所在的目录(用pwd)
viewplaincopytoclipboardprint?
1.#!
/bin/bash
2.echo -e "Your name is ==> $(whoami)"
3.echo -e "The current directory is ==> `pwd`"
#请自行建立一支程式,该程式可以用来计算『您还有几天可以过生日』啊?
?
viewplaincopytoclipboardprint?
1.#!
/bin/bash
2.read -p "Pleas input your birthday (MMDD, ex> 0709):
" bir
3.now=`date +%m%d`
4.if [ "$bir" == "$now" ]; then
5.echo "Happy Birthday to you!
!
!
"
6.elif [ "$bir" -gt "$now" ]; then
7.year=`date +%Y`
8.total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
9.echo "Your birthday will be $total_d later"
10.else
11.year=$((`date +%Y`+1))
12.total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
13.echo "Your birthday will be $total_d later"
14.fi
#让使用者输入一个数字,程式可以由1+2+3...一直累加到使用者输入的数字为止。
viewplaincopytoclipboardprint?
1.#!
/bin/bash
2.read -p "Please input an integer number:
" number
3.i=0
4.s=0
5.while [ "$i" !
= "$number" ]
6.do
7.i=$(($i+1))
8.s=$(($s+$i))
9.done
10.echo "the result of '1+2+3+...$number' is ==> $s"
#撰写一支程式,他的作用是:
1.)先查看一下/root/test/logical这个名称是否存在;2.)若不存在,则建立一个档案,使用touch来建立,建立完成后离开;3.)如果存在的话,判断该名称是否为档案,若为档案则将之删除后建立一个档案,档名为logical,之后离开;4.)如果存在的话,而且该名称为目录,则移除此目录!
viewplaincopytoclipboardprint?
1.#!
/bin/bash
2.if [ !
-e logical ]; then
3.touch logical
4.echo "Just make a file logical"
5.exit 1
6.elif [ -e logical ] && [ -f logical ]; then
7.rm logical
8.mkdir logical
9.echo "remove file ==> logical"
10.echo "and make directory logical"
11.exit 1
12.elif [ -e logical ] && [ -d logical ]; then
13.rm -rf logical
14.echo "remove directory ==> logical"
15.exit 1
16.else
17.echo "Does here have anything?
"
18.fi
#我们知道/etc/passwd里面以:
来分隔,第一栏为帐号名称。
请写一苹程式,可以将/etc/passwd的第一栏取出,而且每一栏都以一行字串『The1accountis"root"』来显示,那个1表示行数。
viewplaincopytoclipboardprint?
1.#!
/bin/bash
2.accounts=`cat /etc/passwd | cut -d':
' -f1`
3.for account in $accounts
4.do
5.declare -i i=$i+1
6.echo "The $i account is /"$account/" "
7.done
来自:
1.写一个脚本,利用循环计算10的阶乘
viewplaincopytoclipboardprint?
1.#!
/bin/sh
2.
3.factorial=1
4.
5.for a in `seq 1 10`
6.do
7. factorial=`expr $factorial /* $a`
8.done
9.
10.echo "10!
= $factorial"
注:
上面有一行,forain`seq110`,其中seq110,即列出现1到10之间所有的数字,这一行也可改为:
forain"12345678910"
2.写一个脚本,执行后,打印一行提示“Pleaseinputanumber:
",要求用户输入数值,然后打印出该数值,
然后再次要求用户输入数值。
直到用户输入"end"停止。
viewplaincopytoclipboardprint?
1.#!
/bin/sh
2.
3.unset var
4.
5.while [ "$var" !
= "end" ]
6.do
7. echo -n "please input a number:
"
8. read var
9. if [ "$var" = "end" ]
10. then
11. break
12. fi
13. echo "var is $var"
14.done
3.写一个脚本,利用循环和continue关键字,计算100以内能被3整除的数之和
viewplaincopytoclipboardprint?
1.#!
/bin/sh
2.sum=0
3.for a in `seq 1 100`
4.do
5. if [ `expr $a % 3` -ne 0 ]
6. then
7. continue
8. fi
9. echo $a
10. sum=`expr $sum + $a`
11.done
12.echo "sum = $sum"
4.一个函数,利用shift计算所有参数乘积,假设参数均为整数(特殊变量$#表示包含参数的个数)
viewplaincopytoclipboardprint?
1.#!
/bin/sh
2.
3.result=1
4.while [ $# -gt 0 ]
5.do
6. result=`expr $result /* $1`
7. shift
8.done
9.echo $resul
5.写一个脚本,可以根据参数文件名,以正确的参数调用tar来解压缩tar.gz或tar.bz2文件。
viewplaincopytoclipboardprint?
1.#!
/bin/sh
2.
3.case ${1##*.tar.} in
4. bz2)
5. tar jxvf $1
6. ;;
7. gz)
8. tar zxvf $1
9. ;;
10. *)
11. echo "wrong file type"
12.esac
6.写一个脚本以方便用户查询rpm的相关信息。
这个脚本首先提示用户选择查询依据,比如
文件名,包名,全部等。
然后提示用户选择查询信息,比如包名,包里所包含的所有文件,
包的信息等。
然后询问是否继续查询,是则循环刚才的过程,否则退出。
viewplaincopytoclipboardprint?
1.#!
/bin/sh
2.RPM=/bin/rpm
3.option="-q"
4.
5.while true
6.do
7. echo "what to query?
"
8. select var in "All" "file" "package name"
9. do
10. case $var in
11. All)
12. option=$option"a"
13. break
14. ;;
15. file)
16. echo -n "please input file name:
"
17. option=$option"f"
18. read argument
19. break
20. ;;
21. package/ name)
22. echo -n "please input package name:
"
23. read argument
24. break
25. ;;
26. *)
27. echo "please choose between 1-3"
28. ;;
29. esac
30. done
31.
32. echo "what do you want to know?
"
33. select var in "location" "info" "package name"
34. do
35. case $var in
36. location)
37. option=$option"l"
38. break
39. ;;
40. info)
41. option=$option"i"
42. break
43. ;;
44. package/ name)
45. break
46. ;;
47. *)
48. echo "please choose between 1-3"
49. ;;
50. esac
51. done
52.
53. ${RPM} $option $argument
54.
55. echo "continue?
[yes/no]"
56. read answer
57.
58. if [ answer = "no" ]
59. then
60. break
61. fi
62.done