`
youyu4
  • 浏览: 424886 次
社区版块
存档分类
最新评论

关于#!/bin/bash和#!/bin/sh

 
阅读更多
#!/bin/bash是指此脚本使用/bin/bash来解释执行。
其中,#!是一个特殊的表示符,其后,跟着解释此脚本的shell路径。
bash只是shell的一种,还有很多其它shell,如:sh,csh,ksh,tcsh,...
我们可以通过以下一个示例来进行实验,了解#!/bin/bash的使用。
除第一行外,脚本中所有以“#”开头的行都是注释。
1)#!/bin/bash只能放在第一行,如果后面还有#!,那么只能看成是注释。
这里有三个脚本(脚本都要使用”chmod +x scriptname“命令来获得可执行权限):
tbash1.sh:
#!/bin/sh
source abc
echo "hello abc"
 
tbash2.sh:
#!/bin/bash
source abc
echo "hello abc"
 
tbash3.sh:
source abc
echo "hello abc"
 
三个脚本执行的结果:
[nsvc@localhost other]$ ./tbash1.sh 
./tbash1.sh: line 2: abc: No such file or directory
注:当source命令执行有问题时,sh不再往下面执行。
[nsvc@localhost other]$ ./tbash2.sh 
./tbash2.sh: line 2: abc: No such file or directory
hello abc
注:当source命令执行有问题时,bash继续执行下面命令。
[nsvc@localhost other]$ ./tbash3.sh 
./tbash3.sh: line 1: abc: No such file or directory
hello abc
注:自身登录系统所在的shell是bash。所以,当source命令执行有问题时,bash继续执行下面命令。
 
如果将tbash1.sh改成:
echo "abc"
#!/bin/sh
source abc
echo "hello abc"
那么,执行结果是:
[nsvc@localhost other]$ ./tbash1.sh 
abc
./tbash1.sh: line 3: abc: No such file or directory
hello abc
也就是说,脚本忽略了第二行“#!/bin/sh",直接使用当前所在的shell(也就是bash)来解释脚本。
 
当把tbash1.sh改成:
#!/bin/sh
#!/bin/bash
source abc
echo "hello abc"
执行结果为:
[nsvc@localhost other]$ ./tbash1.sh 
./tbash1.sh: line 3: abc: No such file or directory
当执行完source命令时,并没有往下执行。说明,#!/bin/sh这一行起到作用了,但#!/bin/bash并没有起作用。在脚本中,除第一行外,脚本中所有以“#”开头的行都是注释。
 
2)#!后面的路径一定要正确,不正确会报错。
假如,我们把tbash1.sh中第一行的#!后面加了一个不存在的路径”/home/sh“:
#!/home/sh
source abc
echo "hello abc"
执行结果为:
[nsvc@localhost other]$ ./tbash1.sh 
-bash: ./tbash1.sh: /home/sh: bad interpreter: No such file ordirectory
系统会提示/home/sh的路径不存在。
 
3)如果一个脚本在第一行没有加上#!+shell路径这一行,那么,脚本会默认当前用户登录的shell,为脚本解释器。
在1)中,脚本tbash3.sh的执行结果,就是用当前自己登录的shell(bash)解释后的结果。我们通常所用的shell都是bash,如果哪天登录到sh,再使用以上类型的脚本,就会有问题。以下是自己登录到sh下,执行tbash3.sh的结果:
-sh-3.2$ ./tbash3.sh 
./tbash3.sh: line 1: abc: 没有那个文件或目录
与1)中的执行结果是不一样的。
因此,大家应该养成脚本首行加上#!+shell路径的习惯。
 
4)/bin/sh相当于/bin/bash --posix
我们将脚本tbash1.sh改为:
#!/bin/bash --posix
source abc
echo "hello abc"
执行结果:
[nsvc@localhost other]$ ./tbash1.sh 
./tbash1.sh: line 2: abc: No such file or directory
与tbash1.sh原脚本执行的结果一样。
 
我们还可以以tbash3.sh为示例。
用以下命令来执行该脚本:
[nsvc@localhost other]$ bash tbash3.sh
tbash3.sh: line 1: abc: No such file or directory
hello abc
[nsvc@localhost other]$ sh tbash3.sh 
tbash3.sh: line 1: abc: No such file or directory
[nsvc@localhost other]$ bash --posix tbash3.sh 
tbash3.sh: line 1: abc: No such file or directory
 "bash tbash3.sh"表示使用bash来作为脚本解释器来执行tbash3.sh。同样,也可以使用如”sh脚本名“这样的命令,来用sh作为脚本解释器。
从结果可以看出,/bin/bash--posix与/bin/sh的执行结果相同。总结起来,sh跟bash的区别,实际上是bash有没开启posix模式的区别。遵守posix规范,可能包括,”当某行代码出错时,不继续往下执行。“
 
最后加上一点说明,每个脚本开头都使用"#!",#!实际上是一个2字节魔法数字,这是指定一个文件类型的特殊标记,在这种情况下,指的就是一个可执行的脚本。在#!之后,接一个路径名,这个路径名指定了一个解释脚本命令的程序,这个程序可以是shell,程序语言或者任意一个通用程序。
分享到:
评论

相关推荐

    BCM4322在CentOS 7驱动程序

    CentOS7.4下编译测试通过。改进了程序并增加了安装说明。 # cat README 1、以root身份执行如下语句.../bin/bash for M in lib80211 cfg80211 wl; do modprobe $M &>/dev/null done 这样,驱动就会开机启动啦!

    ubuntu如何运行.sh文件,.sh文件运行提示脚本异常

    ubuntu执行.sh文件,1 .sh文件是Ubuntu下可执行文件,首先需要确定它的权限是否是可执行的,2、 用vim打开sh文件,3、查看文件格式:4. 修改文件格式: 5. 保存退出: 6. 执行:./filename .../bin/bash

    bash基础用法.docx

    /bin/bash 脚本声明 脚本的概念 将要执行的命令按照顺序保存到一个文本中 给文件相应的权限即可执行 结合控制语句可以完成复杂操作 脚本的应用场景 重复 批量事务处理 自动化运维 定期任务执行 服务器运行监控...

    02-SHELL编程之流程控制和循环语句

    /bin/bash # Name:ping.sh # Path:/shell02/ # Usage:/shell02/ping.sh # ... #获取远程主机的IP地址(定义变量让用户自己输入) read -p "请输入你要ping的远程主机IP:" IP #使用ping命令来判断是否和远程主机互通...

    解析PyCharm Python运行权限问题

    /bin/bash sudo /usr/bin/python "$@" 给到其执行权限: chmod +x ~/bin/python-sudo.sh 第二步 让当前用户执行 sudo python 不需要输入密码执行: sudo visudo -f /etc/sudoers.d/python 此时会自动创建 /etc/

    kafka集群监控之KafkaOffsetMonitor 0.4.6版本地化(实测)

    /bin/bash java -Xms512M -Xmx512M -Xss1024K -XX:PermSize=256m -XX:MaxPermSize=512m \ -cp KafkaOffsetMonitor-assembly-0.4.6-zjz.jar \ com.quantifind.kafka.offsetapp.OffsetGetterWeb \ --offsetStorage ...

    从数据库中取变量传递给shell,生成环境变量

    /bin/bash' from dual union all select 'export my_shell_para='||to_char(count(*)) from yourtable where create_time ='2012.06.09'; spool off exit 2. 主脚本,连续数据库,执行myshellpara.sql #!/bin/...

    shell脚本:一键配置本地yum源仓库

    /bin/bash #date:2020-2-20 #描述:一键配置本地yum源仓库 echo [正在检测是否挂载磁盘----------] mount /dev/cdrom /mnt &> /dev/null cd /mnt/Packages/ if [ $? -eq 0 ] then echo 已将系统安装光盘挂载至/...

    在服务器跑python定时任务

    1、Python自带模块 .../bin/bash #这里python3要写绝对路径 sudo python3 /root/timetask/timetask.py \r echo $(date +%Y-%m-%d %H:%M:%S) '执行完毕' exit 0 给权限 不然跑不了 原创文章 4获赞 1访问量 3359

    VOS30002120 Watchdong Service

    /bin/bash while true do /etc/voscheck.sh sleep 1 done ----------------------------------- Press Esc button and write :wq Save this above file chmod 755 check5.sh cd /etc vi mbx3000.sh (Press i and...

    实际生产xtrbackup 备份脚本

    /bin/bash # 00 20 * * * /backups/xtrabackup/script/xtrbackup_script.sh type xtrabackup >/dev/null 2>&1 || { echo >&2 "I require xtrabackup but it's not installed. Aborting."; exit 1; } ### 变量定义 ...

    Shell脚本部署Prometheus+Grafana+Alertmanager

    一、部署包下载地址 1.百度网盘 ...提取码:to74 .../bin/bash #AUTHOR:anqixiang #DATE:2020-02-15 #MODIFY: #FUNCTION:部署Prometheus+Grafana+Alertmanager #VERSION:Prometheus-V2.15 Grafana-V5.3.

    解决-BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: 权限不够问题

    以上所述是小编给大家介绍的解决-BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: 权限不够问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对软件开发网网站的支持! ...

    kaldi例子学习

    /bin/bash # Copyright Ondrej Platek Apache 2.0 renice 20 $$ # Load training parameters . ./env_voip_cs.sh ;;设置环境变量,各种参数文件夹名称== # Source optional config if exists [ -f env_voip_cs_...

    使用tar备份linux

    /bin/bash # NAME : backup.sh # 2006-03-24 Ver 1.0 Osmond # # 请根据您的需要修改如下变量的值 BACKUPDIR=/backups/dat # 备份文件存放的目录 BACKUPLISTDIR=/backups/list # 要备份目录的文件列表 BACKUPLOGDIR=...

    shell注意事项不建议下载

    /bin/sh将调用默认的命令解释器,这在Linux系统上是/bin/bash. 运行脚本: chmod 555 scriptname (使每个人都有读和执行的权限) [6] 也可以: chmod +rx scriptname (使每个人都有读和执行的权限) chmod u+rx script...

    离线Shell脚本自动化部署Fastdfs

    /bin/bash #关闭防火墙 systemctl stop firewalld.service setenforce 0 #安装编译环境 yum install -y git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget...

    输出执行操作和打印日志的shell脚本实例

    /bin/bash #log function ####log_correct函数打印正确的输出到日志文件 function log_correct () { DATE=`date “+%Y-%m-%d %H:%M:%S”` ####显示打印日志的时间 USER=$(whoami) ####那个用户在操作 echo “${DATE...

    Linux/Unix shell 脚本监控磁盘可用空间

    Linux下监控磁盘的空闲空间的shell脚本,对于系统管理员或DBA来说,必不可少。.../bin/bash # ——————————————————————————+ # CHECK FILE SYSYTEM SPACE BY THRESHOLD | # Filename:

    Shell脚本实现批量添加用户

    /bin/bash for name in tom joy john mark james do useradd $name  echo “redhat” | passwd –stdin $name done 好了,执行结束后会自动新建用户tom、joy、john 、mark、james,并且密码都设置为“redhat”。 ...

Global site tag (gtag.js) - Google Analytics