LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bach command wrapper that can handle quotes? (http://www.linuxquestions.org/questions/programming-9/bach-command-wrapper-that-can-handle-quotes-733921/)

Geneset 06-18-2009 11:21 AM

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?

Kenhelm 06-18-2009 09:22 PM

"$@" 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


GrapefruiTgirl 06-18-2009 10:45 PM

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

Geneset 06-19-2009 08:57 AM

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

Kenhelm 06-19-2009 07:21 PM

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


Geneset 06-22-2009 12:26 PM

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.


All times are GMT -5. The time now is 05:29 AM.