cygwin shell scripting - how to pass values from command line to ssh remote command

classic Classic list List threaded Threaded
10 messages Options
zw
Reply | Threaded
Open this post in threaded view
|

cygwin shell scripting - how to pass values from command line to ssh remote command

zw
Hi
I'm have a shell script, test.sh below to run with cygwin in a Windows box.

#!/usr/bin/sh

ssh qa@load01 'export MAX_MS=100; export OFFSET_MS=89900; export
THREADS=4; export RAMP=1; export LOOPS=2; echo load01 ; cd
/cygdrive/c/apps/bin ; pwd ; nohup ./start.sh  \& ; ps -efW | grep
java ; exit '

This script in turn ssh into another cygwin Windows box which contains
another shell script start.sh to be invoked.

As one can see, the variables MAX_MS, OFFSET_MS, THREADS, RAMP and
LOOPS are hardcoded inside this ssh remote command.

Question:
How can I pass arguments on command line with test.sh such that
variables MAX_MS, OFFSET_MS, THREADS, RAMP and LOOPS
could receive the argument parameters as opposed to hardcoding the numbers ?

Any help is appreciated.

Thanks

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply | Threaded
Open this post in threaded view
|

Re: cygwin shell scripting - how to pass values from command line to ssh remote command

Thorsten Kampe
* Z W (Tue, 14 Oct 2008 11:59:07 -0700)
> I'm have a shell script, test.sh below to run with cygwin in a Windows
> box.
>
> #!/usr/bin/sh
>
> ssh qa@load01 'export MAX_MS=100; export OFFSET_MS=89900; export
> THREADS=4; export RAMP=1; export LOOPS=2; echo load01 ; cd
> /cygdrive/c/apps/bin ; pwd ; nohup ./start.sh  \& ; ps -efW | grep
> java ; exit '

That's not a shell script - it's just a single command. The multiple
export statements are superfluous. The "cd" looks superfluous, too. Also
the "pwd". Also the "exit".
 

> This script in turn ssh into another cygwin Windows box which contains
> another shell script start.sh to be invoked.
>
> As one can see, the variables MAX_MS, OFFSET_MS, THREADS, RAMP and
> LOOPS are hardcoded inside this ssh remote command.
>
> Question:
> How can I pass arguments on command line with test.sh such that
> variables MAX_MS, OFFSET_MS, THREADS, RAMP and LOOPS
> could receive the argument parameters as opposed to hardcoding the numbers ?

http://tldp.org/LDP/abs/html/internalvariables.html -> Positional
Parameters

Thorsten


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

zw
Reply | Threaded
Open this post in threaded view
|

Re: cygwin shell scripting - how to pass values from command line to ssh remote command

zw
Hi Thorsten

Thanks for the response.
I think you understood my question.

I like to pass parameters from tesh.sh as command line arguments and
then pass them into the ssh remote command string. I'm just not sure
of the syntax so that I could pass the paramter values to the remote
command string from test.sh

On Tue, Oct 14, 2008 at 1:10 PM, Thorsten Kampe
<[hidden email]> wrote:

> * Z W (Tue, 14 Oct 2008 11:59:07 -0700)
>> I'm have a shell script, test.sh below to run with cygwin in a Windows
>> box.
>>
>> #!/usr/bin/sh
>>
>> ssh qa@load01 'export MAX_MS=100; export OFFSET_MS=89900; export
>> THREADS=4; export RAMP=1; export LOOPS=2; echo load01 ; cd
>> /cygdrive/c/apps/bin ; pwd ; nohup ./start.sh  \& ; ps -efW | grep
>> java ; exit '
>
> That's not a shell script - it's just a single command. The multiple
> export statements are superfluous. The "cd" looks superfluous, too. Also
> the "pwd". Also the "exit".
>
>> This script in turn ssh into another cygwin Windows box which contains
>> another shell script start.sh to be invoked.
>>
>> As one can see, the variables MAX_MS, OFFSET_MS, THREADS, RAMP and
>> LOOPS are hardcoded inside this ssh remote command.
>>
>> Question:
>> How can I pass arguments on command line with test.sh such that
>> variables MAX_MS, OFFSET_MS, THREADS, RAMP and LOOPS
>> could receive the argument parameters as opposed to hardcoding the numbers ?
>
> http://tldp.org/LDP/abs/html/internalvariables.html -> Positional
> Parameters
>
> Thorsten
>
>
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Problem reports:       http://cygwin.com/problems.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/
>
>

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply | Threaded
Open this post in threaded view
|

