LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-23-2009, 04:21 PM   #1
shriyer
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Rep: Reputation: 0
Executing commands through shell variable


Hey there,
I am trying to pass a command as argument to a function.
The command shows up in $1.
Now I want to execute this command, but if I do a $1

./sample
"bla/blaprintf: warning: ignoring excess arguments, starting with `bla/bla'

The code is :

#!/bin/ksh
fn()
{
$1
}

fn 'printf "bla/bla bla/bla bla bla bla bla\\n"'


Any ideas how to get it work please ?
Thanks,
Shrikant
 
Old 07-23-2009, 06:27 PM   #2
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
It is an issue with shell substitution. I couldn't get it to work with a single argument, no matter what combination of quoting characters or escape characters I tried. it was breaking on the first space. So the error you got was discarding beginning with the second bla/bla. Playing around a bit, I got the following to work both in bash and ksh on my Mac.

Code:
#!/bin/bash
fn()
{
echo "==${1}==";
echo "==${2}==";
$1 "$2"
}

fn printf 'whataver thingy\n'
I added in the echo statements to get the feedback on what was going in to the function. Obviously, you can strip them out.
 
Old 07-23-2009, 07:03 PM   #3
thinknix
Member
 
Registered: Nov 2008
Distribution: Lots!
Posts: 178

Rep: Reputation: 58
Here is an easier solution:

Code:
#!/bin/sh
fn()
{
eval "$1" 
}

fn 'printf "bla/bla bla/bla bla bla bla bla\n"'
 
  


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
GUI for executing shell commands shishirkotkar Linux - Newbie 1 04-12-2008 09:36 AM
executing shell commands in PHP mohtasham1983 Programming 7 09-14-2007 11:04 AM
executing Bourne shell variable as command? ocicat Programming 3 07-31-2007 02:00 AM
Executing shell commands through PHP!!! Richtown Linux - Newbie 6 04-12-2006 02:57 PM
executing shell commands in c++ true_atlantis Programming 2 10-01-2004 04:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:38 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
Open Source Consulting | Domain Registration