-1

I've got a file despicting variables:

IP_1="xx.xx.xx.xx" ID_1="yyyyy"

IP_2="xx.xx.xx.xx" ID_2="yyyyy"

...

IP_6="xx.xx.xx.xx" ID_6="yyyyy"

I want to execute a wget command with the variables including a wildcard.

wget --timeout=5 --wait=2 --tries=3 http://"$IP_* "/getcsv/Log_"$EQ_ *".csv

--output-document=/home/administrator/Logs/"$ID_* "/Log_"$EQ_* "$(date +%F%T).csv -d -v

After execution of the command it seems like the machine doesn't recognize the variable and gives back /home/administrator/Logs/* /Log_* _2018-03-21_12:03:53.csv for example.

How is it possible for me that the script first uses IP_1 + ID_1, than IP_2 + ID_2, ... from the variable file ?

I tried different versions with [], {}, different " " and as an array. Unfortunately i've got nothing working

thanks

best regards

  • What is EQ_*? Is it another numbered variable? – glenn jackman Mar 21 '18 at 13:04
1

You need to do this

for i in {1..6}; do
    ip_var="IP_$i"
    id_var="ID_$i"
    wget ... "http://${!ip_var}/..." -O "/home/administrator/Logs/${!id_var}/..."
done

This uses "variable indirection" as described in the manual: https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

However numbered variables are clumsy. Use arrays instead:

ip=( "xx.xx.xx.xx" "xx.xx.xx.xx" ... "xx.xx.xx.xx" )
id=( "yyyyy" "yyyyy"... "yyyyy" )

for ((i=0; i < ${#ip[@]}; i++)); do
    wget ... "http://${ip[i]}/..." -O "/home/administrator/Logs/${id[i]}/..."
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.