Domanda

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.

È stato utile?

Soluzione

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)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top