LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-05-2013, 06:15 AM   #1
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Rep: Reputation: 9
Calling one from script from another and passing parameters to it


Hi All,

I am making a script in which i am copying another script to another box and then doing ssh to that server and executing it over there. The issue i am having is that i am passing the parameters to the script however the parameters is not being received by the script on the serevr. Below is my script and its output

Code:
#!/bin/bash

#Author: Rohit Bhanot


## Variable defination and declration ##
IFS=$','
LIST="Adapter Info,Exit"
PS3="Please enter your choice : "
USAGE="bash call.sh  <server_name>"

 CALL()
        {
        OPT=$1
        LIST=$2
        echo "Value of opt is $OPT"
        for serv in $LIST
        do
              scp rohit.sh root@$serv:/tmp > /dev/null
#                ssh $serv 'bash' '/tmp/rohit.sh "$OPT"; rm -f /root/rohit.sh'
                ssh $serv -q '/tmp/rohit.sh "$OPT"; rm -f /root/rohit.sh'
       
        done
        }


if [ "$#" == "0" ];
then
        echo ""
        echo  "Missing parameters, this script requires atleast one parameter !!"
        echo -e "Correct usage is --  $USAGE \n"
        exit 1
else
        select i in $LIST
        do
        case $i in
           "Adapter Info" )
               VAR=$@
               CALL Adapter_Info $VAR ;;
                  "Exit" )
           #rm -f $0
          #exit 0
        CALL Exit ;;
        esac
        done
fi
unset IFS
Below is the script rohit.sh which i am calling

Code:
#!/bin/bash

echo "The value of param is $1"
And here is the output

Quote:
:/ #bash call serv2
1) Adapter Info
2) Exit
Please enter your choice : 2
Value of opt is Adapter_Info
The value of param is
#CTRL-C
So can anyone suggest me how to pass the source this script by passing arguements to it. I have tested this method and it worked when both the scripts are on local machine but i want to source the script which is on remote host and pass args to it.
 
Old 07-05-2013, 09:32 AM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
if PermitUserEnvironment is on you can pass $OPT to ssh process; otherwise you should write it into a file, transfer it to the remote host and load it on that side
 
Old 07-05-2013, 09:36 AM   #3
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
$OPT seems to be a well known value, so you can expand it before passing to ssh:
Code:
# you wrote:
ssh $serv -q '/tmp/rohit.sh "$OPT"; rm -f /root/rohit.sh'
# add quotes to evaluate value:
ssh $serv -q '/tmp/rohit.sh '"$OPT"'; rm -f /root/rohit.sh'
 
1 members found this post helpful.
Old 07-05-2013, 10:14 AM   #4
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 203

Rep: Reputation: 26
Please take a look at this. Hope it helps.
 
Old 07-05-2013, 10:25 AM   #5
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by Ygrex View Post
$OPT seems to be a well known value, so you can expand it before passing to ssh:
Code:
# you wrote:
ssh $serv -q '/tmp/rohit.sh "$OPT"; rm -f /root/rohit.sh'
# add quotes to evaluate value:
ssh $serv -q '/tmp/rohit.sh '"$OPT"'; rm -f /root/rohit.sh'
Hi...Thanks it worked, i figured it out that $OPT was already under ' '; so it was not being evaluated so we had to use another ' ' ; just like // in c/c++ in order to escape a single /. Correct me if i am understanding it wrong.

Last edited by Rohit_4739; 07-05-2013 at 10:33 AM.
 
Old 07-05-2013, 10:33 AM   #6
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 203

Rep: Reputation: 26
The double quotes here
Code:
"$OPT"
allow shell interpretation, so $OPT is replaced by its value.
Then the resulting string
Code:
'"$OPT"' becomes 'OPT_value'
is passed to ssh as Ygrex said:
Code:
ssh $serv -q '/tmp/rohit.sh 'OPT_value'; rm -f /root/rohit.sh'
 
1 members found this post helpful.
Old 07-08-2013, 03:25 AM   #7
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by gacanepa View Post
The double quotes here
Code:
"$OPT"
allow shell interpretation, so $OPT is replaced by its value.
Then the resulting string
Code:
'"$OPT"' becomes 'OPT_value'
is passed to ssh as Ygrex said:
Code:
ssh $serv -q '/tmp/rohit.sh 'OPT_value'; rm -f /root/rohit.sh'
Alright cool, thanks i got it now. The only thing i wasn't aware of was value is to be passed as '$VAR'. I am cleared. Thanks all for your help and time. Really appreciate it !!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing parameters to script -help flea89 Linux - Newbie 4 03-13-2010 01:47 PM
Calling perl script and passing variable from php script hosea Programming 5 10-21-2008 09:01 AM
Passing parameters to PERL script paddyjoy Programming 3 03-07-2006 12:04 PM
Passing parameters to a shell script neocookie Linux - General 5 10-20-2005 12:44 PM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 07:41 AM


All times are GMT -5. The time now is 09:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration