Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I try to use ghostscript in a bash script on my linux ubuntu 13.04.

I have a problem to write ghostscript command in variable when the same command written directly, in console or into a script works perfectly.

I don't know how to solve this problem.

Could you help me ?

Example :

My script :

#!/bin/bash
# Script PDF_test_ghs.sh

echo "Direct test :"
gs -o PDF_temp_01.pdf -sDEVICE=pdfwrite -g7000x5600 -c "<</PageOffset [360 380]>> setpagedevice" -f PDF_initial.pdf

echo ""
echo "Test with variable :"
command='gs -o PDF_temp_02.pdf -sDEVICE=pdfwrite -g7000x5600 -c "<</PageOffset [360 380]>> setpagedevice" -f PDF_initial.pdf'
$command
exit 0;

and the result is :

ubuntu@ubun:~$ PDF_test_ghs.sh 
Direct test :
GPL Ghostscript 9.07 (2013-02-14)
Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 3.
Page 1
Page 2
Page 3

Test with variable :
GPL Ghostscript 9.07 (2013-02-14)
Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /undefined in "
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--
Dictionary stack:
   --dict:1167/1684(ro)(G)--   --dict:0/20(G)--   --dict:77/200(L)--
Current allocation mode is local
GPL Ghostscript 9.07: Unrecoverable error, exit code 1
ubuntu@ubun:~$

The first works and the second does not. I want to use the second. Why does it not work?

share|improve this question

1 Answer 1

Your statement fails because the double quotes inside the command are literal quotes, not syntactical. When you run $command, the shell interprets each whitespace-separated part as an argument, and your

"<</PageOffset [360 380]>> setpagedevice"

is not "glued" together by the double quotes anymore.

As a simple example, try:

command='touch "foo bar"'
$command

You'd expect a file called foo bar to be created, but instead you get "foo and bar". What you could try instead is:

eval $command

Here, the command would be evaluated as if it were typed in the shell. But don't expect this to work at all times.


You should read: I'm trying to put a command in a variable, but the complex cases always fail! — storing commands in variables is never recommended. Variables hold data, not commands. If you want to run commands repeatedly, you should use shell functions.

share|improve this answer
    
Thanks a lot, it works fine ! –  froggy Jan 19 '14 at 22:01
    
I'm writing a long script, with many options and the result is this command but i didn't want to make the problem too much complex while answering to be helped. –  froggy Jan 19 '14 at 22:03
    
You're welcome! Since you're new to Super User, note that you can accept an answer if it helped you by clicking the green checkmark next to it. –  slhck Jan 20 '14 at 7:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.