Operating System - Linux
1753328 Members
5055 Online
108792 Solutions
New Discussion юеВ

Add strings in a shell to run a command

 
SOLVED
Go to solution
Mario_88
Advisor

Add strings in a shell to run a command

Hello:

I want to make the following:

XXXX#cat script.sh
#!/usr/bin/sh -x
comando="grep -e uno -e dos prueba.txt"
comando="$comando |grep dos"
$comando

XXX#cat prueba.txt
uno
dos
tres
cuatro


instead executing the command
grep -e uno -e dos prueba.txt|grep dos

I obtain the following:

XXX#./script.sh
+ comando=grep -e uno -e dos prueba.txt
+ comando=grep -e uno -e dos prueba.txt |grep dos
+ grep -e uno -e dos prueba.txt |grep dos
prueba.txt:uno
prueba.txt:dos
grep: can't open |grep
grep: can't open dos

Any ideas?

2 REPLIES 2
James R. Ferguson
Acclaimed Contributor
Solution

Re: Add strings in a shell to run a command

Hi:

# cat script.sh
#!/usr/bin/sh -x
comando="grep -e uno -e dos prueba.txt"
comando="${comando}|grep dos"
eval ${comando}

# ./script.sh
/tmp/0227.sh
+ comando=grep -e uno -e dos prueba.txt
+ comando=grep -e uno -e dos prueba.txt|grep dos
+ eval grep -e uno -e dos prueba.txt|grep dos
+ grep dos
+ grep -e uno -e dos prueba.txt
dos

Regards!

...JRF...
Mario_88
Advisor

Re: Add strings in a shell to run a command

Thanks.