LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-18-2009, 11:21 AM   #1
Geneset
Member
 
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75

Rep: Reputation: 16
Question Bash command wrapper that can handle quotes?


Hi folks, I'm writing a wrapper for a fairly large selection of scripts, that are normally executed something like
Code:
$BIN/script -arg ARG -args "ARG1 ARG2"
but i want to do something else with the output, like=
Code:
$BIN/scriptX -arg ARG -args "ARG1 ARG2" | dosomething -name="scriptX"
Which works fine for one or two cases, but applying this accross the board i would rather do this in a globally defined function, like
Code:
function run() {
    SCRIPT=`basename $1`
    COMMAND=$*
    {
        $COMMAND
    } | dosomething -name=$SCRIPT


run $BIN/scriptX -arg ARG -args "ARG1 ARG2"
but when i check this out, its actually executing
Code:
$BIN/scriptX -arg ARG -args ARG1 ARG2 | dosomething -name=scriptX
As you can see, the double quotes are processed by bash and summarily ignored.
While i could go and escape every single quote in the codebase, i would rather not.
I've tried changing the execution format to eval, exec, weird subshells, using $@ instead of $*, and im out of ideas.

Does anyone know how this could be accomplished?

Last edited by Geneset; 06-18-2009 at 11:56 AM.
 
Old 06-18-2009, 09:22 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
"$@" gives all the arguments on the command line individually quoted "$1" "$2" ....
The demonstration below needs a file 'aa aa' in the current directory.
The file name is listed to sed which then changes all the a's to b's.
Code:
function run() {
  "$@" | sed 's/a/b/g'
}
run ls "aa aa"

# Output
bb bb

# If $@ is used instead of "$@" the output is
ls: aa: No such file or directory
ls: aa: No such file or directory
 
Old 06-18-2009, 10:45 PM   #3
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Question Add more quotes !

Quote:
...Which works fine for one or two cases, but applying this accross the board i would rather do this in a globally defined function, like
Code:
function run() {
    SCRIPT="`basename $1`"
    COMMAND=$*
    {
        $COMMAND
    } | dosomething -name="$SCRIPT"


run $BIN/scriptX -arg ARG -args "ARG1 ARG2"
but when i check this out, its actually executing
Is it working yet? If not, have you tried putting quotes around the items as I have bolded & quoted above ? In the first case, quoting just the $1 may work.

Sasha

Last edited by GrapefruiTgirl; 06-18-2009 at 10:51 PM.
 
Old 06-19-2009, 08:57 AM   #4
Geneset
Member
 
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75

Original Poster
Rep: Reputation: 16
Question

Better example: Convert args to separate lines

quotes
Code:
for arg in "$@"; do
echo $arg
done
./quotes "hello world"
Code:
hello world
./quotes hello world
Code:
hello
world


Expected behaviour
./quotes this is "a test" of quotations
Code:
this
is
"a test" 
of 
quotations
Achieved behaviour
./quotes this is "a test" of quotations
Code:
this
is
a test
of 
quotations
Basically i understand the difference between $* and $@, but i cant work out how to keep the quotes in the output.

Many thanks for your help and attention
G

Last edited by Geneset; 06-19-2009 at 08:57 AM. Reason: incorrect filename, may have been confusing
 
Old 06-19-2009, 07:21 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Try this with your example
Code:
command='./quotes this is "a test" of quotations'
eval $(echo "$command" | sed 's/"[^"]*"/'\''&'\''/g')

this
is
"a test"
of
quotations

# sed changes ./quotes this is "a test" of quotations
# into        ./quotes this is '"a test"' of quotations
 
Old 06-22-2009, 12:26 PM   #6
Geneset
Member
 
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75

Original Poster
Rep: Reputation: 16
Cool

Took a slightly different approach in the end; since I'm actually executing the arguments that i pass into this, i just double quoted each individual argument that gets passed, eg

Code:
while [ -n "$1" ]
do
  COMMAND="${COMMAND} \"$1\""
  shift
done
Which did the job, but thank you all for your kind help.
 
  


Reply

Tags
arguments, bash, quotes, wrapper


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
cd command doesn't work in script with more than two arguments in quotes vipinchauhan LinuxQuestions.org Member Intro 5 05-02-2009 12:20 AM
shell script: how to insert a command into quotes alekone Linux - General 4 01-12-2009 09:56 AM
linux command wrapper linux_guy Linux - Software 3 11-21-2007 12:01 PM
What is a command wrapper? johnsanty Programming 5 09-22-2006 03:19 PM
Quotes in command line expression afshin Programming 4 02-24-2003 08:27 AM


All times are GMT -5. The time now is 07:50 PM.

Main Menu
Advertisement
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