LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-28-2009, 02:51 AM   #1
badrinath_tcs
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Rep: Reputation: 0
using an shell script how to change a value in a file according to the user input


Hi Friends ,

I have a file called a.sql which I need to execute in a database and the script I am calling in a shell script .Before executing the script I have to change a value in the script according to the user input.How can I do this .

Eg...

A.sql file contains an sql query like this
select * from employee
where salary>Enter_salary
order by salary desc;


and My shll script is like this
#!bin/sh
Echo enter the salary
read salary

sqlplus user@databse

@path/a.sql
commit ;

exit;

exit 4;

When executed the script it asks for enter salary and if the user has input 5000 then the Enter_salary value in a.sql must be replaced by 5000.


Please help me on this how can i achieve the same ..


Thanks
Badrinath
 
Old 09-28-2009, 03:15 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
The echo command is "echo" not "Echo".

Are the following lines intended as input to the sqlplus command?
Code:
@path/a.sql
commit ;

exit;
If so then a shell "here document" may work (I do not know the sqlplus command but most work that way)
Code:
sqlplus user@databse <<EOF

@path/a.sql
commit ;

exit;
EOF
You have no place in what I think is the sqlplus input to put the salary. Is it in the path/a.sql file? If so, your script needs to edit path/a.sql, probably using sed and creating a temporary file.

Why are you exiting the script with return code 4? Non-zero is conventionally used to indicate an error.

It is easier to read you code if you put it in code tags.
 
Old 09-28-2009, 03:22 AM   #3
badrinath_tcs
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
THanks for ur reply

Hi Catkin,

Thanks for your reply and pointing some errors ..

The main problem I am facing is that I have to change the value of Enter_salary in a.sql file to 5000 i.e the input of the user in run time when executed the shell script ,,and which is stored in the shell variable..

Apart from this I don't have any problem ..Could you assist how can i do this ..
 
Old 09-28-2009, 03:32 AM   #4
badrinath_tcs
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
I used the sed in the below format but it did not worked ,It replaced the value Enter_salary in a.sql file to $salary not the value that is stored in salary variable

echo "Enter salary"
read salary
sed 's/Enter_salary/$salary/g a.sql >a1.sql
mv a1.sql a.sql

Last edited by badrinath_tcs; 09-28-2009 at 03:37 AM.
 
Old 09-28-2009, 03:59 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by badrinath_tcs View Post
I used the sed in the below format but it did not worked ,It replaced the value Enter_salary in a.sql file to $salary not the value that is stored in salary variable

echo "Enter salary"
read salary
sed 's/Enter_salary/$salary/g a.sql >a1.sql
mv a1.sql a.sql
The single quotes prevent variable substitution (and you missed the closing quote -- or maybe what you posted is not what you ran, because it would not have run). Please use copy and paste and code tags (that's a link so it's easy for you to read about code tags) so we can best see what you are doing.

Try this
Code:
sed "s/Enter_salary/$salary/g" a.sql > a1.sql
You probably want to keep the template a.sql file for next time so better to keep it rather than overwrite it. Even clearer call it a.template.sql and modify it into a.sql
Code:
sed "s/Enter_salary/$salary/g" a.template.sql > a.sql
 
Old 09-28-2009, 04:10 AM   #6
badrinath_tcs
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks a lot ..it is working now .
 
Old 09-28-2009, 07:29 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,232

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
Use the proper SQLPlus syntax: http://www.orafaq.com/wiki/SQL*Plus_..._SQL.2APlus.3F
 
  


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
How do i change to super user then revert back to ordinary user ,using shell script? wrapster Solaris / OpenSolaris 6 03-18-2009 03:37 AM
A way to open a window for user input in shell script? Anithen Linux - Software 10 08-24-2008 01:51 PM
SHell Script Help! When reading input from a user sfmadmax Linux - Newbie 3 12-12-2007 12:03 AM
help on shell script as a wrapper to launch default programs according to input file daveiro Programming 2 11-24-2005 04:20 PM
Shell script user input tuckermaddox Linux - Newbie 5 08-12-2004 03:14 AM


All times are GMT -5. The time now is 04:23 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration