문제

I have created a bash script which will extract a tar.gz file which is decompressed 5 times and remain the last tar.gz file decompressed. When I execute this acript I got this error line 26: 0++: syntax error: operand expected (error token is "+") . Please see below script.

i=0
for tarfile in *.tar.gz
                        do
                         $(($i++))
                         [ $i = 5 ] && break
                tar -xf "$tarfile"
                done

What is the error about and what is the correct way to solve my problem which is extracting the file five times and remain the last file decompressed. Thanks in advance for those who help.

도움이 되었습니까?

해결책

you want to change $(($i++)) to ((i++)). see https://askubuntu.com/questions/385528/how-to-increment-a-variable-in-bash

Let's deconstruct $(($i++)). going outwards, $i expands to 0. so we have the expression $((0++)). 0 can't be incremented since it is a value, not a variable. so you get your error message line 26: 0++: syntax error: operand expected (error token is "+").

The reason to use ((i++)) without a $ at the front is that the $ at the front would actually evaluate i. You don't want i to be evaluated here, you just want i to be incremented. (h/t the other guy)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top