Shell-/bin/bash和/bin/sh解释器的误用引起的脚本语法错误

简介: Shell-/bin/bash和/bin/sh解释器的误用引起的脚本语法错误

20191206203234427.png

生猛干货


从系统安装到程序员必备的Linux技能,还原真实工作场景,手把手带你实战演练

20210304160434749.png

背景

下面的脚本,在Linux上运行良好,在SUNOS执行的时候报语法错误。

#! /bin/sh
#支持fwu的使用fwu 不支持的使用fu
PS_TYPE="ps -fwu"
do_ps=`ps -fwu 2>/dev/null`
if [ "$?" -eq 1 ]
then
  PS_TYPE="ps -fu"
fi
OSTYPE=`uname -a | awk '{print substr($0,1,3)}'`
SELF_PATH=$(cd `dirname $0`; pwd)
#SELF_PATH=`dirname $0`
SELF_NAME=`basename $0`


其实就是获取脚本的当前文件路径。

同样的一段shell脚本,在 Linux主机上运行良好, 但是在SUNOS上 却执行报错了

syntax error at line 12: `SELF_PATH=$' unexpected


问题分析

于是把这行脚本单独拿出来单独执行,但OK。


aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcwNjA5MDMwOTQ4ODc3.png

一番折腾之后,是脚本解释器的问题.

查看主机的SHELL解释器类型

ocsdb02:[/oracle$]echo $SHELL
/bin/bash
ocsdb02:[/oracle$]



解决办法


将 第一行的 #! /bin/sh 调整为 #!/bin/bash ,重新执行OK了。

事实上 SUOS主机上的sh的软连接的配置:

aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcwNjA5MDMzMjUzNTk1.png

LINUX主机上的 sh的软连接配置 (sh一般设成bash的软链)

aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcwNjA5MDMzMTEyMTcy.png


所以才会在Linux上运行OK,在sunos上执行语法错误, sh解释器不支持bash下的一些操作

第二种方法 是修改主机的默认SHELL,即修改软连接为BASH。


知识点回顾


Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incorporates useful features from the Korn and C shells (ksh and csh).


Linux中的shell有多种类型,其中最常用的几种是Bourne shell(sh)、C shell(csh)和Korn shell(ksh)。


Bourne shell在shell编程方面相当优秀,但在处理与用户的交互方面做得不如其他几种shell。


Linux操作系统缺省的shell是Bourne Again shell,它是Bourne shell的扩展,简称Bash,与Bourne shell完全向后兼容,并且在Bourne shell的基础上增加、增强了很多特性。Bash放在/bin/bash中,它有许多特色,可以提供如命令补全、命令编辑和命令历史表等功能,它还包含了很多C shell和Korn shell中的优点,有灵活和强大的编程接口,同时又有很友好的用户界面。


GNU/Linux 操作系统中的 /bin/sh 是 bash(Bourne-Again Shell)的符号链接,但鉴于 bash 过于复杂,有人把 ash 从 NetBSD 移植到 Linux 并更名为 dash(Debian Almquist Shell) https://wiki.ubuntu.com/DashAsBinSh ,并建议将 /bin/sh 指向它,以获得更快的脚本执行速度。


What you should use when writing scripts


If your script requires features only supported by bash, use #!/bin/bash.


But if at all possible, it would be good to make sure your script is POSIX-compatible, and use #!/bin/sh, which should always, quite reliably, point to the preferred POSIX-compatible system shell in any installation.


参考资料:

https://man.cx/bash


搞定Linux核心技术


https://artisan.blog.csdn.net/article/details/72952632?spm=1001.2014.3001.5502


相关文章
|
8月前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
178 0
Linux Bash 脚本中的 IFS 是什么?
|
3月前
|
Devops 关系型数据库 大数据
1000个开源免费的bash脚本合集
【10月更文挑战第4天】
|
4月前
|
Shell
Shell脚本有哪些基本语法?
【9月更文挑战第4天】
90 18
|
6月前
|
Java Shell Linux
使用 sh -x 进行 shell 脚本调试
使用 sh -x 进行 shell 脚本调试
137 9
使用 sh -x 进行 shell 脚本调试
|
5月前
|
Shell
一个能够生成 Markdown 表格的 Bash 脚本
【8月更文挑战第20天】这是一个使用Bash脚本生成Markdown表格的示例。脚本首先设置表头与内容数据,然后输出Markdown格式的表格。用户可以根据需要自定义表格内容。使用时,只需将脚本保存为文件(如 `generate_table.sh`),赋予执行权限,并运行它,即可在终端看到生成的Markdown表格。
|
5月前
|
缓存 Shell Linux
在Linux中,bash shell 中的 hash 命令有什么作用?
在Linux中,bash shell 中的 hash 命令有什么作用?
|
5月前
|
Unix Shell Linux
在Linux中,什么是Bash脚本,并且如何使用它。
在Linux中,什么是Bash脚本,并且如何使用它。
|
5月前
|
Shell 开发者
深入理解Bash脚本中的函数
【8月更文挑战第20天】
105 0
|
5月前
|
存储 Shell 数据处理
深入探讨Bash脚本中的数组
【8月更文挑战第20天】
47 0
|
5月前
|
存储 Shell
Bash 脚本中的 `hash` 命令
【8月更文挑战第19天】
55 0