RE: cygwin shell scripting - how to pass values from command line to ssh remote command

Dave Korn
In reply to this post by Thorsten Kampe
Thorsten Kampe wrote on 14 October 2008 21:11:

> * Z W (Tue, 14 Oct 2008 11:59:07 -0700)
>> I'm have a shell script, test.sh below to run with cygwin in a Windows
>> box.
>>
>> #!/usr/bin/sh
>>
>> ssh qa@load01 'export MAX_MS=100; export OFFSET_MS=89900; export
>> THREADS=4; export RAMP=1; export LOOPS=2; echo load01 ; cd
>> /cygdrive/c/apps/bin ; pwd ; nohup ./start.sh  \& ; ps -efW | grep
>> java ; exit '
>
> That's not a shell script - it's just a single command.

  Uh, it's a shell script *containing* a single ssh command.  Note the
shebang.  Also note the mention of the filename "test.sh", which is not to be
confused with the script "start.sh" on the remote machine.

> The multiple export statements are superfluous.

  Surely not, if you want the definitions to be visible in the sub-shell that
executes "start.sh"?

> The "cd" looks superfluous, too.

  Surely not, unless /cygdrive/c/apps/bin is the qa user's home dir?

>  Also the "pwd".

  Surely not, if it's a bit of debugging trace that lets ZW know that the
directory exists and so the cd command succeeded and there must be something
else wrong if the script doesn't execute?

> Also the "exit".

  Ok, I'm with you there :-)  But I still think one of us needs more coffee...

    cheers,
      DaveK
--
Can't think of a witty .sigline today....


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply | Threaded
Open this post in threaded view
|

Re: cygwin shell scripting - how to pass values from command line to ssh remote command

Matt Wozniski-3
On Tue, Oct 14, 2008 at 6:41 PM, Dave Korn wrote:

> Thorsten Kampe wrote on 14 October 2008 21:11:
>
>> * Z W (Tue, 14 Oct 2008 11:59:07 -0700)
>>> I'm have a shell script, test.sh below to run with cygwin in a Windows
>>> box.
>>>
>>> #!/usr/bin/sh
>>>
>>> ssh qa@load01 'export MAX_MS=100; export OFFSET_MS=89900; export
>>> THREADS=4; export RAMP=1; export LOOPS=2; echo load01 ; cd
>>> /cygdrive/c/apps/bin ; pwd ; nohup ./start.sh  \& ; ps -efW | grep
>>> java ; exit '
>>
>> That's not a shell script - it's just a single command.
>
>  Uh, it's a shell script *containing* a single ssh command.  Note the
> shebang.  Also note the mention of the filename "test.sh", which is not to be
> confused with the script "start.sh" on the remote machine.

I'm with you here...

>> The multiple export statements are superfluous.
>
>  Surely not, if you want the definitions to be visible in the sub-shell that
> executes "start.sh"?

I think Thorsten meant that a single "export" with multiple var=val
pairs would have sufficed here.

>> The "cd" looks superfluous, too.
>
>  Surely not, unless /cygdrive/c/apps/bin is the qa user's home dir?

Definitely not superfluous, since ./start.sh is in that dir..

>>  Also the "pwd".
>
>  Surely not, if it's a bit of debugging trace that lets ZW know that the
> directory exists and so the cd command succeeded and there must be something
> else wrong if the script doesn't execute?

Yep.

>> Also the "exit".
>
>  Ok, I'm with you there :-)  But I still think one of us needs more coffee...

Yep Yep.

~Matt

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply | Threaded
Open this post in threaded view
|

RE: cygwin shell scripting - how to pass values from command line to ssh remote command

John Hardin
In reply to this post by Dave Korn
On Tue, 14 Oct 2008, Dave Korn wrote:

