Hi there
I am really struggling to place a value in a variable with the following loop, having run out of ideas please can someone point me in the right direction?
We first read two PIDs of a program (say calc) into an array, then we loop reading the details of those processes into a value which increments each time the loop runs.
I just can't get proc$val to equal the output from`ls -ld /proc/${array[$val]}`
One example of how I have tried (and failed is below)
val=0
for i in {1..${array
[*]}}
do
[ "proc$val"="`ls -ld /proc/${array[$val]}`" ]
((val++))
done
val=0
for i in ${array
[*]}
do
proc$val="$(ls -ld /proc/$i)" # Check if you really want this to be ls -ld.
((val++))
done
Hi thanks for the reply
just tried in ksh enviroment
$proc$val="$(ls -ld /proc/9467)"
Error
ksh: proc0=dr-xr-xr-x 8 nathan nathan 0 2012-04-27 08:09 /proc/9467: not found [ no such file or directory]
if I omit the proc$val ie
$proc0="$(ls -ld /proc/9467)"
it works!
Try creating an array instead of multiple variables like proc0,proc1..etc
val=0
for i in ${array
[*]}
do
proc_output[$val]="$(ls -ld /proc/$i)"
((val++))
done
Just to be clear, the reason that:
for i in {1..${array[*]}}
Doesn't work is that brace expansion is the first thing that the shell tries to process *before* it's converted the variable into an actual value. So in the shell's "mind" you're trying to say for everything between 1 and the word "${array...}" which obviously doesn't make any sense.
Back to my original issue, just forget the loop for now
just can't get proc$val
to equal the output from `ls -ld /proc/${array[$val]}`
I found the following did work
eval 'proc'$val=`'ls -ld /proc/{$array[$val]}'`
however I just want the time stamp from this string
eval 'proc'$val=`'ls -ld /proc/{$array[$val]} | awk ' {print $7}' '`
fails
awk: line 2: missing } near end of file
is this even possible? any ideas would be gratefullly recieved