[英]BASH: Incrementing a variable using “let”
I want to ask if you guys knew how can i fix my script. 我想问问你们是否知道我该如何解决我的脚本。 I want to go positive with the let command.. any ideas ?
我想积极地使用let命令。
a=1
read -p "Enter any number: " COUNTER
until [ 1 -eq $COUNTER ]; do
echo "What is the name for $COUNTER ?"
read name1
START=$COUNTER
END=1
for i in $START
do
echo "$i"
echo ${name1}_$i: >> foo.sh
done
echo COUNTER $COUNTER
let a\+=1
done
There are several ways to increment a variable in bash: 有几种方法可以增加bash中的变量:
# one way
a=$((a+1))
# or even just
((a++))
# using "let"
let "a=a + 1"
Answering your comment, you could do 回答您的评论,您可以做
${name1}_$((a++))
or 要么
${name1}_$((++a))
depending on whether you wanted to increment the variable before or after echo
ing it. 取决于要在
echo
变量之前还是之后增加变量。
At the very top of your script there is the declaration: 在脚本的最上方是声明:
a=1
that seems to cause the problem. 这似乎引起了问题。 The variable should be declared an integer, which is accomplished two ways:
应该将变量声明为整数,这可以通过两种方式实现:
declare -i a
or 要么
let a=1
Further arithmetic can be done using another let
declaration or double parens notation, where the $
prefix not always has to be used like in the for
loop: 可以使用另一个
let
声明或双倍括号表示法来进行进一步的运算,其中不必总是像for
循环中那样使用$
前缀:
for ((i=0; i<10; ++i)); do
echo "i=$i"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.