Bash 行数和列数取自命令参数;如果缺少,请将默认值设置为3(行)和4(列)。使用Nestedloop**********
我一直得到一个操作数错误,你将如何修复它的代码如下Bash 行数和列数取自命令参数;如果缺少,请将默认值设置为3(行)和4(列)。使用Nestedloop**********,bash,Bash,我一直得到一个操作数错误,你将如何修复它的代码如下 #!/usr/bin/env bash if [ "$#" -eq 0 ] then m=3 n=4 else m=$1 n=$2 fi printf "\n" printf "\n" m_1=$(( m-1 )) n_1=$(( n-1 )) for (( i=0; i<$m; i++ )); do <--- error:operand expected
#!/usr/bin/env bash
if [ "$#" -eq 0 ]
then
m=3
n=4
else
m=$1
n=$2
fi
printf "\n"
printf "\n"
m_1=$(( m-1 ))
n_1=$(( n-1 ))
for (( i=0; i<$m; i++ )); do <--- error:operand expected error token is <
for (( j=0; j<$n; j++ )); do
printf "*"
done
printf "\n"
done
printf "\n"
printf "\n"
#/usr/bin/env bash
如果[“$#”-等式0]
然后
m=3
n=4
其他的
m=1美元
n=2美元
fi
printf“\n”
printf“\n”
m_1=$((m-1))
n_1=$((n-1))
对于((i=0;i您发布的代码对我来说很好,所以我的问题不是解决错误,而是复制/查找错误。我想出了一个脚本,显示了类似的错误消息:
# Make sure m is empty for this test
unset m
for (( i=0; i<$m; i++ )); do
echo 3
done
在本例中,for((j=0;jAlso)出现错误,我将从“$#”
中删除双引号,因为在这里它也被解释为整数,而不是字符串。使用-eq
是正确的,所以您做得对。@Roadowl with(())
您可以使用处理整数。是的,我意识到了。谢谢,我自己从来没有使用过这个构造(我自己也使用老式的方式)。您可以使用:m=${1:-3};n=${2:-4}
# 1. Your script is called with an empty first argument
./my_script.sh "" 4
# Perhaps after calling another program that should return a number,
# but sometimes return en empty string:
./my_script.sh "$(wc -l 2>/dev/null < non_existing_file)" 3
# 2. Your script is called with 1 argument
./my_script.sh 3
./my_script.sh: line xx: ((: j<: syntax error: operand expected (error token is "<")
./my_script.sh: line xx: ((: j<: syntax error: operand expected (error token is "<")
./my_script.sh: line xx: ((: j<: syntax error: operand expected (error token is "<")
#!/usr/bin/env bash
m=${1:-3}
n=${2:-4}
printf "\n\n"
for (( i=0; i<m; i++ )); do
for (( j=0; j<n; j++ )); do
printf "*"
done
printf "\n"
done
printf "\n\n"