LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [SOLVED]Bash script single quote question (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bsolved%5Dbash-script-single-quote-question-890482/)

john_erlandsson 07-07-2011 03:04 PM

[SOLVED]Bash script single quote question
 
Hi!

I am trying to incorporate a gimp batch script into a bash script.

The gimp command have to use single quotes
Example:
Code:

gimp -i -b '(my-script "script-argument")' -b '(gimp-quit 0)'
It does not accept double quotes.

The problem is that i need to pass a bash variable as an argument to my gimp function.

I tried this:
Code:

gimpcmd="gimp -i -b '(batch-coelipse \"$filename\")' -b '(gimp-quit 0)'"
($gimpcmd)

But it throws an error as if the end of the variable $filename ended with a )

I also played around with it on the command line. It works if I echo the $gimpcmd, copy the output and run it as a command. But it fails if I try too run $gimpcmd as a command.

Does anyone know how to throw a variable in between two single quotes in a bash script?

//John

colucix 07-07-2011 03:19 PM

What about closing the single quotes immediately before the $filename variable?
Code:

gimp -i -b 'my-script '\"$filename\" -b '(gimp-quit 0)'

catkin 07-07-2011 03:26 PM

Probably the gimp command doesn't need single quotes but it needs to be made up of separate words:
  1. gimp
  2. -i
  3. -b
  4. (my-script file-name)
  5. -b
  6. (gimp-quit 0)
On the command line, enclosing (my-script file-name) in single quotes makes bash pass it to gimp as a single word.

Probably your script builds the command OK but when you try to run it as $gimpcmd (the enclosing () simply run it in a subshell), bash separates it into words at metacharacters which include "(", ")" and space.

What you are trying to do is difficult in bash. For your command it could probably be done by loading an array with individual words of the array providing the file name does not contain whitespace:
Code:

command[1]=gimp
...
command[4]="(my-script $file_name)")
...
"${command[@]}"

EDIT: hello colucix :)

john_erlandsson 07-07-2011 03:32 PM

Quote:

Originally Posted by colucix (Post 4408161)
What about closing the single quotes immediately before the $filename variable?
Code:

gimp -i -b 'my-script '\"$filename\" -b '(gimp-quit 0)'

That does not seem to work. But thanks for taking an interest.

//John

john_erlandsson 07-07-2011 03:44 PM

Code:

gimpcmd[1]=gimp
gimpcmd[2]=-i
gimpcmd[3]=-b
gimpcmd[4]="(batch-coelipse \"$filename\")"
gimpcmd[5]=-b
gimpcmd[6]="(gimp-quit 0)"
"${gimpcmd[@]}"

WORKS!

Thank you very much!


All times are GMT -5. The time now is 02:06 PM.