1

I was making this shell script to become faster in plotting data with gnuplot. It is the first s.s that I write so I'm having some difficulties. All was good until I started to make the if statement. Now executing this returns this error:

prova2.sh: 53: prova2.sh: Syntax error: end of file unexpected (expecting "fi")

This is an example. What am I doing wrong here?

...
if ["$ANSWER" == "S"]
then
    ANSWER= set grid
else
    ANSWER=""
    echo "you want the plot whit boxes with lines or with points?"
    read HOW
    if ["$HOW" == "boxes"]
    then
        P= boxes
    else if ["$HOW" == "lines"]
    then
        P= lines
    else if ["$HOW" == "points"]
    then
        P= points
        gnuplot <<EOF
   ...
   fi
  • 1
    It says expecting "fi"... – don_crissti Oct 24 '15 at 21:17
  • [ and ] are keywords so must be separated (yes, both sides) from other text or punctuation with a space. – roaima Oct 24 '15 at 21:24
  • I've restructured your code so that it's indented properly. Perhaps you can see the if...fi issue yourself now. – roaima Oct 24 '15 at 21:27
  • else if should be written as elif in shell scripts – cas Oct 24 '15 at 21:30
  • Your assignment statements won't do what you think they will do. A=one is not the same as A= one. The first assigns the string one to the variable A. The second sets A empty and runs the command one. Review your own code as pasted here to identify which form you're using and determine whether it's really correct, or not. – roaima Oct 24 '15 at 21:31
2

Here's a corrected version of the if/then/elif/fi section of your script:

if [ "$ANSWER" == "S" ]
then
    ANSWER='set grid'
else 
    ANSWER=""
fi

echo "you want the plot with boxes, with lines, or with points?"
read HOW

if [ "$HOW" == "boxes" ]
then
    P=boxes
elif [ "$HOW" == "lines" ]
then
    P=lines
elif [ "$HOW" == "points" ]
then
    P=points
fi

Note the lack of a space after the = when setting ANSWER and P, and the use of elif rather than else if. i've also added a space between [ and the variables being tested [boxes or [S is not the same as [ boxes or [ S. The former tries to run, e.g., the command [boxes while the latter runs [ with an argument of boxes.

I've also put quotes in ANSWER='set grid', without them it will set ANSWER=set and then try to run a command called grid. Technically, there should be quotes on the P= lines too, but with only a single word (no spaces) after the = it doesn't need them.


Personally, i'd put the thens on the same lines as the if and elif because i think it makes it much more readable, but that doesn't matter - it's just a style preference, the code works the same either way.

if [ "$HOW" == "boxes" ] ; then
    P=boxes
elif [ "$HOW" == "lines" ] ; then
    P=lines
elif [ "$HOW" == "points" ] ; then
    P=points
fi
1

You have to end each if statement with fi in sh. Like this:

#!/bin/sh

BANANA=1

if [ $BANANA -eq 1 ]; then 
   echo "Banana was 1"
else
    echo "Oops"
fi
echo "This line will always be run"

That is, you need one fi for each if in your program, and the location of the fi determines where the code that is only sometimes executed ends.

  • Ok thanks. Excuse me for the stupid question. – Tarlo_x Oct 24 '15 at 21:33
  • @don_cristi: Fair enough! I'll correct it. – Dan Oct 24 '15 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.