shell脚本基础知识梳理<一>

一、shell 脚本格式
#!/bin/bash
第一行是指定那个程序来编译执行脚本
注释是一“#”开头,

二、脚本执行 source、sh、bash、./执行脚本的区别
1、“. ”点命令,就是个点符号(从Bourne Shell而来)是source的另一名称
2、source 命令(从 C Shell 而来)执行bash shell的内置命令
3、bash /bin/bash命令功能要比sh强大
4、sh /bin/sh命令
5、export可新增,修改或删除环境变量,供后续执行的程序使用。同时,重要的一点是,export的效力仅及于该次登陆操作。注销或者重新开一个窗口,export命令给出的环境变量都不存在了。
export PATH=/bin/bash:$PATH
6、(点 source bash sh ./执行的文件名)他们之间的区别
6.1:点和source 执行方式是等价;即两种执行方式都是在当前shell进程中执行此脚本,而不是重新启动一个shell 而在子shell进程中执行此脚本。
6.2:bash sh (可以无执行权限)二者的执行文件不同
./ (必须有执行权限)三者执行方式是等价的;此三种执行脚本的方式都是重新启动一个子shell,在子shell中执行此脚本。
6.3: 验证结果:

[root@localhost ~]#name=dangxu //定义一般变量
[root@localhost ~]# echo ${name}
dangxu
[root@localhost ~]# cat test.sh //验证脚本,实例化标题中的./*.sh
#!/bin/sh
echo ${name}
[root@localhost ~]# ls -l test.sh //验证脚本可执行
-rwxr-xr-x 1 root root 23 Feb 6 11:09 test.sh
[root@localhost ~]# ./test.sh //以下三个命令证明了结论一
[root@localhost ~]# sh ./test.sh
[root@localhost ~]# bash ./test.sh
[root@localhost ~]# . ./test.sh //以下两个命令证明了结论二
dangxu
[root@localhost ~]# source ./test.sh
dangxu
[root@localhost ~]#

猜你喜欢

转载自blog.51cto.com/14294148/2434425