> Thorsten Kampe wrote on 14 October 2008 21:11:
>
>>> ssh qa@load01 'export MAX_MS=100; export OFFSET_MS=89900; export
>>> THREADS=4; export RAMP=1; export LOOPS=2; echo load01 ; cd
>>> /cygdrive/c/apps/bin ; pwd ; nohup ./start.sh  \& ; ps -efW | grep
>>> java ; exit '
>
>>  Also the "pwd".
>
>  Surely not, if it's a bit of debugging trace that lets ZW know that the
> directory exists and so the cd command succeeded and there must be something
> else wrong if the script doesn't execute?

Wouldn't something along the lines of

     cd /cygdrive/c/apps/bin && nohup ./start.sh &

be a lot safer if you were worried about the directory not being there?

Working that into the one-liner is left as an exercise for the student...
:)

--
  John Hardin KA7OHZ                    http://www.impsec.org/~jhardin/
  [hidden email]    FALaholic #11174     pgpk -a [hidden email]
  key: 0xB8732E79 -- 2D8C 34F4 6411 F507 136C  AF76 D822 E6E6 B873 2E79
-----------------------------------------------------------------------
   Health Care _is_ a right - the government has no business keeping
   you from getting it. But forcing somebody else to pay for your
   health care at gunpoint (i.e. through taxation) is _not_ a right.
-----------------------------------------------------------------------
  21 days until the Presidential Election

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply | Threaded
Open this post in threaded view
|

Re: cygwin shell scripting - how to pass values from command line to ssh remote command

Mark J. Reed
In reply to this post by zw
On Tue, Oct 14, 2008 at 2:59 PM, Z W wrote:
> Question:
> How can I pass arguments on command line with test.sh such that
> variables MAX_MS, OFFSET_MS, THREADS, RAMP and LOOPS
> could receive the argument parameters as opposed to hardcoding the numbers ?

This isn't really a cygwin question; there are lots of shellscripting
tutorials on the web, which I recommend you look for.  Meanwhile,
however, try this:

#!/bin/bash

function usage
{
    echo >&2 "Usage: $0 [-h|-l loops|-m max_ms|-o offset_ms|-r ramp|-t threads]"
    exit $1
}


# default values
loops=2
max_ms=100
offset_ms=89900
ramp=1
threads=4

while getopts 'hl:m:o:r:t:' opt; do
    echo opt=$opt
    case "$opt" in
        h) usage 0;;
        l) loops="$OPTARG";;
        m) max_ms="$OPTARG";;
        o) offset_ms="$OPTARG";;
        r) ramp="$OPTARG";;
        t) threads="$OPTARG";;
        ?) usage 1;;
    esac
done

shift $(( OPTIND - 1 ))

