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 ?
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:
# 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.
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:
for ((i=0; i<10; ++i)); do
echo "i=$i"
done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.