#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
这个bash脚本在ubuntu上给了我糟糕的替换错误。任何帮助都将不胜感激。
#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
这个bash脚本在ubuntu上给了我糟糕的替换错误。任何帮助都将不胜感激。
当前回答
看起来“+x”会导致问题:
root@raspi1:~# cat > /tmp/btest
#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
root@raspi1:~# chmod +x /tmp/btest
root@raspi1:~# /tmp/btest
root@raspi1:~# sh -x /tmp/btest
+ jobname=job_201312161447_0003
/tmp/btest: 4: /tmp/btest: Bad substitution
其他回答
您的脚本语法是有效的bash和良好的。
故障可能原因:
Your bash is not really bash but ksh or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash. Do ls -l /bin/bash and check it's really bash and not sym-linked to some other shell. If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh or sh script.sh (and your default shell is not bash). Since you have proper shebang, if you have bash ./script.sh or bash ./script.sh should be fine.
我使用#!Bin /bash也尝试了所有的方法,比如在#! Bin /bash之前或之后没有行。 然后又尝试使用+x,但仍然没有工作。 最后我试着运行脚本./script.sh,它工作得很好。
#!/bin/bash
jobname="job_201312161447_0003"
jobname_post=${jobname:17}
root@ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts# sh jar .sh juru .sh: 3: juru .sh:糟糕的替换
root@ip-10-2-250-36: / home / bitnami python-module / workflow_scripts / sh -照顾。 root@ip-10-2-250-36: / home / bitnami python-module / workflow_scripts #
对于到达这里的其他命令,当使用env变量语法执行命令时,也会出现相同的消息,例如${which sh}而不是正确的$(which sh)
尝试使用bash命令显式地运行脚本,而不是将其作为可执行文件执行。
另外,确保脚本的第一行没有空字符串。
即确保#!/bin/bash是脚本的第一行。