LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2006, 01:51 PM   #1
lenzj
LQ Newbie
 
Registered: Aug 2006
Posts: 5

Rep: Reputation: 0
Bash script - passing command as string


I'm trying to write a bash script function that will either execute a command or echo the command to the console depending on a variable test.
Code:
#!/bin/bash
execD () {
if [[ "$Debug" == "On" ]] 
    then
	echo "$*"
    else
	$*
    fi
}
Debug='On'
execD "echo \"Debug = $Debug\" | wc -w"
Debug='Off'
execD "echo \"Debug = $Debug\" | wc -w"
Output of above bash script.
Code:
echo "Debug = On" | wc -w
"Debug = Off" | wc -w
What I was intending the second line of output to be is the number 3, but instead bash assumes the pipe and wordcount command are part of the text to be echoed. I've found that if I replace the $* in the else section with /bin/bash -c "$*" then it works. It seems like a less elegant and less efficient way to do this however. Is there a way to do this without shelling out to another bash environment?

Thanks in advance,
Jason
 
Old 08-23-2006, 02:49 PM   #2
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Try replacing
Code:
        $*
with
Code:
        eval "$*"
 
Old 08-23-2006, 03:01 PM   #3
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
FYI: bash ignores special characters like | when they come from environment variables. This makes a good deal of sense. If this weren't the case, a person passing a parameter like \" to any otherwise working script would break that script, because that script is suddenly missing an ending quote.

eval will force bash to evaluate those characters regardless.
 
Old 08-24-2006, 11:36 AM   #4
lenzj
LQ Newbie
 
Registered: Aug 2006
Posts: 5

Original Poster
Rep: Reputation: 0
That worked like a charm. I missed the eval command somehow when I was looking into this. I guess the language engineers knew how to protect me from myself! Thank you both for the responses.
 
  


Reply



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
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 07:47 AM
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 AM.

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
Open Source Consulting | Domain Registration