Any unix scripters here? Help?

  • Thread starter G.T
  • 4 comments
  • 635 views

G.T

11,462
United Kingdom
U.K
Paganisterr
Ak Paganister
Hi all

I have a quick question. I've just started learning the unix scripting language (and also the first language I've tried to learn significantly) and have a problem I can not for the life of me figure out. I presume it's a tiny syntax issue at the beginning of the loop but I swear it's layed out the same as examples on the 'net.

Basically I'm just creating a small loop which asks the user to input x amount of files and want it to loop that many times. The errors that appear are:

test2: line 3: x: command not found
test2: line 4: [: -lt: unary operator expected

Here's the code:
Code:
echo "Please enter the number times you want to check for a file:"
read OPTION
x = "0"
	while [ $x -le $OPTION ]
	do	
		echo "Enter filename"
		read FILENAME
		if [ -e $FILENAME ]
			then echo "File" $FILENAME "does exist."
		else
			echo "File" $FILENAME "does not exist."
		fi
	done

Any help? Please? :(
 
Last edited:
Oh my god... thanks SO much. I'm guessing no quote and spaces are needed to define a variable? Guess I should have known that... :dunce:
 
You're welcome :)

I can't remember if the spaces would cause an issue or not, but you definitely don't want to use the quotes to define an integer.
 
Indeed, it was the spaces causing the problem; one of the quirks of shell scripting; sometimes they're optional, sometimes they're required such as immediately after [ or before ].

Incidentally it would have worked with or without the quotes around the 0; quotes would have been required if the value had an imbedded space. For instance, these three statements would all work:

x=0
x="0"
s="foo bar"

but this would not:

s=foo bar
 

Latest Posts

Back