LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-10-2013, 06:27 AM   #1
cnitin
LQ Newbie
 
Registered: Mar 2012
Posts: 25

Rep: Reputation: Disabled
Preserving Variables over SSH.


Hi ,

I am writing a script which would first check if any database is up on the server and if any database is up it will export its name and login to database and fire few sql queries , all this will occur over ssh.

Below is my code :

Code:
/usr/bin/ssh -qn oracle@10.1.46.233 "
. $HOME/.bash_profile
ps -ef|grep pmon |grep -v grep > $HOME/dbname.txt
cd $HOME
#echo \"content of dbname\"
#cat $HOME/dbname.txt
echo \"-------------------------\"
while read inputline
        do
        echo \"----inside while loop----\"
        #echo $inputline
        echo $inputline > $HOME/test.txt
        input=`cat $HOME/test.txt |awk '{print $1}'`
        echo $input
done < $HOME/dbname.txt

#echo $dbname
#export ORACLE_SID=test
#sqlplus  \"/ as sysdba\" << EOF
#select name,open_mode from v\\\$database;
#exit;
#EOF
"
Now the problem is when i execute this script it creates dbname.txt file which contains the output of grep , but test.txt file blank ,even if i echo $inputline it shows nothing.

Below is the output.

Code:
[oracle@test ~]$ ./test.sh
content of dbname
oracle   12449     1  0 May06 ?        00:00:53 ora_pmon_test
-------------------------
----inside while loop----
As per me this issues seems to be because of variables not being able to preserve its content.

I am using RHEL 4.2 (Pretty old test server)
Please reply if any other information is required.

Please help.

Regards,
Nitin

Last edited by cnitin; 05-10-2013 at 06:39 AM.
 
Old 05-10-2013, 07:02 AM   #2
cnitin
LQ Newbie
 
Registered: Mar 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Hi Guys,

Please help
 
Old 05-10-2013, 07:47 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
I believe what you want to use is called a "here document" redirection.

From the bash manpage:
Code:
Here Documents
       This type of redirection instructs the shell to  read  input  from  the
       current source until a line containing only delimiter (with no trailing
       blanks) is seen.  All of the lines read up to that point are then  used
       as the standard input for a command.

       The format of here-documents is:

              <<[-]word
                      here-document
              delimiter

       No  parameter expansion, command substitution, arithmetic expansion, or
       pathname expansion is performed on word.  If any characters in word are
       quoted,  the  delimiter is the result of quote removal on word, and the
       lines in the here-document are not expanded.  If word is unquoted,  all
       lines  of  the here-document are subjected to parameter expansion, com‐
       mand substitution, and arithmetic expansion.  In the latter  case,  the
       character  sequence  \<newline> is ignored, and \ must be used to quote
       the characters \, $, and `.

       If the redirection operator is <<-, then all leading tab characters are
       stripped  from  input  lines  and  the line containing delimiter.  This
       allows here-documents within shell scripts to be indented in a  natural
       fashion.
The likely problem you are having is quoting...

Try replacing the command with:

Code:
usr/bin/ssh -qn oracle@10.1.46.233 <<ENDOFCOMMANDS
...
ENDOFCOMMANDS
And put your script where the "..." is.

Also - remember that things like ` will be interpreted on the local system and not on the remote. As are all substitutions of environment variables, so they will have to be escaped to prevent them from being interpreted on the wrong system.

Last edited by jpollard; 05-10-2013 at 07:50 AM.
 
1 members found this post helpful.
Old 05-10-2013, 07:49 AM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Maybe examine the done line.
 
Old 05-10-2013, 07:56 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
It can get a bit tricky doing all that (eg setting vars to be interpreted on remote etc) from the first host.
Easiest is to write the script, then scp across ( only need to do this when script is changed), then run it remotely and capture the output
Code:
ssh user@remote "/path/to/script.sh"
By default output appears back on first host, so you can re-direct to a file if reqd and eg email it.
Use ssh auth keys if this is automated.
 
Old 05-10-2013, 08:35 AM   #6
cnitin
LQ Newbie
 
Registered: Mar 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Cool

Thanks a lot JPollard.

I've changed the script to below and now it works ,

Code:
/usr/bin/ssh -qn oracle@10.1.46.233 "
. \$HOME/.bash_profile
ps -ef|grep pmon |grep -v grep > \$HOME/dbname.txt
cd \$HOME
echo \"content of dbname\"
cat \$HOME/dbname.txt
echo \"-------------------------\"
while read inputline
        do
        echo \"----inside while loop----\"
        echo \$inputline
        echo \$inputline > \$HOME/test.txt
        input=\`cat \$HOME/test.txt |awk '{print $1}'\`
        echo \$input
done < \$HOME/dbname.txt

#echo $dbname
#export ORACLE_SID=test
#sqlplus  \"/ as sysdba\" << EOF
#select name,open_mode from v\\\$database;
#exit;
#EOF
"
your analysis was correct abt escaping the variables.

Last edited by cnitin; 05-10-2013 at 08:37 AM.
 
  


Reply



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] variables with spaces in remote ssh genderbender Programming 5 11-22-2010 08:02 AM
Accessing environmental variables through ssh plesset Debian 1 02-25-2009 05:29 PM
SSH Displaying Environment Variables movitto Linux - Networking 5 08-12-2005 10:51 AM
retaining environment variables using ssh lightningdan Linux - Software 3 12-15-2004 05:43 PM
DISPLAY variables on X with SSH darklogik_org Slackware 15 03-29-2004 01:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:06 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
Open Source Consulting | Domain Registration