Re: bash for-loop - syntax error: operand expected



Hi Chris,

thanks for all your input.

Chris F.A. Johnson wrote:

if [ -x /bin/kill ]; then

Why use an external command? Bash has kill built in.

Ok, I will correct it. This was originally a C-Shell script which I had to
port to bash...

echo -e " - Killing process with PID ${PID}..."

What's the point of the non-standard -e option? You don't have any
escape sequences in the args.

It used to have escape sequences which I removed. But the -e will not hurt,
will it?

But why not:

pidlist=`getkill_list.sh`
echo "Killing $pidlist"
kill pidlist

I simplified the for-loop before posting. One of the processes takes a very
long time to terminate (around 20 seconds), so I have to wait until it
really disappears from the process list before I continue, this is done
inside of the for loop.

LINES is not a good variable to use; bash sets it to the number of
lines in the current terminal.

Ok, I will choose a better name.

# $LINES will now contain:
# /dev/mapper/vg01r5-lvol0 /spfs reiserfs rw,notail 0 0
# /spfs/mnt /spmnt none rw,bind 0 0

IFS=$'\n'
for i in ${LINES}; do # <-- problem occurs right here

There is no problem there.

Yes, there is, because this is line 471.

Avoid the non-portable == in a test expression.

Ok.

fuserflag=
grep '/spfs' /etc/mtab |
while read dev mntpt type opts
do
if [ "$mntpt" -eq "/spfs" ]
then
fuserflag=1
else
case $dev in
/spfs*) MPS_MOUNTED="${MPS_MOUNTED} $2" ;;
esac
fi
done

if [ -n "$fuserflag" ]
then
PIDS=`fuser -m /spfs | cut -d: -f2`
fi

Thanks for the suggestion.

The problem now is: when the script is run for the first time,
getkill_list.sh will return a number of PIDs and the script will kill
them as intended. Afterwards, when it greps the lines out of /etc/mtab,
the for loop will fail:

./myscript.sh:471: syntax error: operand expected (error token
is "/dev/mapper/vg01r5-lvol0 /spfs reiserfs rw,notail 0 0")

What is line 471?

The for loop, see above.

When I now start it immediately again, getkill_list.sh will return no
PIDs as they have already been killed previously, so no killing is done
this time and now, the for loop runs through fine. $LINES have exactly
the same contents in both cases.

Can you give me a hint what goes wrong here?

Put a set -x in your script and foloow the execution.

Already done, and I cannot figure out why a

for i in /dev/mapper/vg01r5-lvol0 /spfs reiserfs rw,notail 0
0\n /spfs/mnt /spmnt none rw,bind 0 0; do foo...; done

sometimes succeeds and sometimes not.

Clean up the code so that it is more easily readable (and more
efficient).

First of all I will have to get it to work. :-)

Regards, Frank
--
The ultimate performance killer for Unix environments:
tail -f /dev/zero > /dev/null
.



Relevant Pages