amio
March 11, 2009, 3:29am
1
Hi All,
Am copying mulitple files in a directory in names File0,File1,File2 etc.
I need to print separately the PID of these copies using File names.
for((i=0;i<5;i++))
do
mypid=`ps aux | awk '/File$i/ && !/awk/ { print $2 }'`
echo PID is $mypid
done
It printed nothing. Thinking "File$i" is not found. Am i right? Is there a way to find the PID of a process using variable substituition? If not, how it cud be achieved?
FYI,I got the PID of FIle0 or FIle1 if i use its name specifically like,
mypid=`ps aux | awk '/File0/ && !/awk/ { print $2 }'`
echo PID is $mypid
Thanks,
Amio
amio:
Hi All,
Am copying mulitple files in a directory in names File0,File1,File2 etc.
I need to print separately the PID of these copies using File names.
for((i=0;i<5;i++))
do
mypid=`ps aux | awk '/File$i/ && !/awk/ { print $2 }'`
echo PID is $mypid
done
It printed nothing. Thinking "File$i" is not found. Am i right? Is there a way to find the PID of a process using variable substituition? If not, how it cud be achieved?
FYI,I got the PID of FIle0 or FIle1 if i use its name specifically like,
mypid=`ps aux | awk '/File0/ && !/awk/ { print $2 }'`
echo PID is $mypid
Thanks,
Amio
try
ps aux | awk '/File$i/ && !/awk/ { print $2 }' & mypid=$!
echo mypid
To use the Shell variables, use "-v i.e. Variable" option in awk.
Do this:
fname=File$i
mypid=`ps aux | awk -v var="$fname" 'BEGIN /var/ && !/awk/ { print $2 }'`
Note, that your shell variable is fname...which the filename
and the corresponding awk variable is "var"
Hope this helps.
amio
March 12, 2009, 7:44am
4
Thanks friends for ur replies.
amio.
angad.makkar:
To use the Shell variables, use "-v i.e. Variable" option in awk.
Do this:
fname=File$i
mypid=`ps aux | awk -v var="$fname" 'BEGIN /var/ && !/awk/ { print $2 }'`
Note, that your shell variable is fname...which the filename
and the corresponding awk variable is "var"
Hope this helps.
Have you tried what you've suggested?
fname=File$i
mypid=`ps aux | awk -v var="$fname" '$0 ~ var && !/awk/ { print $2 }'`
amio
March 12, 2009, 8:00am
6
ps aux | awk '/File$i/ && !/awk/ { print $2 }' & mypid=$!
echo mypid
worked for me..
Thanks
amio
March 12, 2009, 8:03am
7
mypid=`ps -w aux | awk '/'"$myvar"'/ && !/awk/ { print $2 }'`
echo PID is $mypid
It also gives the PID using variable subtituition..
Thanks