LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: run command with parameters from variable (http://www.linuxquestions.org/questions/programming-9/bash-run-command-with-parameters-from-variable-595467/)

Roger Krowiak 10-29-2007 10:25 AM

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.

vadiml 10-29-2007 11:59 AM

Why don't you try:
Code:

get_list() {
IFS=$'\n'
t=`$1`
for entry in $t; do
  echo "NAME$3:  $entry" >> /file.$2
done
}


carl.waldbieser 10-29-2007 05:08 PM

Quote:

Originally Posted by Roger Krowiak (Post 2940958)
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
}


Roger Krowiak 10-30-2007 04:27 AM

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.


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