LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-13-2009, 05:25 AM   #1
fabdog
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Rep: Reputation: 0
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
 
Old 02-13-2009, 05:50 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 02-13-2009, 06:28 AM   #3
fabdog
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 02-13-2009, 06:37 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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?
 
Old 02-13-2009, 06:50 AM   #5
fabdog
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Ok, using eval solved my problem.
Sorry, I thought I could just pass the command without the eval...
Thanks for helping me, colucix!
 
Old 02-13-2009, 07:32 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass command-line parameter to shell script? Kropotkin Linux - Newbie 12 07-25-2011 10:24 AM
Try to start Knoppix but can't type equal sign jcksrobbins Linux - General 11 09-04-2009 11:33 PM
Shell script help: How to pass a directory path to the script? dv502 Programming 13 09-10-2008 04:07 AM
pass php variables to shell script viveksnv Programming 1 03-05-2008 12:46 AM
PHP pass argument to shell script monzter Programming 2 08-14-2004 07:16 AM


All times are GMT -5. The time now is 10:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration