开发者

Which of my double and single quotes do I need to escape in my Bash script?

开发者 https://www.devze.com 2023-02-28 04:46 出处:网络
I have this line in a for loop in a bash script: gvpr -c \\\'N{pos=\\\"\\\"}\\\' ${FILE} | fdp -o data/${PAD}.${FILE} -Tdot -Nlabel=\'\"\"\' -Nshape=point -Gstart=$RA开发者_Go百科NDOM

I have this line in a for loop in a bash script:

gvpr -c \'N{pos=\"\"}\' ${FILE} | fdp -o data/${PAD}.${FILE} -Tdot -Nlabel='""' -Nshape=point -Gstart=$RA开发者_Go百科NDOM

I need the script to run (after expanding the variables) the following code:

gvpr -c 'N{pos=""}' 072.grafo1643.10.planar_drawn.dot | fdp -o data/1.test -Tdot -Nlabel="" -Nshape=point -Gstart=$RANDOM

I don't know how to format the script to get these commands to run. Any help is appreciated.

Here is the entirety of the script:

#!/bin/bash

if [ ! -d "data" ]; then
    mkdir data
fi

for FILE in `ls`
do
    if [ $FILE != process.sh ] && [ -f $FILE ]; then
        fdp $FILE -o data/p.$FILE -Nlabel=\"\" -Nshape=point
        for i in {1..100}
        do
            printf -v PAD "%03d" $i
            gvpr -c \'N{pos='""'}\' ${FILE} | fdp -o data/${PAD}.${FILE} -Tdot -Nlabel='""' -Nshape=point -Gstart=$RANDOM
        done
    fi
done


try not escaping some of your single quotes. I don't have gvpr or fdp so could not test. But you can give this a try and let me know the outcome

#!/bin/bash
mkdir data 2>/dev/null
for FILE in *
do
    if [ "$FILE" != process.sh ] && [ -f "$FILE" ]; then
        fdp "$FILE" -o data/"p.${FILE}" -Nlabel="" -Nshape=point
        for i in {1..100} # if you have Bash 4, you can do {001.100} 
        do
            printf -v PAD "%03d" $i
            gvpr -c 'N{pos=""}' "${FILE}" | fdp -o data/"${PAD}.${FILE}" -Tdot -Nlabel="" -Nshape=point -Gstart=$RANDOM
        done
    fi

done


Escaping the outer single quotes would definitely be wrong (as in \'N{pos=\"\"}\'). Single quotes don't evaluate variables (with $) inside of them. Thus only the single quote inside single quotes would have to be escaped.

gvpr -c 'N{pos=""}' "$FILE" | fdp -o "data/${PAD}.${FILE}" -Tdot -Nlabel="" -Nshape=point -Gstart="$RANDOM"

... should work from the syntactic point of view. Of course I don't know the commands gvpr and fdp to judge what they would expect.

Anyway, 'data/${PAD}.${FILE}' would not yield what you want, because you want the variables inside to expand. Thus the use of "data/${PAD}.${FILE}" in my example.

Oh, and your expanded example contradicts what you gave above "$FILE" and "data/${PAD}.${FILE}" don't quite match in the expanded form ;)

And assuming that your gvpr command wants the string quoted again, you'd have to go for:

gvpr -c '\'N{pos=""}\'' "$FILE" | fdp -o "data/${PAD}.${FILE}" -Tdot -Nlabel="" -Nshape=point -Gstart="$RANDOM"


If you want to keep the outer single quotes in 'N{pos=\"\"}', I'd use:

gvpr -c "'N{pos=\"\"}'" ${FILE} | fdp -o data/${PAD}.${FILE} -Tdot -Nlabel='""' -Nshape=point -Gstart=$RANDOM

Other option:

gvpr -c \''N{pos=""}'\' ${FILE} | fdp -o data/${PAD}.${FILE} -Tdot -Nlabel='""' -Nshape=point -Gstart=$RANDOM

(You don't need to escape double quotes in single-quoted strings.)

0

精彩评论

暂无评论...
验证码 换一张
取 消