LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   command in variable (https://www.linuxquestions.org/questions/linux-general-1/command-in-variable-821513/)

thsuk1 07-22-2010 10:29 AM

command in variable
 
I am making a pipeline of several commands.
This is a part of my shell script.
-----------------------------------
array=(
"mkdir src"
"ls -l | grep tk_ > src/list"
)
len=${#array[*]}

i=0;
while [ $i -lt $len ]; do
echo ${array[$i]}
if !(${array[$i]}); then exit 1; fi;
let i++
done
---------------------------------------
"mkdir src" is working well but "ls -l | grep tk_ > src/list" does not work.
These are error messages.
-----------------------------------------
ls: cannot access |: No such file or directory
ls: cannot access grep: No such file or directory
ls: cannot access tk_: No such file or directory
ls: cannot access >: No such file or directory
ls: cannot access src/list: No such file or directory
-----------------------------------------

How can I solve this problem?

Thanks.

smoker 07-22-2010 11:23 AM

If you want the commands to work properly, then enclose them like this :

Code:

array=(
$(mkdir src)
$(ls -l | grep tk_ > src/list)
)

However, you won't get the echo you are expecting.

thsuk1 07-22-2010 12:25 PM

Thanks but,
 
I think the commands in your code run at the moment when 'array' is created, not in the while loop.

Anyway thanks.


All times are GMT -5. The time now is 11:36 PM.