Hi All
I have written the following script:
#!/bin/ksh
while read cmdline
do
echo `$cmdline`
pid="$cmdline"
done<commands.txt
===========
commands.txt contains:
ps -ef | grep abc | grep xyz |awk '{print $2};
My objective is to store the o/p of the command in a variable and do ceratin operations on it. It works fine if i just ls but not working with pipe...
agama
July 7, 2011, 10:54pm
2
If your snipit of code is cut directly from your file you are missing a quote
ps -ef | grep abc | grep xyz |awk '{print $2};'
If it was just a cut/paste error in your quote, this might also be a better way:
evail pid=\$( $cmdline )
The eval causes the contents of $cmdline to be expanded, and then the statement is executed which causes the command inside $( ... )
to be executed the the resulting output assigned to pid.
Hope this gets you moving in the right direction.
I tried bt its giving syntax error that '(' is expected i the line:
eval pid= \$( $cmdline )
agama
July 8, 2011, 12:14am
4
Space between the equal and backslant is the problem I think:
eval pid=\$( $cmdline )
There cannot be spaces before or after the equal sign in bash or ksh.
u cn just write: pid=eval "$cmdline" and it will work fine....
Thanks for giving the idea of eval... Its wrking fine nw
I'm curious why you don't just set the script executable and ./script > file ?
Actually i need to use the output in some other script as well as i m getting this input by running other script...
clx
July 13, 2011, 2:44am
8
If the operations are limited within the loop, you could just skip the "commands.txt" stuff.
ps -ef | grep abc | grep xyz |awk '{print $2} | while read pid
do
echo $pid
done
You might need to handle the variable scoping (subshell) if needed outside the loop.
eval "$cmdline" works.
Why there is need of special syntax eval pid=\?($cmdline)