문제

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 echoing 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top