Shell脚本
1>利用vi 建立一个脚本文件,该文件在用户输入年、月之后,自动打印数出该年该月的日历。然后以3种不同方式执行该脚本,如有不能执行情况,请说明原因。脚本代码:administrator@administrator-virtual-machine:~/LinuxShell$ cat -n printcal011#!/bin/bash23#利用vi 建
·
1>利用vi 建立一个脚本文件,该文件在用户输入年、月之后,自动打印数出该年该月的日历。然后以3种不同方式执行该脚本,如有不能执行情况,请说明原因。
脚本代码:
administrator@administrator-virtual-machine:~/LinuxShell$ cat -n printcal01
1 #!/bin/bash
2
3 #利用vi 建立一个脚本文件,该文件在用户输入年、月之后,自动打印数出该年该月的日历
4 echo "Please input a year."
5 read year
6 echo "Please input a month."
7 read month
8
9 echo "$year 年 $month 月的日历为:"
10 cal $month $year
测试:
方式一:
administrator@administrator-virtual-machine:~/LinuxShell$ PATH=$PATH:.
administrator@administrator-virtual-machine:~/LinuxShell$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:.
administrator@administrator-virtual-machine:~/LinuxShell$ chmod a+x printcal01
administrator@administrator-virtual-machine:~/LinuxShell$ printcal01
Please input a year.
2016
Please input a month.
10
2016 年 10 月的日历为:
十月 2016
日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
方式二:
administrator@administrator-virtual-machine:~/LinuxShell$ bash printcal01
Please input a year.
2000
Please input a month.
10
2000 年 10 月的日历为:
十月 2000
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
方式三:
administrator@administrator-virtual-machine:~/LinuxShell$ bash < printcal01
administrator@administrator-virtual-machine:~/LinuxShell$
没有达到脚本预期的结果
2>编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果两个单词相同则显示“Match”,并显示“End of program”,如果不同则显示“End of program”。
脚本代码:
administrator@administrator-virtual-machine:~/LinuxShell$ cat -n isequals01
1 #!/bin/bash
2
3 #提示用户输入两个单词,
4 #并将其读入,
5 #然后比较这两个字符串是否相等
6 #如果这两个字符串相等,输出“Match”
7 #并显示“End of the program”
8 #如果这两个字符串不想等,输出“End of the program”
9
10 #接受用户的输入
11 echo "Please input two words."
12 echo "Please input the first word."
13 read firstString
14 echo "Please input the second word."
15 read secondString
16
17 #比较这这两个字符串是否相同
18 if [[ $firstString = $secondString ]]
19 then echo "Match"
20 fi
21 echo "End of the program."
测试:
方式一:
administrator@administrator-virtual-machine:~/LinuxShell$ chmod a+x isequals01
administrator@administrator-virtual-machine:~/LinuxShell$ isequals01
Please input two words.
Please input the first word.
java
Please input the second word.
java
Match
End of the program.
administrator@administrator-virtual-machine:~/LinuxShell$ isequals01
Please input two words.
Please input the first word.
java
Please input the second word.
c
End of the program.
方式二:
administrator@administrator-virtual-machine:~/LinuxShell$ bash isequals01
Please input two words.
Please input the first word.
java
Please input the second word.
java
Match
End of the program.
administrator@administrator-virtual-machine:~/LinuxShell$ bash isequals01
Please input two words.
Please input the first word.
java
Please input the second word.
c
End of the program.
方式三:
administrator@administrator-virtual-machine:~/LinuxShell$ bash < isequals01
Please input two words.
Please input the first word.
End of the program.
administrator@administrator-virtual-machine:~/LinuxShell$ bash < isequals01
Please input two words.
Please input the first word.
End of the program.
没有达到脚本预期的结果:
3>修改上述程序,编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果两个单词相同显示“Match”,不同则显示“Not match”,最后显示“End of program”。
<编程提示>请使用 if…then…else 控制结构。
脚本代码:
administrator@administrator-virtual-machine:~/LinuxShell$ cat -n isequals02
1 #!/bin/bash
2
3 #提示用户输入两个单词,
4 #并将其读入,
5 #然后比较这两个字符串是否相等
6 #如果这两个字符串相等,输出“Match”
7 #如果这两个字符串不相等,输出“Not match”
8 #最后显示“End of program.”
9
10 #接受用户的输入
11 echo "Please input two words."
12 echo "Please input the first word."
13 read firstString
14 echo "Please input the second word."
15 read secondString
16
17 #比较这这两个字符串是否相同
18 if [[ $firstString = $secondString ]]
19 then echo "Match"
20 else
21 echo "Not Match"
22 fi
23 echo "End of the program."
测试:
方式一:
administrator@administrator-virtual-machine:~/LinuxShell$ chmod a+x isequals02
administrator@administrator-virtual-machine:~/LinuxShell$ isequals02
Please input two words.
Please input the first word.
java
Please input the second word.
java
Match
End of the program.
administrator@administrator-virtual-machine:~/LinuxShell$ isequals02
Please input two words.
Please input the first word.
java
Please input the second word.
c
Not Match
End of the program.
方式二:
administrator@administrator-virtual-machine:~/LinuxShell$ bash isequals02
Please input two words.
Please input the first word.
java
Please input the second word.
java
Match
End of the program.
administrator@administrator-virtual-machine:~/LinuxShell$ bash isequals02
Please input two words.
Please input the first word.
java
Please input the second word.
c
Not Match
End of the program.
方式三:
administrator@administrator-virtual-machine:~/LinuxShell$ bash < isequals02
Please input two words.
Please input the first word.
Not Match
End of the program.
administrator@administrator-virtual-machine:~/LinuxShell$ bash < isequals02
Please input two words.
Please input the first word.
Not Match
End of the program.
未能达到预期的脚本运行结果:
4>编程使用case结构创建一个简单的菜单,屏幕显示菜单:
a. Current date and time
b. User currently logged in
c. Name of the working directory
d. Contents of the working directory
Enter a,b,c or d:
根据用户输入选项做相应操作。
脚本代码:
administrator@administrator-virtual-machine:~/LinuxShell$ cat -n printmenu
1 #!/bin/bash
2 #author:lixinghai
3
4 #编程使用case结构创建一个简单的菜单,
5 #屏幕显示菜单
6 # MENU
7 #a.Current date and time
8 #b.User currently logged in
9 #c.Name of the working directory
10 #d.Contests of the working directory
11 #Enter a,b,c or d:
12
13 echo -e "\n MENU \n"
14 echo "a.Current date and time"
15 echo "b.User currently logged in"
16 echo "c.Name of the working directory"
17 echo "d.Constests of the working directory"
18 echo "Enter a,b,c or d:"
19
20 read choice
21 case $choice in
22 a)date;;
23 b)who;;
24 c)pwd;;
25 d)ls;;
26 esac
测试:
方式一:
administrator@administrator-virtual-machine:~/LinuxShell$ chmod a+x printmenu
administrator@administrator-virtual-machine:~/LinuxShell$ printmenu
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
Enter a,b,c or d:
a
2016年 11月 17日 星期四 17:48:45 CST
administrator@administrator-virtual-machine:~/LinuxShell$ printmenu
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
Enter a,b,c or d:
b
administrator :0 2016-11-17 17:32 (:0)
administrator pts/10 2016-11-17 17:32 (:0)
administrator@administrator-virtual-machine:~/LinuxShell$ printmenu
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
Enter a,b,c or d:
c
/home/administrator/LinuxShell
administrator@administrator-virtual-machine:~/LinuxShell$ printmenu
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
Enter a,b,c or d:
d
123 chmod01 doubleprocess01 frequentcommands m1.c printcal01
....(文件太多没有完全展示)
方式二:
administrator@administrator-virtual-machine:~/LinuxShell$ bash printmenu
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
Enter a,b,c or d:
a
2016年 11月 17日 星期四 17:51:57 CST
administrator@administrator-virtual-machine:~/LinuxShell$ bash printmenu
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
Enter a,b,c or d:
b
administrator :0 2016-11-17 17:32 (:0)
administrator pts/10 2016-11-17 17:32 (:0)
administrator@administrator-virtual-machine:~/LinuxShell$ bash printmenu
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
Enter a,b,c or d:
c
/home/administrator/LinuxShell
administrator@administrator-virtual-machine:~/LinuxShell$ bash printmenu
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
Enter a,b,c or d:
d
123 chmod01 doubleprocess01 frequentcommands m1.c printcal01 showmenu test touch01
方式三:
administrator@administrator-virtual-machine:~/LinuxShell$ bash < printmenu
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
Enter a,b,c or d:
bash: 行 21: 未预期的符号 `)' 附近有语法错误
bash: 行 21: ` a)date;;'
不能够正确的执行:
1>修改上题,使用户可以连续选择直到想退出时才退出。
脚本代码:
administrator@administrator-virtual-machine:~/LinuxShell$ cat -n printmenu01
1 #!/bin/bash
2 #author:lixinghai
3
4 #编程使用case结构创建一个简单的菜单,
5 #屏幕显示菜单
6 # MENU
7 #a.Current date and time
8 #b.User currently logged in
9 #c.Name of the working directory
10 #d.Contests of the working directory
11 #Enter a,b,c or d:
12 #使得用户可以连续选择,
13 #直到用户想退出才退出。
14
15 echo -e "\n MENU \n"
16 echo "a.Current date and time"
17 echo "b.User currently logged in"
18 echo "c.Name of the working directory"
19 echo "d.Constests of the working directory"
20 echo "e.exit the menu"
21 echo "Enter a,b,c,d or e:"
22 echo " "
23
24 while true
25 do
26 echo -n "Please input your choice:"
27 read choice
28 case $choice in
29 a)date;;
30 b)who;;
31 c)pwd;;
32 d)ls;;
33 e)break;;
34 esac
35 done
测试:
方式一:
administrator@administrator-virtual-machine:~/LinuxShell$ chmod a+x printmenu01
administrator@administrator-virtual-machine:~/LinuxShell$ printmenu01
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
e.exit the menu
Enter a,b,c,d or e:
Please input your choice:a
2016年 11月 17日 星期四 17:56:49 CST
Please input your choice:b
administrator :0 2016-11-17 17:32 (:0)
administrator pts/10 2016-11-17 17:32 (:0)
Please input your choice:c
/home/administrator/LinuxShell
Please input your choice:d
123 chmod01 doubleprocess01 frequentcommands m1.c printcal01 showmenu test touch01
(文件太多没有完全展示:)
Please input your choice:e
administrator@administrator-virtual-machine:~/LinuxShell$
方式二:
administrator@administrator-virtual-machine:~/LinuxShell$ bash printmenu01
MENU
a.Current date and time
b.User currently logged in
c.Name of the working directory
d.Constests of the working directory
e.exit the menu
Enter a,b,c,d or e:
Please input your choice:a
2016年 11月 17日 星期四 17:58:31 CST
Please input your choice:b
administrator :0 2016-11-17 17:32 (:0)
administrator pts/10 2016-11-17 17:32 (:0)
Please input your choice:c
/home/administrator/LinuxShell
Please input your choice:d
123 chmod01 doubleprocess01 frequentcommands m1.c printcal01 showmenu test touch01
1.c commandchar echo frequentcommands01 m2.c printcharacter singlechar test01 traversearray
2.c commandchar01 edit getsum man01 printf01 singlechar01 test02 until
3.c commandchar02 errfile groupcommand01 manwc printf02 singleprocess01 test03 userinfor
abc commands exe1 groupcommand02 menu printmenu source test04 varibles01
alias01 commandsAssist exe2 head01 mv01 printmenu01 source1 test05 varibles02
alias02 copy exe6 Hello.c mv02 privilege source2 test06 varibles03
alias03 copyandcat exist history01 mv03 read source3 test07 wc01
array cp01 explain history02 myfile reverse source5 testcase wc02
case cp02 fibolaqi history5 newfile rmdir01 specialcharacters01 testfile while
cat01 CResource fibolaqi01 isequals outappend rmdir02 specialcharacters02 testfor
cat02 cuts fibonacci isequals01 pdw01 rmdir03 stdin testfor01
cat03 DestDir fibonacci01 isequals02 position script stdout01 testuntil
cat11 different file line pr shellscript01 stdout02 testwhile
cd01 dir01 file.c logicalor prfile shellscript02 $student* testwhile01
cd02 dir02 find longicaland printarray shellscript03 systeminfor threeprocess01
chmod doublechars for longicalseq printcal shift01 tail01 tmp1
Please input your choice:e
administrator@administrator-virtual-machine:~/LinuxShell$
方式三:
administrator@administrator-virtual-machine:~/LinuxShell$ bash < printmenu01
your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your choice:Please input your
...(使用该方式出现死循环)
不能够正确的执行:
6.设计一个程序,从命令行接收数值参数,并计算这些参数的和。
脚本代码:
administrator@administrator-virtual-machine:~/LinuxShell$ cat -n getsum
1 #!/bin/bash
2
3 #设计一个程序,从命令行接受数值参数,并计算这些参数的和
4 sum=0
5 if [[ $1 -eq "" ]]
6 then echo "ERROR:parameters hanpped error"
7 echo "Please try again"
8 else
9 while [[ $1 -ne "" ]]
10 do
11 let "sum=sum+$1"
12 shift
13 done
14 echo "The sum of all the numbers is $sum "
15 fi
测试:
方式一:
administrator@administrator-virtual-machine:~/LinuxShell$ chmod a+x getsum
administrator@administrator-virtual-machine:~/LinuxShell$ getsum 1 2 3 4 5 6 7 8 9
The sum of all the numbers is 45
方式二:
administrator@administrator-virtual-machine:~/LinuxShell$ bash getsum 1 2 3 4 5 6 7 8 9
The sum of all the numbers is 45
方式三:
administrator@administrator-virtual-machine:~/LinuxShell$ bash < getsum
ERROR:parameters hanpped error
Please try again
不能够正确的执行:
。
总结:运行脚本有三种方式
1>将当前目录添加到环境变量中:PATH=$PATH:.
给脚本添加可执行的权限
直接在命令行输出脚本的名字,然后回车
在脚本正确的前提下,基本上都是可以成功运行的
2>使用bash 脚本的名字
在脚本正确的前提下,基本上都是可以成功运行的
3>使用bash < 脚本的名字
在脚本正确的前提下,如果需要从标准输入流接受用户的输入,都是不能直接执行成功的
更多推荐
已为社区贡献1条内容
所有评论(0)