Webodan
Webodan

Reputation: 101

Two questions (sorry if i'm hoarding too much)

1) I'm creating a program in bash that asks for a filename, then runs convert for such a filename after assigning it a variable $name with read. The directory it is searching in also has spaces, which I have sorted out the following way:

read -p "please input filename: " file
convert /path/to/that/file/with/bothersome\ spaces/here/$file.png /destination/path/to/that/file/with\ some/spaces/$file.pdf 

All is nice... until the filename itself happens to have spaces on it. I have attempted putting in the variable $file typing it $FILE , $"file" , "$file" and '$file' but none seem to work.

2) Is there any way I can do a goto or loop to a certain line in the script within an if statement? just so I don't have to rewrite those operations again (which seems pointless).

I'm making something of this sort:

echo "do this or that? [a, b] "
read input

if [[ $input == "a" ]]; then
{
app1
app2
app3
} &> /dev/null

(around this point I'd like to have something that allowed me to ask again if I want to loop this process or quit the script altogether.) and then...

else
{
app4
app5
app6
} &> /dev/null

(same as the previous parenthesis)

fi
exit

So yeah, that would be it. If both can't be answered, I can manage just getting answer to the second and re-running that script as needed (in case i need to perform it a second time or whatever).

Thank you very much for your help.

Edit: here is the original file

    #!/bin/bash

echo "Do this type of file or the other? [a, b] "
read input

if [[ $input == "A" || $input == "a" ]]; then
{
/usr/bin/app1 "/path/to/open" &
/usr/bin/app2 "/file/to/open.file" &
/usr/bin/app3 "/file/to/open.file" &
/usr/bin/app4 &
/usr/bin/app5 &
} &> /dev/null
while :; do #attempting what Barmar said. this is the particular part i want to loop from each section
echo "stuff opened"
read -p "please input filename: " file
convert /original/file/"$file".png /converted/file/"$file".pdf
echo "do another one? [Y,N] " stop
if [[ $stop == "n" || $stop == "N" ]]; then
break   
wait
else
{
/usr/bin/app6 "/different/file/to/open" &
/usr/bin/app7 "/different/file/to/open.file" &
/usr/bin/app8 "/different/file/to/open.file" &
/usr/bin/app9 &
/usr/bin/app10 &
} &> /dev/null
while :; do #attempting what Barmar said. this is the particular part i want to loop from each section
read -p "please input file:  " file
convert /original/file/"$file".png /converted/file/"$file".pdf
echo "do another one? [Y,N] " stop
if [[ $stop == "n" || $stop == "N" ]]; then
break   
wait
fi

doing it in this particular way it breaks the if statements.

What I want in essence is, that the first IF either takes the first block of apps, then the convert part, OR the second block of apps, then its convert part. And then another IF inside of both convert parts to loop either of them as needed.

Upvotes: 0

Views: 70

Answers (1)

Barmar
Barmar

Reputation: 780713

1) Double quotes should do it.

convert /path/to/that/file/with/bothersome\ spaces/here/"$file".png /destination/path/to/that/file/with\ some/spaces/"$file".pdf 

2) Use a while loop, and use break to stop the loop.

while :; do
    echo "do this or that? [a, b] "
    read input

    if [[ $input == "a" ]]; then
        {
        app1
        app2
        app3
        } &> /dev/null
        read -p "Stop? " stop
        if [[ $stop == "y" ]]; then
            break
        fi
    else
        {
        app4
        app5
        app6
        } &> /dev/null
    fi
done

Upvotes: 1

Related Questions