我以前从未写过shell脚本,现在遇到一个简单的问题... 我有一个for循环,每次运行run.sh脚本时都会执行。为了查看脚本已经运行到哪里,我想打印出每5000个索引。
$counter = 0
for (( i = 0 ; i <= 5000; i++ ))do
if ($i = $counter); then
echo "$counter"
counter=$(counter+1000)
fi
./run.sh
done
运行这段代码会出现以下错误。
./for_loop.sh: line 1: =: command not found
./for_loop.sh: line 3: 0: command not found
我尝试使用以下方式初始化变量counter:
declare -i counter = 0
这使我得到了下面的错误
./for_loop.sh: line 1: declare: `=': not a valid identifier
=
和let =
,因此您可以使用counter=0
或let counter=0
来分配第一个计数器。let
仅用于数字,而=
适用于任何字符串。 - Derek Mahar