Specifying a list name as argument and using that list in script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Specifying a list name as argument and using that list in script.
# 1  
Old 12-06-2014
Specifying a list name as argument and using that list in script.

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:
Code:
check.sh list1

Code:
#!/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?

Thanks,
Gary
# 2  
Old 12-06-2014
Changed it slightly to separate the items in the line.
Code:
while read line
do
    for H in $(echo $line | tr -d '"' | tr ' ' '\n')
    do
        echo $H
        ping -c 1 $H
        echo
    done
done < $1

The above should work for list files of any name or size with one item per line,
or many items per line (space seperated, quoted or unquoted).
Quote:
This is confusing?

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.

Last edited by ongoto; 12-06-2014 at 10:43 PM.. Reason: rewrite -- first example broke
# 3  
Old 12-06-2014
Guess he ment more something like:

Untested:
Code:
#!/bin/bash
[[ -z "$1" ]] && echo "You passed no arguments!" && exit 1
SEARCH="$1"
oIFS="$IFS"
IFS="= \""
FILE_LIST="$HOME/list.txt"

grep ^$SEARCH "$FILE_LIST"|while read var entries;do
	for entry in $entries;do
		echo "Pinging $entry"
		ping -c1 "$entry"
	done
done

While the $HOME/list.txt contains:
Code:
list1="www.amazon.com www.google.com"
list2="www.redhat.com www.apple.com"
list3="www.amazon.com www.apple.com"

Hope this helps

EDIT (tested):
Forgot to add the quotes to IFS

Last edited by sea; 12-06-2014 at 09:52 PM..
# 4  
Old 12-06-2014
Quote:
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?
Yes. It's called indirect referencing.
Code:
#!/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

These 4 Users Gave Thanks to junior-helper For This Post:
# 5  
Old 12-06-2014
@ sea

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?

Last edited by ongoto; 12-06-2014 at 10:46 PM..
This User Gave Thanks to ongoto For This Post:
# 6  
Old 12-06-2014
You're welcome! The name of the reference variable doesn't have to be ref though.

Quote:
When I run your script, it uses the list1 variable list1="www.fsf.org www.defectivebydesign.org" , not the command line argument.
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.

Quote:
If I comment out the list1 variable in your script and run it, there is no output?
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.

Hope this helps.
These 2 Users Gave Thanks to junior-helper For This Post:
# 7  
Old 12-07-2014
OK
I understand how it works now.

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Argument list too long

Hi Team, Here's the situation. I have approximately 300000 to 500000 jpg files in /appl/abcd/work_dir mv /appl/abcd/work_dir /appl/abcd/process_dir The above move command will work if the jpg files count is close to 50000 (not sure). If the count is less this mv command holds good. But if... (14 Replies)
Discussion started by: kmanivan82
14 Replies

2. Shell Programming and Scripting

[Solved] Argument list with awk

Hello, I want to execute remote command with ssh. For exemple, i have a variable SERVERS=lpar1,lpar2,lpar3 I want to execute some commands like: ssh -q lpar1 ls / ssh -q lpar2 ls / ssh -q lpar3 ls / Can you help me with awk command ? Thank you :) (6 Replies)
Discussion started by: khalidou13
6 Replies

3. UNIX for Advanced & Expert Users

Argument list too long w/ sed

Hi all, I am using GNU sed (named gsed under macports) in OSX. I have a directory with a series of files named pool_01.jpg through pool_78802.jpg. I am trying to use this command to rename the files to their checksum + extension. md5sum * | gsed -e 's/\(*\) \(.*\(\..*\)\)$/mv -v \2 \1\3/e' ... (3 Replies)
Discussion started by: openthomas
3 Replies

4. Shell Programming and Scripting

mv : Argument list too long

Hi I am using find command -- find "directory1" -type f | xargs -i mv {} "directory2" to avoid above argument list too long problem. But, issue i am facing is directory1 is having subdirectories due to this i am facing directory traversal problem as i dont want to traverse subdirectories... (9 Replies)
Discussion started by: VSom007
9 Replies

5. Shell Programming and Scripting

Argument list too long!!

Dear Experts, I have a list of 10K files in a directory. I am not able to execute any commands lile ls -lrt, awk, sed, mv, etc........ I wanna execute below command and get the output. How can I achieve it?? Pls help. root# awk -F'|' '$1 == 1' file_20120710* | wc -l /bin/awk: Argument list... (2 Replies)
Discussion started by: Naga06
2 Replies

6. Shell Programming and Scripting

Argument list too long problem

I have a huge set of files (with extension .common) in my directory around 2 million. When I run this script on my Linux with BASH, I get /bin/awk: Argument list too long awk -F'\t' ' NR == FNR { a=NR } NR != FNR { sub(".common", "", FILENAME) print a, FILENAME, $1 } '... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. UNIX for Dummies Questions & Answers

Complete command list/argument using ps

Hi, Using ps, I can't work out what is the right options to use to show full listing of the process or command, can someone please advise what options I should be using? Example output of a "trimmed" process is as below and I know this is not the complete command line that I've executed,... (8 Replies)
Discussion started by: newbie_01
8 Replies

8. Shell Programming and Scripting

Argument too long list error

I have a wrote a script which consits of the below line.. Below of this script I'm getting this error "ksh: /usr/bin/ls: arg list too long" The line is log_file_time=`ssh -i $HOME/.ssh/id_rsa -q $i ls -lrt /bp/karthik/test/data/log/$abc*|tail -1|awk '{print $8}'` And $abc alias is as "p |... (1 Reply)
Discussion started by: 22karthikreddy
1 Replies

9. UNIX for Dummies Questions & Answers

Argument list too long - SSH

Hi I executed the code for file in `ls pdb*.ent` do new_name=`echo $file | sed 's/^pdb//;s/.ent/.txt/'` mv $file $new_name done Its giving error at ' ls pdb*.ent' argument list too long i have around 150000 entries please help Thank you (6 Replies)
Discussion started by: empyrean
6 Replies

10. Shell Programming and Scripting

getopts and a list argument

Hi, I'm using bash and ksh93 compatible derivatives. In a recent getopts experience, I found myself spending far too much time on this little problem. I hope someone can help... So here's the deal. I want to build have a command line interface that accepts either zero, one, or... (4 Replies)
Discussion started by: duderonomy
4 Replies
Login or Register to Ask a Question