# if command -problem

**URL:** <https://community.unix.com/t/if-command-problem/154309>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [June 27, 2005, 6:14am UTC](https://community.unix.com/t/if-command-problem/154309 "2005-06-27T06:14:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasikaran](https://community.unix.com/letter_avatar/vasikaran/32/6_22734026db12803ebb3e059622ac3534.png) [@vasikaran](https://community.unix.com/u/vasikaran)\
**Post date:** [June 27, 2005, 6:14am UTC](https://community.unix.com/t/if-command-problem/154309/1 "2005-06-27T06:14:33Z")

</div>

Hi,

This is Vasikaran, I am just trying to run the if command using this test program, but while running , i am getting the syntax error as

"syntax error: unexpected end of file"

this is the test program  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

echo " enter number"  
read num  
echo "Enter the choice of you want to see "  
echo "1. 1message"  
echo "2. 2message"  
echo "3. 3message"  
read choice  
if (choice=1) then  
echo "This is reading the first choice"  
endif

Pls note : i am running this program in linux environment

Thanks for the help in advance.

Vasikaran

---

<div class="post-metadata">

**Author:** ![google](https://community.unix.com/user_avatar/community.unix.com/google/32/65_2.png) [@google](https://community.unix.com/u/google)\
**Post date:** [June 27, 2005, 7:51am UTC](https://community.unix.com/t/if-command-problem/154309/2 "2005-06-27T07:51:17Z")

</div>

Change the "if" line to this: Note there the spaces after the "[" and before the "]" are required. Also, to access the value of a variable in shell, prefix the variable with a "$" sign. Use "fi" to close the "if" statement.

```nohighlight
if [$choice -eq 1] then
echo "This is reading the first choice"
fi

```

You can clean up your code by using a "select case" which will build the menu for you. Here is the syntax (note that the ";;" must end every case.)

```nohighlight
select NUM in 1 2 3 quit
do
   case $NUM in
      1) echo "This is reading the first choice"
           ;;
      2) echo "This is reading the second choice"
           ;;
      3) echo "This is reading the third choice"
           ;;
    quit) echo "Goodbye"
           exit;;
   esac
done

```

Lastly, to fancy it up a bit, use the builtin variable PS3 to set your prompt string for the menu (Add it just before the "select case" statement)

```nohighlight
PS3="Make a Selection From the Following List"

```

---

<div class="post-metadata">

**Author:** ![rahul123\_libra](https://community.unix.com/letter_avatar/rahul123_libra/32/6_22734026db12803ebb3e059622ac3534.png) [@rahul123\_libra](https://community.unix.com/u/rahul123_libra)\
**Post date:** [June 27, 2005, 7:51am UTC](https://community.unix.com/t/if-command-problem/154309/3 "2005-06-27T07:51:29Z")

</div>

yes use spaces between [and]

u can also use

if  
....  
....  
...  
fi

---

<div class="post-metadata">

**Author:** ![vasikaran](https://community.unix.com/letter_avatar/vasikaran/32/6_22734026db12803ebb3e059622ac3534.png) [@vasikaran](https://community.unix.com/u/vasikaran)\
**Post date:** [June 27, 2005, 8:38am UTC](https://community.unix.com/t/if-command-problem/154309/4 "2005-06-27T08:38:29Z")

</div>

Thanks for the reply..

I have tried using if .........fi command but while trying i am getting this below mentioned error.. pls help

Ofcourse case statement is working, but i want to work it out with If statement..

"syntax error near unexpected token `fi'  
search1.sh: line 16: `fi'"

Thanks and Regards  
Vasikaran

---

<div class="post-metadata">

**Author:** ![rahul123\_libra](https://community.unix.com/letter_avatar/rahul123_libra/32/6_22734026db12803ebb3e059622ac3534.png) [@rahul123\_libra](https://community.unix.com/u/rahul123_libra)\
**Post date:** [June 27, 2005, 8:57am UTC](https://community.unix.com/t/if-command-problem/154309/5 "2005-06-27T08:57:15Z")

</div>

Can u plz paste the code of the if block

---

<div class="post-metadata">

**Author:** ![reborg](https://community.unix.com/user_avatar/community.unix.com/reborg/32/167_2.png) [@reborg](https://community.unix.com/u/reborg)\
**Post date:** [June 27, 2005, 11:37am UTC](https://community.unix.com/t/if-command-problem/154309/6 "2005-06-27T11:37:39Z")

</div>

Neither of the suggestions will work for the OP as he is using a csh base shell. I'm pretty rusty on csh but I think it should be something like

```nohighlight
echo " enter number"
read num
echo "Enter the choice of you want to see "
echo "1. 1message"
echo "2. 2message"
echo "3. 3message"
read choice
if ( $choice == 1 ) then
    echo "This is reading the first choice"
endif

```

---

<div class="post-metadata">

**Author:** ![vasikaran](https://community.unix.com/letter_avatar/vasikaran/32/6_22734026db12803ebb3e059622ac3534.png) [@vasikaran](https://community.unix.com/u/vasikaran)\
**Post date:** [June 28, 2005, 2:06am UTC](https://community.unix.com/t/if-command-problem/154309/7 "2005-06-28T02:06:31Z")

</div>

Actually i am trying the following code when in run this program in linux environment

echo " enter the mobile number "  
read mob  
echo "Enter the choice of log you want to see "  
echo "1. F.log"  
echo "2. A.log"  
echo "3. I.log"  
echo "4. R.log"  
echo "5. M.log"  
echo "6. A.log"  
read choice  
if [$choice == 1] then  
echo "This will give the contents present in f.log"  
grep $mob /data/logs/f.log | cut -f1,2,4,5,11 -d" "  
endif

the error message i am getting as

"line 17: syntax error: unexpected end of file"

---

<div class="post-metadata">

**Author:** ![kumariak](https://community.unix.com/letter_avatar/kumariak/32/6_22734026db12803ebb3e059622ac3534.png) [@kumariak](https://community.unix.com/u/kumariak)\
**Post date:** [June 28, 2005, 4:04am UTC](https://community.unix.com/t/if-command-problem/154309/8 "2005-06-28T04:04:50Z")

</div>

Try giving semicolon ( ; ) after the closing bracket  
if [\<condition\>]; then  
.....  
....

fi
