sh syntax error unexpected token done

I'm getting the following error:

line 21: syntax error near unexpected token `done`
line 21: `done`

and I haven't been able to figure out why.

Here is my code

#!/bin/sh
if [ $# -lt 2 ]; then
	echo 'Usage: rename getexp/replStr [list-of-files]'
	exit 0
fi
arg = $1
shift
while [ $# -ge 1 ]; do
	if [ -w $1 ]; then
		newFile=$1 | sed "s/$arg/"
		mv $1 $newFile
	else
		echo "$1 does not exist as a writable file"
		exit 0
	fi
	shift
done

Sorry if there is some obvious mistake in my code, this is more or less my first shell script.

newFile=$1 | sed "s/$arg/"

I am not sure what exactly you are trying to do here but this is wrong.

perhapes you wanted to do

cat $1 | sed 's/$arg/replace/g'

cheers,
Devaraj Takhellambam

Also remove the spaces either side of the "=".

arg=$1

Should be

while [[ $# -ge 1 ]]; do

why is that?

In this context we just want to stop the while loop when a condition is reached. In this context either syntax would work. However using [[ ... ]] with while is a good habit because in some old shells any variables created within a [ ... ] loop are not available after the loop finishes.

The syntax within the brackets is governed by different rules (see man sh).
Coincidentally the syntax for some test commands is the same as the syntax for some conditional expressions (including -ge ).

[[ conditional expression ]]

[ test ]

Qualification: I have just tried both with an ancient /bin/sh and the my [[ .. -ge .. ]] syntax does not work at all!