Shell Script 概念与简单入门
如何运行这个文件?很简单,可以有底下几个方法:
直接命令下达: shell.sh 文件必须要具备可读与可运行 (rx) 的权限,然后:
- 绝对路径:使用 /home/dmtsai/shell.sh 来下达命令;
- 相对路径:假设工作目录在 /home/dmtsai/ ,则使用 ./shell.sh 来运行
- 变量『PATH』功能:将 shell.sh 放在 PATH 指定的目录内,例如: ~/bin/
以 bash 程序来运行:透过『 bash shell.sh 』或『 sh shell.sh 』来运行
script 的运行方式差异 (source, sh script, ./script)
利用直接运行的方式来运行 script
[root@www scripts]# echo $firstname $lastname <==确认了,这两个变量并不存在喔! [root@www scripts]# sh sh02.sh Please input your first name: VBird <==这个名字是鸟哥自己输入的 Please input your last name: Tsai Your full name is: VBird Tsai <==看吧!在 script 运行中,这两个变量有生效 [root@www scripts]# echo $firstname $lastname <==事实上,这两个变量在父程序的 bash 中还是不存在的!
利用 source 来运行脚本:在父程序中运行
[root@www scripts]# source sh02.sh Please input your first name: VBird Please input your last name: Tsai Your full name is: VBird Tsai [root@www scripts]# echo $firstname $lastname VBird Tsai <==嘿嘿!有数据产生喔!
- 区别:当子程序完成后,在子程序内的各项变量或动作将会结束而不会传回到父程序中