创建shell脚本文件时,必须在文件的第一行指明所使用的shell,格式如下:
#!/bin/bash
在普通的shell脚本行中,英镑符号 (#) 用作注释行。shell不处理shell脚本中的注释行。
但是shell脚本文件的第一行是个特例,#后面跟着感叹号告诉shell下面脚本的shell是 bash shell还是其他的shell脚本。
[root@hadoop ~]# ll /bin/bash
-rwxr-xr-x. 1 root root 964608 10月 31 2018 /bin/bash
指明shell后,将命令输入到文件中的每一行,后面跟回车,如前所述,使用#符号可以添加注释,在shell脚本中,shell将按照文件中命令的顺序处理命令,例如:
#!/bin/bash
# this is the first shell script
date
who
执行结果如下:
2019年 09月 05日 星期四 10:34:07 CST
root tty1 2019-09-05 10:23
root pts/0 2019-09-05 10:23 (gateway)
也可以在脚本中将两条命令一起运行,可以将它们放在同一行,并用分号将其隔开:”
#!/bin/bash
date;who
要引用当前目录中的文件,可以在shell中使用单点操作符:
[root@hadoop test_shell]# ./1.sh
2019年 09月 05日 星期四 10:44:36 CST
root tty1 2019-09-05 10:23
root pts/0 2019-09-05 10:23 (gateway)