当我执行下面的脚本时,它只是坐在那里没有输出.有什么想法有什么不对?
#!/bin/bash
for k in `seq 0 4`
do
for c1 in `seq 1 30`
do
for c2 in `seq $c1 30`
do
for b1 in `seq 1 $c1`
do
for b2 in `seq $b1 $c2`
do
for s1 in `seq 0 (($c1 - $b1))`
do
for s2 in `seq 0 (($c2 - $b2))`
do
echo "k: $k - c1: $c1 - c2: $c2 - b1: $b1 - b2: $b2 - s1: $s1 - s2: $s2"
done
done
done
done
done
done
done
Run Code Online (Sandbox Code Playgroud)
其中一个seq陈述无限期地运行.你错过了几个美元符号:
for s1 in `seq 0 $(($c1 - $b1))`
for s2 in `seq 0 $(($c2 - $b2))`
Run Code Online (Sandbox Code Playgroud)
不要seq在这里使用; 这是毫无意义.
for ((k=0; k<4; k++)); do
for ((c1=1; c1<30; c1++)); do
for ((c2=c1; c2<30; c2++)); do
for ((b1=1; b1<c1; b1++)); do
for ((b2=b1; b2<c2; b2++)); do
for ((s1=0; s1<(c1-b1); s1++)); do
for ((s2=0; s2<(c2-b2); s2++)); do
echo "k: $k - c1: $c1 - c2: $c2 - b1: $b1 - b2: $b2 - s1: $s1 - s2: $s2"
done
done
done
done
done
done
done
Run Code Online (Sandbox Code Playgroud)
seq是一个外部命令,bash必须分叉外部进程,运行,读取输出等; 它涉及更多的开销,甚至不能保证在所有操作系统上都存在(或表现相同)!
相比之下,(( ))创建一个数学上下文; 这样的环境中,你并不需要使用$扩展变量,和传统的整数运算符(<,>,++,--,等)可供选择.