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.
# 8  
Old 12-07-2014
/*
EDIT:
@ Junior-Helper: very nice one, thank you!
*/

For Ongoto:
(I just inserted linebreaks for readability)
Code:
✔ ~ $ cat testscript.sh list.txt ; sh testscript.sh list1

#!/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

list1="www.amazon.com www.google.com"
list2="www.redhat.com www.apple.com"
list3="www.amazon.com www.apple.com"

Pinging 
ping: unknown host 
Pinging www.amazon.com
PING www.amazon.com (176.32.98.166) 56(84) bytes of data.

--- www.amazon.com ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

Pinging www.google.com
PING www.google.com (173.194.35.19) 56(84) bytes of data.
64 bytes from mil01s16-in-f19.1e100.net (173.194.35.19): icmp_seq=1 ttl=56 time=26.8 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 26.844/26.844/26.844/0.000 ms
+ ~ $

That is on and with:
Code:
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)
...

Hope this helps

Last edited by sea; 12-07-2014 at 02:43 AM..
This User Gave Thanks to sea For This Post:
# 9  
Old 12-07-2014
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.

Code:
$ cat ping_names.txt 

LIST1="www.fsf.org www.google.com"
LIST2="www.redhat.com www.apple.com"
LIST3="www.fsf www.apple.com"

Code:
#!/bin/bash

source ./ping_names.txt

ref=$1

for H in ${!ref};  
   do ping -c 1 $H;     
done;

Code:
$ ./check.sh LIST1
PING www.fsf.org (208.118.235.131) 56(84) bytes of data.
64 bytes from www.fsf.org (208.118.235.131): icmp_req=1 ttl=52 time=84.8 ms

--- www.fsf.org ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 84.859/84.859/84.859/0.000 ms
PING www.google.com (74.125.25.147) 56(84) bytes of data.
64 bytes from pa-in-f147.1e100.net (74.125.25.147): icmp_req=1 ttl=47 time=19.6 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 19.685/19.685/19.685/0.000 ms

# 10  
Old 12-07-2014
You don't need the ref=$1. Try for H in ${!1};
This User Gave Thanks to RudiC For This Post:
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