shell脚本实例
脚本实例
脚本实例
---
学习的捷径就是练习
:
1.在linux里面是不在乎后缀名的,但是建议写上后缀名,如test.sh,这样一眼便看出这
是shell程序。
2.如果不能运行,一般要执行chmod +x filename 使文件可执行
3.执行格式一般为./test.sh,为了安全起见。
4.写shell脚本时最好要建立良好的习惯。 在每个 script 的档头处记录好∶(练习的时
候免了吧) ?
注:鸟哥的shell用的是bash,不过建议写成 #!/bin/sh这样就可以使用系统默认版本的
shell,而不一定就是用bash。
在获取命令的运行结果中,鸟哥用的是`(不是单引号‘),建议用$(),更好一些。
# 请建立一支 script ,当你执行该 script 的时候,该 script 可以显示∶ 1. 你目前的身
份 (用 whoami ) 2. 你目前所在的目录 (用 pwd)
view plaincopy to clipboardprint?
1. #!/bin/bash
2. echo -e "Your name is ==> $(whoami)"
3. echo -e "The current directory is ==> `pwd`" #!/bin/bash
echo -e "Your name is ==> $(whoami)"
echo -e "The current directory is ==> `pwd`"
# 请自行建立一支程式,该程式可以用来计算『您还有几天可以过生日』啊??
view plaincopy to clipboardprint?
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 #!/bin/bash
read -p "Pleas input your birthday (MMDD, ex> 0709)
now=`date +%m%d`
if [ "$bir" == "$now" ]; then
echo "Happy Birthday to you!!!"
elif [ "$bir" -gt "$now" ]; then
year=`date +%Y`
total_d=$(($((`date --date="$year$bir" +%s`-`date +
echo "Your birthday will be $total_d later"
else
year=$((`date +%Y`+1))
total_d=$(($((`date --date="$year$bir" +%s`-`date +
echo "Your birthday will be $total_d later"
fi
# 让使用者输入一个数字,程式可以由 1+2+3... 一直累加到使用者输入的数字为止。
view plaincopy to clipboardprint?
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" #!/bin/bash
read -p "Please input an integer number: " number
i=0
s=0
while [ "$i" != "$number" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "the result of ‘1+2+3+...$number‘ is ==> $s"
# 撰写一支程式,他的作用是: 1.) 先查看一下 /root/test/logical 这个名称是否存在; 2.) 若
不存在,则建立一个档案,使用 touch 来建立,建立完成后离开; 3.) 如果存在的话,判
断该名称是否为档案,若为档案则将之删除后建立一个档案,档名为 logical ,之后离开;
4.) 如果存在的话,而且该名称为目录,则移除此目录! view plaincopy to clipboardprint?
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 #!/bin/bash
if [ ! -e logical ]; then
touch logical
echo "Just make a file logical"
exit 1
elif [ -e logical ] && [ -f logical ]; then
rm logical
mkdir logical
echo "remove file ==> logical"
echo "and make directory logical"
exit 1
elif [ -e logical ] && [ -d logical ]; then
rm -rf logical
echo "remove directory ==> logical"
exit 1
else
echo "Does here have anything?"
fi
# 我们知道 /etc/passwd 里面以 : 来分隔,第一栏为帐号名称。请写一苹程式,可以将
/etc/passwd 的第一栏取出,而且每一栏都以一行字串『The 1 account is "root" 』来显示,
那个 1 表示行数。
view plaincopy to clipboardprint?
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 #!/bin/bash
accounts=`cat /etc/passwd | cut -d‘:‘ -f1`
for account in $accounts
do
declare -i i=$i+1
echo "The $i account is /"$account/" "
done
来自
来自来自
来自:
::
:http://sucre.javaeye.com/blog/625918
1. 写一个脚本,利用循环计算10的阶乘 view plaincopy to clipboardprint?
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" #!/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"停止。 view plaincopy to clipboardprint?
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 #!/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整除的数之
和
view plaincopy to clipboardprint?
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" #!/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计算所有参数乘积,假设参数均为整数( 特殊变量$# 表
示包含参数的个数) view plaincopy to clipboardprint?
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 #! /bin/sh
result=1
while [ $# -gt 0 ]
do
result=`expr $result /* $1`
shift
done
echo $resul 5.写一个脚本,可以根据参数文件名,以正确的参数调用tar来解压缩tar.gz或tar.bz2文件。 view plaincopy to clipboardprint?
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 #!/bin/sh
case ${1##*.tar.} in
bz2)
tar jxvf $1
;;
gz)
tar zxvf $1
;;
*)
echo "wrong file type"
esac 6.写一个脚本以方便用户查询rpm的相关信息。这个脚本首先提示用户选择查询依据,比如
文件名,包名,全部等。然后提示用户选择查询信息,比如包名,包里所包含的所有文件,
包的信息等。然后询问是否继续查询,是则循环刚才的过 程,否则退出。 view plaincopy to clipboardprint?
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
本文出自 “江湖笑笑生” 博客,请务必保留此出处http://xuexuhui.blog.51cto.com/9647696/1662963
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。