Script output as input for next command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script output as input for next command
# 1  
Old 08-06-2013
Script output as input for next command

Hi All,
Hoping you can help as im in desperate need... I'm very new to unix scripting so apoligies,

I have setup an expect script in order to log into a node on our network, This will provide an output as per the below

Code:
*********** information:
    *************: n/a
    TEST IP : n/a
    ****************: n/a                  ***************: n/a

  ********* Information:
    **********: 345:66:25434
    **********: 345:66:8468
    *********: n/a
    *******************: n/a
    *******************:
      ***** ***** **: 33:345:66
      TEST IP : 123.45.67.89

What I need to be able to do is read the IP address next to where it says TEST IP and then use this in another command. Problem I have is the IP does not always display in the bottom TEST IP line sometimes it can be in the first TEST IP line and N/A will be next to the second.

Essentially I need to read the IP address next to TEST IP and then use in another command. Would anyone be kind enough to help me out please. Sorry for the stars but as its security info I cant show it.

Thanks In advance.
# 2  
Old 08-06-2013
This should output IP from lines with TEST IP
Code:
awk -F"[ :]+" --posix '$0~/TEST IP/ && $4~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ {print $4}' file

# 3  
Old 08-06-2013
Code:
awk '/TEST IP/&&!/n\/a/{print $NF}' file

# 4  
Old 08-06-2013
thanks

Thanks for your response's guys, So this is a print output from within an expect script. I need the IP addresses to be read and then I want to execute another command using that ip as the variable.

How would you recommend is best to do this.
# 5  
Old 08-06-2013
Log to a file and run the awk on that file when the time comes.
# 6  
Old 08-06-2013
if the next script is occuring almost simultaneously with the first script, you can just set a variable to the value of the ip address and then run the next command (see sample below) ...
Code:
MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2}'  file)
ping $MYIP

# 7  
Old 08-07-2013
If this condition is true for more than one IP
MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2}' file)
you can use exit to get only the first.
MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2;exit}' file)
This will make this work ping $MYIP
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capturing script output and input to log

Hi Is there a way that I can capture a shell script (both output and input) to a log file where I can analyze it? Thanks (6 Replies)
Discussion started by: nimo
6 Replies

2. UNIX for Dummies Questions & Answers

Using grep output as input for sed command

Hi, I would like to know if this is possible, and if so what can i do to make this work. I would like to grep a line X from fileA and then use the output to replace a word Y in fileB. grep "line X" fileA | sed -e 's/Y/X/g' > outfile this statement does not work, as i do not know how to... (7 Replies)
Discussion started by: cavanac2
7 Replies

3. Shell Programming and Scripting

Command Output to Standard Input

Hi All, How do I provide the output of a command to another command which is waiting for an input from the user ? Ex : I need to login to a device via telnet. In the script, initially I use the "read" command to get the IP Address, Username and Password of the device from the user. Now,... (1 Reply)
Discussion started by: sushant172
1 Replies

4. UNIX for Dummies Questions & Answers

Send output of grep as input of kill command

I would appreciate any help. I need to run 'ps -ef | grep 'process', get the process id and kill that process. I have got this far: - Get pid using ps -ef | awk '/process/{ print $2}' after this I'm kind of stuck.. - Use pipe to redirect the output to kill pid=ps -ef | awk '/bmserver/{... (2 Replies)
Discussion started by: foxtron
2 Replies

5. Shell Programming and Scripting

how to input the CAT command output in Shell

Hi guys... I am new to this scripting...so please forgive me if anything worng in my questions... here is my question.. I have file structure /home/oracle/<sid>/logs/bkup now i want to write a script which should grep the sid name from a file..and it should replace the <SID> with... (1 Reply)
Discussion started by: troubleurheart
1 Replies

6. UNIX for Dummies Questions & Answers

problem with output of find command being input to basename command...

Hi, I am triying to make sure that there exists only one file with the pattern abc* in path /path/. This directory is having many huge files. If there is only one file then I have to take its complete name only to use furter in my script. I am planning to do like this: if ; then... (2 Replies)
Discussion started by: new_learner
2 Replies

7. Shell Programming and Scripting

Shell script getting input from output

I have a program that can be run in terminal, when its run it either returns SSH OK or CRITICAL, how do i use the output in my script? good ./check_sh myserver SSH OK bad ./check_sh myserver CRITICAL I want to store it in a variable btw, SSH OK will give the variable $SSH=1 and if its... (1 Reply)
Discussion started by: aspect_p
1 Replies

8. Shell Programming and Scripting

Using output to input another command

Hi guys. Is it possible (I'm sure it is) to use the output of a simple 'ls' command as input of another command 'tail'. It is not really the output of the 'ls'. I have to useeach line of the output. This is the first command... ls *myFile*021308* Which it outputs many filenames. For each... (3 Replies)
Discussion started by: rodrimuino
3 Replies

9. Shell Programming and Scripting

Using Output from one command as input to another

This site has been very helpful thus far.. I thank you all in advance for sharing the knowledge. Let me get to it. I am trying to write a very small script to take away from the boredom of doing the same thing over and over. Everynow and again I have to get the hex value of a file using a... (2 Replies)
Discussion started by: BkontheShell718
2 Replies

10. Shell Programming and Scripting

Asking about shell script input output redirection

Hi, Can anyone please tell me what these lines do? ls >& outfile ls outfile 2>&1 Thanks. (1 Reply)
Discussion started by: trivektor
1 Replies
Login or Register to Ask a Question