Operating System - HP-UX
1829104 Members
5009 Online
109986 Solutions
New Discussion

Inserting \ character in a string passed to another command

 
SOLVED
Go to solution
Rui Vilao
Regular Advisor

Inserting \ character in a string passed to another command

Greetings,

I have a shell script that receives a command line and executes a remote shell and passes the command string as argument.

Content of shell script:

cmd=$1
remsh host1 -l user1 -n "export DISPLAY=$DISPLAY;/opt/app/xpto.sh $cmd

This executes:

remsh host1 -l user1 -n "export DISPLAY=$DISPLAY;/opt/app/xpto.sh xpto (12)

When executing, I get the error:

I get the error

sh: Syntax error at line 1 : `(' is not expected.

I am trying to insert some \ character before the ( character but without success:

parsed_cmd=`echo $cmd | sed "s/(/\\0050/g" | sed "s/(/\(/g"`

Any help/suggestion is highly appreciated.
TIA.

Kind Regards,

Rui.
"We should never stop learning"_________ rui.vilao@rocketmail.com
9 REPLIES 9
Graham Cameron_1
Honored Contributor

Re: Inserting \ character in a string passed to another command

echo $cmd|sed -e "s/(/\\\\(/" -e "s/)/\\\\)/"
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Rui Vilao
Regular Advisor

Re: Inserting \ character in a string passed to another command

Graham,

Thanks for your suggestion.

The problem is if you have this within a script it will fail:

In script:

remsh host1 -l user1 -n "export DISPLAY=$DISPLAY;echo $parsed_cmd"

It returns:

ksh: syntax error at line 1 : `(' unexpected


TIA,

Rui.
"We should never stop learning"_________ rui.vilao@rocketmail.com
Mark Grant
Honored Contributor

Re: Inserting \ character in a string passed to another command

I'm still struggling to understand exactly what you are trying to do here but it is a Friday!

If I understand you correctly I would have though enclosing $cmd in " marks might help. You might need to enclose it with \" if being part of the remsh though.
Never preceed any demonstration with anything more predictive than "watch this"
Graham Cameron_1
Honored Contributor

Re: Inserting \ character in a string passed to another command

Looks ok to me...
--
#PS1="# "
# cmd="xpto (12)"
# parsed_cmd=$(echo $cmd|sed -e "s/(/\\\\(/" -e "s/)/\\\\)/")
# echo $parsed_cmd
xpto \(12\)
# remsh crimple "echo $parsed_cmd"
xpto (12)
#
--
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Rui Vilao
Regular Advisor

Re: Inserting \ character in a string passed to another command


You are right I was not clear enough...
This is my script:

cat cmd.sh
#! /usr/bin/ksh

cmd="COMMAND (12)"

parsed_cmd=`echo $cmd|sed -e "s/(/\\\\(/" -e "s/)/\\\\)/"`

remsh host1 -l user1 -n "export DISPLAY=$DISPLAY;echo $parsed_cmd"

... and it returns:

ksh: syntax error at line 1 : `(' unexpected

TIA,

Rui.
"We should never stop learning"_________ rui.vilao@rocketmail.com
Graham Cameron_1
Honored Contributor
Solution

Re: Inserting \ character in a string passed to another command

Rui
It's the backticks.
Change
--
parsed_cmd=`echo $cmd|sed -e "s/(/\\\\(/" -e "s/)/\\\\)/"`
-- to --
parsed_cmd=$(echo $cmd|sed -e "s/(/\\\\(/" -e "s/)/\\\\)/")
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Mark Grant
Honored Contributor

Re: Inserting \ character in a string passed to another command

I still think putting escaped quotation marks around $parsed_command should work.
Never preceed any demonstration with anything more predictive than "watch this"
john korterman
Honored Contributor

Re: Inserting \ character in a string passed to another command

Hi,
...and I think that the error resembles what you get when doing this:
$ cmd=COMMAND (12)
ksh: syntax error: `(' unexpected

In your first posting you say that the content of the shell script is:
cmd=$1

If this is in fact the case, try changing it to:
cmd="$"

regards,
John K.
it would be nice if you always got a second chance
Rui Vilao
Regular Advisor

Re: Inserting \ character in a string passed to another command


Many thanks to Graham Cameron.

Cheers,

Rui
"We should never stop learning"_________ rui.vilao@rocketmail.com