Run command stored in variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Run command stored in variable
# 1  
Old 05-06-2014
Run command stored in variable

Hi,

I have a shell script where I have to connect t oracle database. I have to store the command in a variable and then run the command through the variable.The OS is solaris 10 and the shell is bash.pls help when I run the command it doesnot work.Thanks.

Code:
if [db=prod]
           CONNECT= "sqlplus ser/password@dbname"
           elif [db=test]
            CONNECT= "sqlplus ser/password@dbname"
            else
             exit
          fi
  'CONNECT'


Last edited by vbe; 05-06-2014 at 06:11 PM.. Reason: code tags please not HTML
# 2  
Old 05-06-2014
Code:
if [ "$db" = "$prod" ]
then
        CONNECT="sqlplus ser/password@dbname"
elif [ "$db" = "$test" ]
then
        CONNECT="sqlplus ser/password@dbname"
else
        exit
fi
$CONNECT


Last edited by wisecracker; 05-06-2014 at 06:28 PM.. Reason: Forgot the second "then"...
This User Gave Thanks to wisecracker For This Post:
# 3  
Old 05-06-2014
Hi thanks a lot . It worked , I have another problem as when I am redirecting the output to a file it doesn't connect to database.Pls suggest.thanks in advance

Code:
if [ "$db" = "$prod" ]
then
CONNECT="sqlplus ser/password@dbname <<END_SQL > logfile1
elif [ "$db" = "$test" ]
then
CONNECT="sqlplus ser/password@dbname"<<END_SQL > logfile2
else
exit
fi
$CONNECT]
sql statements
quit ;
END_SQL
exit


Last edited by Don Cragun; 05-07-2014 at 12:18 AM.. Reason: Change HTML tags to CODE tags.
# 4  
Old 05-06-2014
This question made me worry when I saw it, and that's why -- take it to its natural conclusion and it stops working. Shell doesn't work that way... It will split spaces, but it won't start over from scratch and assume that everything in your string is shell syntax. That'd be dangerous, and if you want dangerous, you have to use eval to forcefeed it to the shell. That'll ensure that a string containing <<EOF, >redirection, any accidental spaces and quotes, and/or `rm -Rf ~/` will be processed as the shell literally would instead of fixed string contents.

So the answer, the real answer, is "don't do that". Otherwise, 6 months down the road, you're going to try and read your own code and have no idea what its doing because commands are defined 3 if-statements deep, 9 pages away from where they're used, and run via recursive recursive eval. It'll be your job to figure out what elusive injected quote, brace, bracket, backtick, or dollar sign is making your generated-code-generator explode and fix it -- or more likely give it a proper rewrite into this:

Code:
if [ somecondition ]
then
        LOGFILE="logfile1"
        AUTH="ser/password@dbname"
else
        LOGFILE="logfile2"
        AUTH="ser/password@dbname"
fi

sqlplus $AUTH <<END_SQL > $LOGFILE
...
quit;
END_SQL

Don't use variables to store commands. The shell has to do a lot of doublethink to handle that and is left open to lots of bugs and mistakes you'd be better off avoiding. Use variables to store the parts of the command that vary.

Last edited by Corona688; 05-06-2014 at 07:18 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-06-2014
Thanks a lot. Not only you solved my problem but also showed me the better way to do it. Very very thankful. Not to mention your explanation was great.SmilieSmilie
This User Gave Thanks to Rossdba For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Resolve parameter value stored in a variable

Hi All, I have below variable, xyz=\$AI_XFR Now, if you will run the below command => echo $xyz $AI_XFR It is returning hardcoded string value. Whereas in environment, there is value in it. Like below: => echo $AI_XFR /home/aditya/sandbox/xfr/ I need to resolve this... (4 Replies)
Discussion started by: adgangwar
4 Replies

2. Red Hat

How to check values stored in variable?

hey, i have stored values of query like this val_2=$( sqlplus -s rte/rted1@rel75d1 << EOF set heading off select source_id from cvt_istats where istat_id > $val_1; exit EOF ) echo $val_2 now , val_2 has five values displayed like this 1 2 3 4 5 what i have to do is to check the... (1 Reply)
Discussion started by: ramsavi
1 Replies

3. Shell Programming and Scripting

Need to run Oracle stored procedure from UNIX env

Hi Everyone, I want to create a script where i need to run the oracle stored procedure from unix script and get the output(sequence number ) into a variable which i will pass in my datastage job. Below is my stored procedure:- DECLARE P_TRANSTYPE VARCHAR2(20); ... (4 Replies)
Discussion started by: prasson_ibm
4 Replies

4. Shell Programming and Scripting

Run stored procedure from shell script

Hello all, I am trying to run stored procrdure from shell script which takes one argument. And also I want to verify in the script whether the script executed successfully. However the Stored procedure is not running from shell script. Manually if I run it update the data in the table. Can... (29 Replies)
Discussion started by: PriyaSri
29 Replies

5. IP Networking

effects of bin file stored in nfs & run as daemon

Hi, Good Day, I had this question in my mind.Hope someone can give me his/her thought about it. Question: I had a binary files stored in the nfs system and mounted several workstation locally and running as deamon. Is there in effect to the networks or any problem me arise regarding network... (3 Replies)
Discussion started by: jao_madn
3 Replies

6. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

7. Shell Programming and Scripting

Blank as variable/output when run from command line

When I run this: PDHDURL=`/usr/bin/curl --silent http://www.phdcomics.com/comics.php | /usr/bin/grep -o http://www.phdcomics.com/comics/archive/.*.gif | head -1` echo -e "$PHDURL" It is totally blank. However, when I just run it from the terminal: /usr/bin/curl --silent... (2 Replies)
Discussion started by: killer54291
2 Replies

8. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

9. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

10. UNIX for Dummies Questions & Answers

can i exectue a command stored in a variable.it goes like this

hi the code i ve written is as follows: command="egrep ',100,|,200,' | wc -l" count =`cat trial.txt | $command` echo "$count" the contents of trial.txt is : 000,100,200,300, 001,200,100,201, 002,200,100,400, i need to get the count using the command stored in a variable and using... (2 Replies)
Discussion started by: Syms
2 Replies
Login or Register to Ask a Question