if (( $# )); then
    usage 1
fi

ssh localhost 'cd /cygdrive/c/apps/bin && LOOPS="'"$loops"'"
MAX_MS="'"$max_ms"'" OFFSET_MS="'"$offset_ms"'" RAMP="'"$ramp"'"
THREADS="'"$threads"'" nohup ./start.sh & ps -efW | grep java'

That ssh command should be all on one line.  Careful with the
sequences of quotation marks.  The idea is to build a command string
that includes literal quotation marks intermixed with the quoted
expansion of local variables.

Also note that if all you're looking to getting out of that ps |grep
java line is the PID, you can just use $!.

--
Mark J. Reed <[hidden email]>

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply | Threaded
Open this post in threaded view
|

Re: cygwin shell scripting - how to pass values from command line to ssh remote command

Thorsten Kampe
In reply to this post by zw
* Z W (Tue, 14 Oct 2008 14:15:28 -0700)
> Thanks for the response.
> I think you understood my question.

Yes.

> I like to pass parameters from tesh.sh as command line arguments and
> then pass them into the ssh remote command string. I'm just not sure
> of the syntax so that I could pass the paramter values to the remote
> command string from test.sh

I answered that one. I gave you the link to the ABSG where this question
is answered. All you have to do is to open the link and read the
passage. You don't expect to get spoonfed, do you?!

Thorsten


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply | Threaded
Open this post in threaded view
|

RE: cygwin shell scripting - how to pass values from command line to ssh remote command

Thorsten Kampe
In reply to this post by Dave Korn
* Dave Korn (Tue, 14 Oct 2008 23:41:05 +0100)

> Thorsten Kampe wrote on 14 October 2008 21:11:
> > * Z W (Tue, 14 Oct 2008 11:59:07 -0700)
> >> I'm have a shell script, test.sh below to run with cygwin in a Windows
> >> box.
> >>
> >> #!/usr/bin/sh
> >>
> >> ssh qa@load01 'export MAX_MS=100; export OFFSET_MS=89900; export
> >> THREADS=4; export RAMP=1; export LOOPS=2; echo load01 ; cd
> >> /cygdrive/c/apps/bin ; pwd ; nohup ./start.sh  \& ; ps -efW | grep
> >> java ; exit '
> >
> > That's not a shell script - it's just a single command.
>
> Uh, it's a shell script *containing* a single ssh command. Note the
> shebang. Also note the mention of the filename "test.sh", which is not
> to be confused with the script "start.sh" on the remote machine.

Putting a single command in a shell script is useless - because it's a
single command. You can call it a shell script because it has the
shebang but I call it as it is: it's just a single command.
 
> > The multiple export statements are superfluous.
>
> Surely not, if you want the definitions to be visible in the sub-shell
> that executes "start.sh"?

Something like "export MAX_MS=100 OFFSET_MS=89900 [...]" for instance?
 
> > The "cd" looks superfluous, too.
>
>   Surely not, unless /cygdrive/c/apps/bin is the qa user's home dir?

That's what a "path" is for, right?

    cd /cygdrive/c/apps/bin; ./start.sh
is in most cases equivalent to

    /cygdrive/c/apps/bin/start.sh
 
> >  Also the "pwd".
>
>   Surely not, if it's a bit of debugging trace that lets ZW know that the
> directory exists and so the cd command succeeded and there must be something
> else wrong if the script doesn't execute?

If you do "debugging" by cd'ing into a directory and then pwd'ing, then
you need a tank full of coffee...
 
> > Also the "exit".
>
> Ok, I'm with you there :-) But I still think one of us needs more
> coffee...

Guess, it's you...

Thorsten


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply | Threaded
Open this post in threaded view
|

RE: cygwin shell scripting - how to pass values from command line to ssh remote command

Gary R. Van Sickle
Thorsten Kampe wrote:

> * Dave Korn (Tue, 14 Oct 2008 23:41:05 +0100)
> > Thorsten Kampe wrote on 14 October 2008 21:11:
> > > * Z W (Tue, 14 Oct 2008 11:59:07 -0700)
> > >> I'm have a shell script, test.sh below to run with cygwin in a
> > >> Windows box.
> > >>
> > >> #!/usr/bin/sh
> > >>
> > >> ssh qa@load01 'export MAX_MS=100; export OFFSET_MS=89900; export
> > >> THREADS=4; export RAMP=1; export LOOPS=2; echo load01 ; cd
> > >> /cygdrive/c/apps/bin ; pwd ; nohup ./start.sh  \& ; ps
> -efW | grep
> > >> java ; exit '
> > >
> > > That's not a shell script - it's just a single command.
> >
> > Uh, it's a shell script *containing* a single ssh command. Note the
> > shebang. Also note the mention of the filename "test.sh",
> which is not
> > to be confused with the script "start.sh" on the remote machine.
>
> Putting a single command in a shell script is useless -
> because it's a single command. You can call it a shell script
> because it has the shebang but I call it as it is: it's just
> a single command.

...whahuh?  It's only useless until you have to type "ssh qa@load01 'export
MAX_MS=100; export OFFSET_MS=89900; export THREADS=4; export RAMP=1; export
LOOPS=2; echo load01 ; cd /cygdrive/c/apps/bin ; pwd ; nohup ./start.sh  \&
; ps -efW | grep java ; exit '" more than once.  Regardless, as I believe
others have already pointed out, the cygwin content of this thread is
tending towards zero.  I humbly suggest all parties involved take a break,
get their morning coffee/dewski/homebrew, and continue this discussion in a
forum more suited for answering this sort of generic unixoid question.

--
Gary R. Van Sickle
 


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/