admin 管理员组文章数量: 1086019
On ubuntu I run the following script (which should randomly fail or succeed for testing purposes):
if [ "$RANDOM" -gt 16384 ]; then
echo "Success!"
exit 0
else
echo "Failure!"
exit 1
fi
with
sh random.sh
but it fails with
random.sh: 1: [: Illegal number:
Failure!
What is the cause of the problem?
On ubuntu I run the following script (which should randomly fail or succeed for testing purposes):
if [ "$RANDOM" -gt 16384 ]; then
echo "Success!"
exit 0
else
echo "Failure!"
exit 1
fi
with
sh random.sh
but it fails with
random.sh: 1: [: Illegal number:
Failure!
What is the cause of the problem?
Share Improve this question asked Mar 28 at 9:55 AlexAlex 44.5k104 gold badges300 silver badges513 bronze badges1 Answer
Reset to default 5The $RANDOM
variable is a bash extension, so you have to invoke your script with bash
instead of sh
. On Ubuntu, sh
is typically linked to the more light-weight dash
shell which doesn't support these extensions.
Also see Difference between sh and Bash
To make reasonably sure that the correct interpreter is used, add a shebang to your script:
#!/bin/bash
if [ "$RANDOM" -gt 16384 ]; then
echo "Success!"
exit 0
else
echo "Failure!"
exit 1
fi
and make it executable:
chmod +x random.sh
A user can now simply execute the script without specifying what interpreter it needs in order to work properly:
./random.sh
本文标签: Why does this bash script fail on ubuntuStack Overflow
版权声明:本文标题:Why does this bash script fail on ubuntu? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744044888a2523936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论