admin管理员组

文章数量:1530518

2024年7月16日发(作者:)

shell脚本实例---学习的捷径就是练习

写在前面:

1.在linux里面是不在乎后缀名的,但是建议写上后缀名,如,这样一眼便看

出这是shell程序。

2.如果不能运行,一般要执行chmod +x filename 使文件可执行

3.执行格式一般为./,为了安全起见。

4.写shell脚本时最好要建立良好的习惯。 在每个 script 的档头处记录好∶(练习的

时候免了吧)

script 的功能;

script 的版本资讯;

script 的作者与联络方式;

script 的版权宣告方式;

script 的 History (历史纪录);

script 内较特殊的指令,使用绝对路径的方式来下达;

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+ 一直累加到使用者输入的数字为止。

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

来自:/blog/625918

1. 写一个脚本,利用循环计算10的阶乘

view plaincopy to clipboardprint?

1. #!/bin/sh

2. factorial=1

3. for a in `seq 1 10`

4. do

5. factorial=`expr $factorial /* $a`

6. done

7. 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. unset var

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

4. do

5. echo -n "please input a number: "

6. read var

7. if [ "$var" = "end" ]

8. then

9. break

10. fi

11. echo "var is $var"

12. 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. result=1

3. while [ $# -gt 0 ]

4. do

5. result=`expr $result /* $1`

6. shift

7. done

8. echo $resul

#! /bin/sh

result=1

while [ $# -gt 0 ]

do

result=`expr $result /* $1`

shift

done

echo $resul

5.写一个脚本,可以根据参数文件名,以正确的参数调用tar来解压缩或

2文件。

view plaincopy to clipboardprint?

1. #!/bin/sh

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

3. bz2)

4. tar jxvf $1

5. ;;

6. gz)

7. tar zxvf $1

8. ;;

9. *)

10. echo "wrong file type"

11. 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. while true

5. do

6. echo "what to query?"

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

8. do

9. case $var in

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

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

23. break

24. ;;

25. *)

26. echo "please choose between 1-3"

27. ;;

28. esac

29. done

30. echo "what do you want to know?"

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

32. do

33. case $var in

34. location)

35. option=$option"l"

36.

37.

38.

39.

40.

41.

42.

43.

44.

45.

46.

47.

48.

break

;;

info)

option=$option"i"

break

;;

package/ name)

break

;;

*)

echo "please choose between 1-3"

;;

esac

49. done

50. ${RPM} $option $argument

51. echo "continue? [yes/no]"

52. read answer

53. if [ answer = "no" ]

54. then

55. break

56. fi

57. done

本文标签: 脚本用户建立输入查询