While loop using command output...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop using command output...
# 1  
Old 12-02-2008
While loop using command output...

If I run this command
Code:
networksetup -listallnetworkservices

I get the following output.
Code:
Ethernet
AirPort
*Parallels Host-Guest
*Parallels NAT
MY VPN
Ethernet 2

I want to make changes to only anything that contains the word "Ethernet" which I can do with grep.
But What I really need a little help is with a While loop run this command on each of my adapters.

Thanks in advance.

Last edited by elbombillo; 12-02-2008 at 04:24 PM..
# 2  
Old 12-02-2008
Code:
#!/bin/ksh
networksetup -listallnetworkservices |grep Ethernet | while read LINE
do
#   do what need to do with $LINE
done

# 3  
Old 12-02-2008
This is probably is REAL DUMB NOOB question, and I SHOULD know this by now.... but can I run this a standard sh script because you used #!/bin/ksh Instead of #!/bin/sh
# 4  
Old 12-02-2008
it should work for either
# 5  
Old 12-02-2008
Code:
networksetup -listallnetworkservices |grep Ethernet | while read LINE
do
networksetup -setwebproxy $LINE com.intranet.proxy 8080
networksetup -setautoproxyurl $LINE http://config.proxy.
networksetup -setautoproxystate $LINE off
done

I get this as a return
Code:
** Error: The parameters were not valid.
** Error: The parameters were not valid.


If I do echo it goes display the correct information, does this do line by line or entire output?
Also If I manually enter each command with each Ethernet output, but get this via script.
# 6  
Old 12-02-2008
that appears to be an error from networksetup not from the looping. Check your commands.

you might need to put double quotes:
Code:
networksetup -listallnetworkservices |grep Ethernet | while read LINE
do
networksetup -setwebproxy "$LINE" com.intranet.proxy 8080
networksetup -setautoproxyurl "$LINE" http://config.proxy.
networksetup -setautoproxystate "$LINE" off
done

# 7  
Old 12-02-2008
OMG it works. I've been thing to find out how to run loops from grep outputs for a long time. THANK Man, You have taught me how to fish.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Python Paramiko multi threading to capture all output for command applied in loop

My issue : I am getting only last command output data in ouput file. Though comamnd "print(output)" displays data for all 3rd column values but the data saved in file is not what required it hs to be the same which is being printed by command"print(output)". Could you please help me to fix this,... (0 Replies)
Discussion started by: as7951
0 Replies

2. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

3. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

4. Red Hat

Command understanding the output file destination in case of standard output!!!!!

I ran the following command. cat abc.c > abc.c I got message the following message from command cat: cat: abc.c : input file is same as the output file How the command came to know of the destination file name as the command is sending output to standard file. (3 Replies)
Discussion started by: ravisingh
3 Replies

5. Shell Programming and Scripting

Redirect output of command line to for loop

I would like to redirect output of command line in for loop as $line. Output should be processed as line but instead it throw whole output. Could somebody help me on how to redirect output of command line and process it line by line without sending output to any file. below is my code ... (1 Reply)
Discussion started by: tapia
1 Replies

6. UNIX for Dummies Questions & Answers

Need to get a loop variable from output of a command

Hi, I am trying to get a loop counter i and set its value as the ouput of a command: i=`printmo TEST1 | grep -i TEST2 | wc -l` Then I want to use i as counter to run a loop i number of times. Like if i gets a value of 5 I'll have to run loop 5 times. But will i here be a numeric... (3 Replies)
Discussion started by: pat_pramod
3 Replies

7. Shell Programming and Scripting

while loop output

:wall:Hi I am a beginner to unix In a shell script i see the below code # set admin email so that you can get email ADMIN=someone@somewhere.com host=`hostname` date=`date` # set alert level 70% is default ALERT=70 df -h | grep / | grep -v '^Filesystem|tmpfs|cdrom' | awk '{ print... (1 Reply)
Discussion started by: prabhu_kumar
1 Replies

8. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

9. UNIX Desktop Questions & Answers

Loop column output

I need help in what to do with a bash script? I'm trying to run a command to output the data from a table and then insert it into commands. Looping for each row of data. For example the output data from a table: 10 John house 20 Jane apt 30 Joe townhomeThen I need to take the output... (1 Reply)
Discussion started by: handband2
1 Replies
Login or Register to Ask a Question