Visit Our UNIX and Linux User Community


How to get the for loop output from a remote server in UNIX shell scripting?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the for loop output from a remote server in UNIX shell scripting?
# 1  
Old 11-05-2013
How to get the for loop output from a remote server in UNIX shell scripting?

Hi,

I am using ksh , when i try to use for loop i am getting the expected output.

Code:
$for variable in $(ps -fu user | grep -i something/ | grep -i something | grep -v grep | awk '{print $2}');do
> grep $variable /tmp/some_path/*/*
> done

when tried the below to remote server, getting the entire content rather than the expected output.

Code:
$for variable in $(ssh server_name "ps -fu user | grep -i something/ | grep -i something | grep -v grep | awk '{print $2}'");do
> grep $variable /tmp/some_path/*/*
> done

I tried the below to remote server , but getting the below error.

Code:
$ssh server_name "for variable in $(ps -fu user | grep -i something/ | grep -i something | grep -v grep | awk '{print $2}');do
> grep $variable /tmp/some_path/*/*;
> done"

Code:
/bin/ksh: syntax error at line 2: `31656' unexpected

I am not sure what is the syntax mistake i did in the above script.

Please advise.

Thanks,
Regards,
karthikram
# 2  
Old 11-05-2013
If you have pgrep then you can probably do
Code:
pgrep -u user something

and no problem to run that as ssh argument.
If you insist in your complex commands, save them in a script command.sh
And run it remotely with
Code:
ssh -qx server_name ksh < command.sh

By passing via a stream the script commands are only interpreted by one shell (the ksh at the remote server).
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 11-05-2013
Thanks you very much , i came across pgrep many times but it didnt strick.

I am able to learn that if we need to run such kind of script better to save and run.

Code:
By passing via a stream the script commands are only interpreted by one shell (the ksh at the remote server).


Thanks,
Regards,
karthikram
# 4  
Old 11-05-2013
You'll need to fiddle around with some serious escaping, like (and eventually more)
Code:
$ssh server_name 'for variable in $(ps -fu user | grep -i something/ | grep -i something | grep -v grep | awk "{print\ \$2}");do  grep "$variable" /tmp/some_path/*/*; done'

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-05-2013
Thank you very much , you are all great, it worked perfectly.

i made only this change to work in ksh

Code:
$ssh server_name 'for variable in $(ps -fu user | grep -i something/ | grep -i something | grep -v grep | awk "{print \$2}");do  grep "$variable" /tmp/some_path/*/*; done'

Thanks,
Regards,
karthikram

Previous Thread | Next Thread
Test Your Knowledge in Computers #891
Difficulty: Easy
In Unix, sed is often used as a filter in a pipeline.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

2. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

3. Shell Programming and Scripting

Ssh to remote server loop is breaking

hi All, cat login.list server1 userid1 server2 userid2 server3 userid3 ---------------------------------------- #SSHSCRIPT.ksh FILE=login.list while read vah vah_id do ssh $vah -l $vah_id "pwd" done < "$FILE" ----------------------------------------- When i... (2 Replies)
Discussion started by: raghur77
2 Replies

4. Shell Programming and Scripting

How to pass current year and month in FOR LOOP in UNIX shell scripting?

Hi Team, I have created a script and using FOR LOOP like this and it is working fine. for Month in 201212 201301 201302 201303 do echo "Starting the statistics gathering of $Month partitions " done But in my scripts the " Month " variable is hard-coded. Can you please any one... (3 Replies)
Discussion started by: shoan
3 Replies

5. Shell Programming and Scripting

Triggering remote UNIX shell script from Remote desktop

I m trying to run a batch script in remote desktop which executes unix commands on the unix server...the problem is i wnt the output in HTML format.so in my batch script i m giving the cmd like ssh hostname path ksh HC_Report.ksh>out.html ...but it generates the HTML file in remote desktop .i... (2 Replies)
Discussion started by: navsan
2 Replies

6. Shell Programming and Scripting

how to store output into variable-in unix shell scripting

Hi, Output of "ps -o etime,time,pcpu,pmem,fname -C sbd-java" command is - Elapsed Time %cpu %MEM COMMAND 02:14:03 00:03:28 2.5 6.3 sbd-java Can anyone tell me, how to store the the value 2.5 in a variable? When I say echo $X where x is a variable then... (4 Replies)
Discussion started by: pspriyanka
4 Replies

7. Shell Programming and Scripting

generate tabular output from an input text file in unix shell scripting

Hi, I have the output (as below) which i want it to be in a table. For e.g. space utilization in PSE on path /logs is 0% space utilization in PSE on path /logs/tuxedo/tuxlsp is 16% space utilization in PSE on path /ldvarlsp/lsp/log is 37% space utilization in PSE on path /home is 6%... (7 Replies)
Discussion started by: pkbond
7 Replies

8. Shell Programming and Scripting

Remote server's output

Hi All, Can anybody help me, i have 50 servers within a network. For checking the HDD utilization using df -k, Right now i am logging into each server daily and then noting the output. I want that i should be able to run the command in all servers while sitting in single server and getting... (1 Reply)
Discussion started by: varunksharma87
1 Replies

9. Shell Programming and Scripting

Running for loop on remote server

Hi, I'm having a problem performing for loop on remote server, i know this can be done with one liner but i'm not sure how it works if using logical operator such as for ifs and case or while for server in sterverA serverB serverC ; do ssh -v $server "cd ~/MyDocuments/; bag=`find... (6 Replies)
Discussion started by: sexyTrojan
6 Replies

Featured Tech Videos