shell学习第一天 Shell脚本开发简单规范
"#!/bin/bash"
“#!”称为幻数,在执行Bash脚本的时候,内核会根据它来确定该用哪个程序来解释Shell脚本中的内容。这一行必须在脚本顶端的第一行,如果不是第一行则为注释。
vi one.sh
#!/bin/bash
echo “this is first day”
#!/bin/bash <==如果写到这里则为注释
有时在不同的shell脚本里面看到会有bash 以及sh 两种方式。这两种的区别是什么?
sh是bash的一个软链接
1 2 |
<strong>[root@localhost ~]# ll /bin/sh lrwxrwxrwx. 1 root root 4 3月 17 21:56 /bin/sh –> bash</strong> |
标准的写法为 “#!bin/bash”
一般的写法为sh,因为书写较为简单。
下面是Linux中常用脚本语言开头的编码写法,不同语言脚本的开头一般都要加上如下相应语言的开头标识内容。
1、#!/bin/sh
2、#!/bin/bash
3、#!/usr/bin/awk
4、#!/bin/sed
5、#!/usr/bin/tcl
6、#!/usr/bin/expect
7、#!/usr/bin/perl
“#”后面的为注释,可自成一行,也可在代码后面加:
例:
vi one.sh
echo “hello world” #这里是注释
#这里也可以是注释
使用脚本的方法:
1、bash script-name 或 sh script-name(推荐使用的方式)
2、path /script-name 或 ./script-name(在当前路径下执行脚本)
3、source script-name 或 . script-name #注意“.”号以及后面的空格
当shell脚本文件没有可执行权限时,应使用第1和第3种方式
只有当shell脚本有可执行权限时,才可以使用第2种方式
而第1种和第3种方法的区别在于:
第一种shell执行方式并不将变量应用于父shell 而第三种可以
例:
1 2 3 4 5 6 7 8 |
<strong>[root@localhost shell]# </strong><strong>vi mm.sh</strong> #!/bin/bash user=”whoami” <strong>[root@localhost shell]# sh mm.sh [root@localhost shell]# echo $user </strong> <strong>[root@localhost shell]# source mm.sh [root@localhost shell]# echo $user whoami</strong> |
版权声明:
作者:龙魂
链接:https://blog.wlzs.cn/shell%e5%ad%a6%e4%b9%a0%e7%ac%ac%e4%b8%80%e5%a4%a9/
来源:学海无涯
文章版权归作者所有,未经允许请勿转载。

共有 0 条评论