Is there a way I can specify the name of a list as an argument to a shell script and then use the values of that list name in the script?
I need to do this WITHOUT using case statements.
Something like this:
check.sh list1
#!/bin/bash
list1="www.amazon.com www.google.com"
list2="www.redhat.com www.apple.com"
list3="www.amazon.com www.apple.com"
for H in $1;
do ping -c 1 $H; # Doesn't work
done;
My reason for not wanting to use case statements is that the lists will be added/changed/removed by other Sys Admins and I don't want all of us to be mucking around with the script. Actually the lists will be in a different file altogether and I'll just source that file to make it even less likely the script will need to be opened.
If there's not a method in shell, can Perl handle this?
#!/bin/bash
list1="www.fsf.org www.defectivebydesign.org"
list2="www.gnu.org www.bash-hackers.org"
list3="www.linux-libre.fsfla.org www.stallman.org"
ref=$1
for H in ${!ref}
do ping -c 1 $H
done
Could you post your output please. Your script isn't working for me? $ check.sh list1
---------- Post updated at 06:40 PM ---------- Previous update was at 06:17 PM ----------
@ junior-helper
I didn't know about ref . Thanks
When I run your script, it uses the list1 variable list1="www.fsf.org www.defectivebydesign.org" , not the command line argument. $ check.sh list1
If I comment out the list1 variable in your script and run it, there is no output?
You're welcome! The name of the reference variable doesn't have to be ref though.
Of course it uses the command line argument, otherwise it would not show the content of the list1 variable.
$1 is the first command line argument ref=$1 command line argument is assigned to the ref variable ${!ref} accessing $list1 via $ref.
There is no error checking at all.
If you comment out list1 and use list1 as argument, the script fails to find the list1 variable, thus there is no output. It's the same as if you try check.sh list4 or even just check.sh .
Another issue you might come across is that the script will ignore potential arguments after the first argument, e.g. check.sh list1 list3 , I think you know why.
uname -a
Linux $(hostname) 3.17.3-200.fc20.x86_64 #1 SMP Fri Nov 14 19:45:42 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
bash --version
GNU bash, Version 4.2.53(1)-release (x86_64-redhat-linux-gnu)
...
You guys are quick and very helpful! Thank you very much.
My original question was answered and I'll use something like below to start with. I'll incorporate the other suggestions to make my script better.