[other] 问一下shell脚本中的{}展开问题
Tofloor
poster avatar
weijiahao
deepin
2023-02-02 05:39
Author

使用deepin写shell脚本,执行后得到非期望的结果,内容及执行结果如下:

截图_选择区域_20230201212906.png

但是在centos中,{}会正常展开,得到期望的结果:

截图_选择区域_20230201213918.png

请问下如何让deepin中的脚本得出和cenots脚本正确的结果

Reply Favorite View the author
All Replies
蔡EEPIN
deepin
2023-02-02 06:11
#1

for i in $(seq 1 10);do echo $i;done 可以用这种

Reply View the author
lanxing0821
deepin
2023-02-02 06:23
#2

这是因为底层 shell 不同。在 Debian/Ubuntu 系统中,sh 指向 dash ,而不是 bash 。下面的命令可以查询你的系统底层 shell :

ls -l /bin/sh

dash 和 bash 在语法上有些差别,比如 bash 支持大括号扩展,而 dash 不支持。

dash -c 'echo {0..9}'        # 照原样输出
bash -c 'echo {0..9}'        # 将数字展开

有关 dash 和 bash 的更多差别,参见 https://wiki.ubuntu.com/DashAsBinSh

Reply View the author
weijiahao
deepin
2023-02-02 06:38
#3
lanxing0821

这是因为底层 shell 不同。在 Debian/Ubuntu 系统中,sh 指向 dash ,而不是 bash 。下面的命令可以查询你的系统底层 shell :

ls -l /bin/sh

dash 和 bash 在语法上有些差别,比如 bash 支持大括号扩展,而 dash 不支持。

dash -c 'echo {0..9}'        # 照原样输出
bash -c 'echo {0..9}'        # 将数字展开

有关 dash 和 bash 的更多差别,参见 https://wiki.ubuntu.com/DashAsBinSh

很高兴收到您的回复,经过提醒,发现/bin/sh确实软连接指向dash

截图_选择区域_20230201223547.png

已成功解决此问题,再次感谢您的解答

Reply View the author
weijiahao
deepin
2023-02-02 06:40
#4
蔡EEPIN

for i in $(seq 1 10);do echo $i;done 可以用这种

感谢您的解答,已经找到问题的原因,您的方法也是一个非常不错的解决办法,再次 表示感谢

Reply View the author
deepinuser17
deepin
2023-02-02 07:11
#5

更传统的用法是:

 

for i in `seq 1 10`; do echo $i; done

 

在被Debian/Ubuntu的sh坑了一把之后, 在所有脚本程序里都把/bin/sh改成了/bin/bash.  这样无论是Redhat系, 还是Debian系, 同样的脚本都可以正常运行。

Reply View the author
神末shenmo
deepin
Spark-App
Q&A Team
2023-02-02 17:32
#6

写个shebang

#!/bin/bash

Reply View the author
weijiahao
deepin
2023-02-06 07:12
#7
deepinuser17

更传统的用法是:

 

for i in `seq 1 10`; do echo $i; done

 

在被Debian/Ubuntu的sh坑了一把之后, 在所有脚本程序里都把/bin/sh改成了/bin/bash.  这样无论是Redhat系, 还是Debian系, 同样的脚本都可以正常运行。

感谢,这是一个不错的建议

Reply View the author
weijiahao
deepin
2023-02-06 07:13
#8
神末shenmo

写个shebang

#!/bin/bash

感谢坛友的回复,/bin/bash确实是更规范的写法

Reply View the author