Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Escape character for '

Status
Not open for further replies.

pvedaa

Programmer
Dec 24, 2008
1
GB
Hello experts
I am trying to write a shell script which will add ' ' to a unix variable and then pass it to oracle for inserting to a table.

I am running the script as root and I have to do a su -c .
The problem is the character ' is not recognised even after adding escape character \

Here is my script
----------------------
su - oracle -c '. $HOME/.profile; a=10;quote=`echo a | sed -e s/a/\'/g`;echo $quote;hosted=`hostname`; host=$quote$hosted$quote;

sqlplus scott/tiger <<EOF

insert into unix_box values($host,0,sysdate);
commit;
EOF
'
-----------------------
Error ->
test3.sh: 0403-057 Syntax error at line 1 : ``' is not matched.

-->I have a workaround to define quot in .profile - But that is the last option. Any pointer will be much appreciated. I am running this script on AIX 5.

Thanks & Regards
Vedaa
 
Hi

Inside a string delimited with single quotes ( ' ) nothing is expanded. So escaping anything, including a single quote, inside such string, will not work.

You forgot to specify what shell are you using, but you can have that single quote in [tt]bash[/tt] and [tt]mksh[/tt] like this :
Code:
quote=`echo -e "\\x27"`
So as far as I understand your code, this should work :
Code:
su - oracle -c  '. $HOME/.profile; echo -e "insert into unix_box values (\\x27`hostname`\\x27,0,sysdate);
commit;" | sqlplus scott/tiger'
Of course, not tested.

Feherke.
 
Another messy option is to terminate the single quotes, escape the single quote outside them, and then resume the single quotes again:

Code:
su - oracle -c  '. $HOME/.profile; echo "insert into unix_box values ('\'`hostname`\'',0,sysdate);
commit;" | sqlplus scott/tiger'

That should work in other shells too.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top