Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding strings in shell script

Status
Not open for further replies.

mariocq

MIS
Apr 20, 2001
61
0
0
US
Hello:

I want to make the following
XXX:#cat prueba.txt
uno
dos
tres
cuatro

---------
#!/usr/bin/sh -x
comando="grep -e uno -e dos prueba.txt"
comando="$comando |grep dos"
$comando
----------------
but instead executing the comand
grep -e uno -e dos prubea.txt|grep dos
I obtain the following:
-----------------------------------
+ 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
 
for multiple grep strings, use egrep 'str1|str2'

also, you want to use the eval builtin, in order to recheck for special characters like pipe symbol which in your example is not visible to the shell until the variable comando is expanded, but at that stage, the pipe symbol has lost its special meaning.

Code:
#!/usr/bin/sh -x
comando="egrep 'uno|dos' prueba.txt"
comando="$comando |grep dos"
eval $comando


HTH,

p5wizard
 
Try this:
eval $comando

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top