Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.