Syntax error in script with if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Syntax error in script with if statement
# 1  
Old 12-03-2010
Syntax error in script with if statement

I'm working on a function in a shell script I'm writing that will eventually take in and print out a list of vendor names and aliases (for my work) Here's the function in question:

Code:
addvendorandalias ()
{	 
echo
echo -n 'Would you like to create a new vendor list (y or n)? '
read answer
if [ answer='y' ] then
	echo 'Enter the name you would like to give the file: ' 
	read vendorfilename 	
	touch $vendorfilename 	
fi
echo 'Enter the vendorname: '
	read vendorname
}

when I try to run the script get this message:

-bash: vendorlist.txt: line 28: syntax error near unexpected token `fi'
-bash: vendorlist.txt: line 28: `fi'

I cannot for the life of me figure out what I've done wrong. According to the book I have I've constructed the if statement properly.
# 2  
Old 12-03-2010
Quote:
if [ answer='y' ]; then
echo 'Enter the name you would like to give the file: '
read vendorfilename
touch $vendorfilename
fi
You have to put a semicolon after the "if test" if you put "then" on the same line. or you can break "then" to a new line without adding a semicolon.
# 3  
Old 12-03-2010
At the same time answer should be used as $answer as below,

if [ $answer='y' ]; then

Hope this should work now!
Vasanth.
# 4  
Old 12-03-2010
I don't think either will work (save for the ;, as stated)!

Code:
$ cat Scr1
unset answer
[ answer='y' ] && echo YeeHaa1
[ $answer='y' ] && echo YeeHaa2

$ ./Scr1
YeeHaa1
YeeHaa2

Spaces either side of =, and quoting the "$answer", not the 'y' would be more useful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Syntax for if statement

I'm new to unix and the command line and am trying to learn different commands. I have a file (teledir.txt) that contains a name and phone number for 3 different people. I am writing a script that is to take two positional parameters and I typed out how it should behave: if <name and number... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

Help with if statement syntax in shell script

I want to make the file test condition a variable ($Prmshn in code below). My goal is to use something like the first three unsuccessful if statetments since the 'if #!/bin/ksh test_input() { Prmshn=${1} InFLNm=${2} ifReq="-$Prmshn $InFLNm" #the following three if statments fail: #if ] ;... (10 Replies)
Discussion started by: ms63707
10 Replies

3. Shell Programming and Scripting

[Solved] 0403-057 Syntax error for if statement

I am getting the following error when I am running a script in ksh when trying to execute an if statement comparing two numerical values tstmb.sh: 1.5321e+08: 0403-057 Syntax error Below is my code snippet. #!/bin/ksh set -x TODAY=$(date +%y%m%d) for file in $(ls -rt *.log | tail... (11 Replies)
Discussion started by: kiran1112
11 Replies

4. Shell Programming and Scripting

If statement Syntax error

Hi Can you please tell me what is wrong with this line: if && ]; then basically i want to check if x = 12 and F (Filename) end with 'g'. But it is throwing syntax error. (7 Replies)
Discussion started by: rtagarra
7 Replies

5. Shell Programming and Scripting

Help with if statement syntax

Hi, Is there a way to compare the value in if condition with a list of values. eg . if ] then echo "it's a mammal" else echo "its not" fi Thanks! (8 Replies)
Discussion started by: neil.k
8 Replies

6. Shell Programming and Scripting

Syntax error calling TCL script from shell script

hello everyone i am beginner on shell scripting .and i am working on my project work on ad hoc network i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem " syntax error near unexpected token `('... (1 Reply)
Discussion started by: marcoss90
1 Replies

7. UNIX for Dummies Questions & Answers

if statement code syntax

Hi, can someone please tell me what is wrong with this code? I just want it to check if the file size is greater than 2000kb. if Thanks! ---------- Post updated at 09:23 PM ---------- Previous update was at 09:21 PM ---------- I should probably post the full code: #!/bin/sh... (9 Replies)
Discussion started by: Bengel
9 Replies

8. Shell Programming and Scripting

syntax error on if statement

Hi, Can you please help me with this one: I write an "if" statement, something like this: if then echo "big file" else echo "normal file" and I get an error: `'then is not expected Thanks in advance (6 Replies)
Discussion started by: apenkov
6 Replies

9. Shell Programming and Scripting

Confirming Syntax - IF statement.

Hi All, Has been a while since I was last on, so I hope everyone has been doing fine. ;) Would like to know if the below IF statement syntax is correct for a ksh environment. It's been pushed into live as someone had deleted the development copy(!); not withstanding that, the statement now... (3 Replies)
Discussion started by: Cameron
3 Replies

10. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies
Login or Register to Ask a Question