[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程序内容解析:
[yr@localhost shellscript]$ sh helloworld.sh Hello World!
[yr@localhost shellscript]$ chmod +x helloworld.sh [yr@localhost shellscript]$ ./helloworld.sh Hello World!
[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的内容,结果却输出为空。
[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 中是有确切值的。
原文地址:http://blog.csdn.net/fly_yr/article/details/45722429