[英]echo - Syntax error: Bad substitution
A script with a problem: 有问题的脚本:
1 #!/bin/bash
2
3 skl="test"
4 # get length
5 leng=$(expr length $skl)
6 # get desired length
7 leng=$(expr 22 - $leng)
8
9 # get desired string
10 str=$(printf "%${leng}s" "-")
11
12 # replace empty spaces
13 str=$(echo "${str// /-}")
14
15 # output
16 echo "$str obd: $skl $str"
17
but it outputs: 但它输出:
name.sh: 13: Syntax error: Bad substitution
please help, thanks I would be very grateful :) 请帮忙,谢谢,我将非常感激:)
The following line: 以下行:
str=$(echo "${str// /-}")
is resulting into Syntax error: Bad substitution
because you are not executing your script using bash
. 导致
Syntax error: Bad substitution
因为您没有使用bash
执行脚本。 You are either executing your script using sh
or dash
which is causing the error. 您正在使用
sh
或dash
执行脚本,这会导致错误。
EDIT: In order to fix your script to enable it to work with sh
and dash
in addition to bash
, you could replace the following lines: 编辑:为了修复您的脚本以使其能够与除了
bash
之外的sh
和dash
一起使用,您可以替换以下行:
# get desired string
str=$(printf "%${leng}s" "-")
# replace empty spaces
str=$(echo "${str// /-}")
with 同
str=$(printf '=%.0s' $(seq $leng) | tr '=' '-')
Take out all unnecessary expr calls, using pure BASH features: 使用纯BASH功能取出所有不必要的expr调用:
#!/bin/bash
skl="test"
# get length
leng=${#skl}
# get desired length
leng=$((22 - leng))
# get desired string
str=$(printf "%${leng}s" "-")
# replace empty spaces
str=$(echo "${str// /-}")
# output
echo "$str obd: $skl $str"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.