-   Linux - Newbie (
-   -   rsh command issue (

vicky007aggrwal 03-01-2013 06:54 AM

rsh command issue
Hi Guys,
iam not able to run below rsh command having the for loop having back tips in it
please suggest i wasted whole one day to figure it out
but still no solution.
please assits what is the issue
rsh -l <user name> <machine name> "for i in `cat ~/file`
echo $i
i am using redhat linux 5

fortran 03-01-2013 07:25 AM

What do you want to do using this script.
I think this for loop will show every word before space as a separate line.
Have you run this for loop on your system first? Is it giving proper results?
If you want to run on other machine, you can use ssh too.

I can give you example to run the script using ssh.
You have used double quote that can be the problem.
An example to run the script containing loop

ssh 192.165.xx.xx 'for i in `cat /path/of /the/file` ; do echo $i; done'

My public key is saved as 'authorized_keys' in .ssh of server that's it does not ask me password while running the script.

Where are you getting the problem?
Is it just finished showing command prompt of remote machine and not executing the rest of the script?
Paste your error, if there is any.

shivaa 03-01-2013 08:14 AM

Perhaps you should get any error or message while running it? Can you make a try on:

~$ rsh -l <user name> <machine name> "for i in `cat ~/file`; do echo $i; done"
Also make a try with what @pavi_kanetkar has said above.

vicky007aggrwal 03-01-2013 10:56 AM

we're using rsh in our project & the actual issue is we're not able to use back tick in a rsh command..

any thoughts how we can use back ticks in rsh commands

shivaa 03-01-2013 11:18 AM

In that case, if login shell for user username on machinename is bash, then you can:

~$ rsh -l <username> <machinename> "for i in $(cat ~/file); do echo $i; done"
But how can you say that rsh won't accept backticks? Also, as asked above, did you get any error or message(s)?

vicky007aggrwal 03-01-2013 02:39 PM

Thanks shiva & how to use pwd command in rsh as below also
Not working. Please suggest

rsh -l <user name> <machine name> "cd /home/hgs/her/server;ps -ef | grep 'pwd' `

In this also pwd command is showing current directory of my base/source
Machine not destination machine.

Kindly help , m bit naive in these

shivaa 03-01-2013 02:49 PM

Give a try to:

~$ rsh -l <user name> <machine name> "cd /home/hgs/her/server; pwd"
Do not use ps -ef | grep 'pwd', it shouldn't give you appropriate results.

vicky007aggrwal 03-01-2013 02:52 PM

Thanks shivaa for so prompt reply,but my objective is
To grep the process running from the respective directories
Do you hve any better solution

Can u help me in understand why ps -ef|'pwd' is not apt one

shivaa 03-01-2013 03:00 PM

Sorry, I thought that you just need to check working directory.

You can try:

~$ rsh -l <username> <machinename> "cd /home/hgs/her/server; ps -aef | grep $(echo $PWD)"

~$ ps -ef  |grep 'pwd'
It will return all process with name having 'pwd' inside it, which don't make much sense. Instead you can:


~$ ps -aef | grep $(echo $PWD)
But let know know why you want to get a process containg PWD word or working directory?

jpollard 03-01-2013 03:01 PM

Do the pwd without the ps... the string you get back will be something like '/home/username'.

The ps -ef command will give you strings like "gdm 6765 1 0 Feb27 ? 00:00:00 /usr/libexec/at-spi-bus-launcher" or "username 30558 1 0 08:59 ? 00:00:02 nautilus -n"

Command executed do not necessarily have "/home/username" format... usually they are relative paths, not full paths (as shown in the second line - the command is nautilus... but unless the user types in the full path /usr/bin/nautilus to start the program, the "/usr/bin" will not be present, and neither will the "/home/username/xxx" show for the "xxx" program - only "xxx" will be there.

BTW, the reason your `pwd` only executes locally because your string is being interpreted locally before being passed to the remote system. If you want to pass the `pwd` to the remote system, enclose the command string with single quotes (apostrophe) instead of double quotes.

schneidz 03-01-2013 04:23 PM

i think we volleyed back and forth enough here. can the op explain why the antiquated and insecure program rsh is preferable to ssh ?

also how are you sure that the backticks is what is messing up your script.

you will probably get better results if you paste a sample of the inputs and what you want the output to look like.

vicky007aggrwal 03-02-2013 12:44 AM

thanks everyone for your help, jpollard your suggestion worked when i used the single quotes with back ticks in my rsh command

i have one last issue now, let me explain my scenario to clear out the confusion , i am invoking below command

rsh -l <username> <machinename> 'cd $DIR_LOCATION; ps -aef | grep `pwd` | grep -v grep| grep -i catalina;echo $?'
now what is happening is that $DIR_LOCATION variable is not getting replaced with its value as i am using single quotes but the irony the back tick only works in single
quotes. I tried using doube quotes in place of single but then the command in back tick is not getting executed.

we have one legacy application which is been configured with rsh oonly not ssh,thats why i have to go with that only

Please please share your thoughts & help me in resolving this issue
thanks again for your time.

shivaa 03-02-2013 12:57 AM

This is because you've declared this variable in on your local machine, but trying to use it on remote machine. DIR_LOCATION is not any shell or environmental variable, so your system cannot take it's value until it's declared in the remote system.

Just do like:

~$ rsh -l <username> <machinename> 'DIR_LOCATION=$1; cd $DIR_LOCATION; ps -aef | grep `pwd` | grep -v grep| grep -i catalina;echo $?'

vicky007aggrwal 03-02-2013 01:09 AM

Thanks SIva, there is bit more to my use case,actually i canont use $1 directly as i have series of other variables invloved in this process.

As mentioned below APP_STRING & DIR_LOCATION are GLOBAL variables, i cannot afford to write the same variable assignments again in rsh

is there any other approach that i can use, sorry for not narrating the complete scenario earlier but now its complete.

$APP_STRING=`cat configfile | grep -i $1`
$DIR_LOCATION=`echo $APP_STRING |awk -F'|' '{ print $2 }'
rsh -l <username> <machinename> 'cd $DIR_LOCATION; ps -aef | grep `pwd` | grep -v grep| grep -i catalina;echo $?'

shivaa 03-02-2013 02:05 AM

Well, in that case, better to create a script on remote machine, add all stuff in that script and invoke that script from your local machine using rsh.

On remote machine:

~$ vi /home/username/
APP_STRING=`cat configfile | grep -i $1`
DIR_LOCATION=`echo $APP_STRING |awk -F'|' '{ print $2 }'`
ps -aef | grep `pwd` | grep -v grep| grep -i catalina
echo $?

Then on local machine:

~$ rsh -l <username> <machinename> "./"
~$ rsh -l <username> <machinename> "bash"
~$ rsh -l <username> <machinename> "./ <something>"

Note: <something> will be used as $1 i.e. argument passed to the script.

Also take a note, do not use $ symbol before variable name while declaring it. But use when invoking it.

All times are GMT -5. The time now is 06:23 PM.