Search code examples
bashquotes

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


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=$RANDOM

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

Solution

  • 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