菜鸟的《Linux程序设计》学习—shell script
1. 认识shell script
2. shell script程序编写
[yr@localhost shellscript]$ vim helloworld.sh //在打开的文本中,编辑程序,helloworld.sh #!/bin/bash #Program: # This is my first shell script program. It will show "Hello World!" on # the screen. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo -e "Hello World! \a \n" exit 0程序内容解析:
3. 程序的编译运行
[yr@localhost shellscript]$ sh helloworld.sh Hello World!
(2)使用chmod改变文件权限,直接用./helloworld.sh 执行
[yr@localhost shellscript]$ chmod +x helloworld.sh [yr@localhost shellscript]$ ./helloworld.sh Hello World!
我们看到,以上两种方式,都可以顺利成功的将脚本和helloworld.sh执行完毕。
4. 两种编译方式详解
[yr@localhost shellscript]$ vim test.sh //下面是程序内容 #!/bin/bash #Program # User inputs his first name and last name. Program shows his full name. #History: # 2015/05/14 shine_yr First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:usr/local/sbin:~/bin export PATH read -p "Please input your first name: "firstname #提示用户输入 read -p "Please input your last name: "lastname #提示用户输入 echo -e "\nYour full name is: $firstname $lastname" #结果在屏幕输出 exit 0首先,利用直接执行的方式来执行脚本:
[yr@localhost shellscript]$ sh test.sh Please input your first name: shine Please input your last name: yr Your full name is: shine yr [yr@localhost shellscript]$ echo $firstname [yr@localhost shellscript]$ echo $lastname [yr@localhost shellscript]$从上面可以看出,程序顺利执行,然后我利用echo命令打算输出firstname以及lastname的内容,结果却输出为空。
然后,使用source的方式执行脚本:
[yr@localhost shellscript]$ source test.sh Please input your first name: shine Please input your last name: yr Your full name is: shine yr [yr@localhost shellscript]$ echo $firstname shine [yr@localhost shellscript]$ echo $lastname yr [yr@localhost shellscript]$我们可以看到,此时变量firstname以及lastname 中是有确切值的。
5. 编写shell script的良好习惯
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。