LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script - Pass command options with equal sign (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-pass-command-options-with-equal-sign-704380/)

fabdog 02-13-2009 04:25 AM

Shell script - Pass command options with equal sign
 
Dear Forum,

I ran into a syntax problem when writing a shell script to execute commands.

I am assigning several variables and then want to execute the command (dmcopy). My code looks the following

*snip*
# Echo command to see if the variables are correct
echo command \"${evt_i}[option0=1,2,3][option1=1:2][option3 abc=150]\" ${path_i}_evts_Selected.fits option=image

# Execute command
command \"${evt_i}[option0=1,2,3][option1=1:2][option3 abc=150]\" ${path_i}_evts_Selected.fits option=image

When I look at the output, "echo" gives the correct output and the command if copied and pasted runs fine. But running the command from the script does not work and I think it is due to some special characters (equal sign?). How do I properly escape this and still get the command to run? I really tried for quite some time and could not figure it out.

Thanks for help,
Fab

colucix 02-13-2009 04:50 AM

How do you run the command from the script? Did you use eval? To find out which command it actually tries to execute, run the script using
Code:

bash -x name_of_the_script.sh
in this way you will get a trace of every command executed by the shell, so that you can see if there is an error in the command line. Post the output here if in doubt.

fabdog 02-13-2009 05:28 AM

Thanks for the tip.

I see now where the problem is. The output looks like the following:
command '"${evt_i}[option0=1,2,3][option1=1:2][option3' 'abc=150]"' {path_i}_evts_Selected.fits option=image

So there are two locations, where there should not be single quotes (') for the command to properly run.
What is the syntax so bash does not put the single quotes there?

Thanks.

colucix 02-13-2009 05:37 AM

The single quotes you see are just the escaped blank spaces, but they should be removed from the resulting command line before execution. My question is: do you use eval to run the resulting command? Can you post the actual code of the script to let us take a closer look?

fabdog 02-13-2009 05:50 AM

Ok, using eval solved my problem. :o
Sorry, I thought I could just pass the command without the eval...
Thanks for helping me, colucix!

colucix 02-13-2009 06:32 AM

You're welcome! :) eval forces the shell to do all the substitutions (variable, commands, parameter substitutions and so on) before actually execute the command.


All times are GMT -5. The time now is 04:51 AM.