LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 10-29-2007, 09:25 AM   #1
Roger Krowiak
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 33

Rep: Reputation: 15
Question BASH: run command with parameters from variable


Hi,

I'm trying to assign command with parameters to variable and later in function perform this command:


Code:
get_list "ps uwax" arg2 arg3      #call function with 3 args
.
.
.
get_list() {
IFS=$'\n'
for entry in $($1); do
  echo "NAME$3:  $entry" >> /file.$2
done
}
but the output is:

Code:
ps uwax: command not found
The problem is it doesn't recognize it as ps with arguments, but as a single command "ps uwax". Any ideas how to fix it? Thanks.
 
Old 10-29-2007, 10:59 AM   #2
vadiml
Member
 
Registered: Oct 2003
Posts: 42

Rep: Reputation: 19
Why don't you try:
Code:
get_list() {
IFS=$'\n'
t=`$1`
for entry in $t; do
  echo "NAME$3:  $entry" >> /file.$2
done
}
 
Old 10-29-2007, 04:08 PM   #3
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Quote:
Originally Posted by Roger Krowiak View Post
Hi,

I'm trying to assign command with parameters to variable and later in function perform this command:


Code:
get_list "ps uwax" arg2 arg3      #call function with 3 args
.
.
.
get_list() {
IFS=$'\n'
for entry in $($1); do
  echo "NAME$3:  $entry" >> /file.$2
done
}
but the output is:

Code:
ps uwax: command not found
The problem is it doesn't recognize it as ps with arguments, but as a single command "ps uwax". Any ideas how to fix it? Thanks.
I think part of the problem is that you set IFS to a newline. The shell seems to have have a problem with your command substituion once you do this. Why not do something like:
Code:
get_list() {
    $1 | 
        while read line; do
            echo "NAME$3:  $line" >> /file.$2
        done
}
 
Old 10-30-2007, 03:27 AM   #4
Roger Krowiak
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 33

Original Poster
Rep: Reputation: 15
vadiml&carl.waldbieser thank you for replies. The problem was indeed caused by IFS set to \n. I've applied your solution Carl and now it works as I want it. Learned something new, thank you very much.
 
  


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
[Bash] Command inside a variable yvovandoorn Programming 5 01-20-2007 06:48 PM
simple bash variable command itz2000 Programming 6 06-19-2006 08:24 PM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 03:12 PM
Install scripts do not run...BASH# no such command tmera Linux - General 1 12-17-2000 02:33 PM


All times are GMT -5. The time now is 07:53 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration