Fedya
Fedya

Reputation: 13

Syntax error: word unexpected ( expecting "fi" )

i try do the script:

#!/bin/bash
cat test.txt
echo "Write del if you want delete or write save if you want save"
read s1
echo "Which symbol"
read s2
if [ $s1 = "del" ]
then
sed -i "/^$s2/d"
cat test.txt
if [ $s1 = "save" ]
then
echo "saved"
else
echo "Error"
fi cat test.txt

and i have error in - Syntax error: word unexpected ( expecting "fi" ) Help please!

Upvotes: 0

Views: 1817

Answers (2)

Bsquare ℬℬ
Bsquare ℬℬ

Reputation: 4487

There are several issues.

First, you must replace following lines:

sed -i "/^$s2/d"
cat test.txt

by

sed -i "/^$s2/d" test.txt

I guess you didn't want to have 2 if/then instructions, but an elsif one, so you should fix your script like this:

then
sed -i "/^$s2/d" test.txt
elif [ $s1 = "save" ]
then
echo "saved"

In addition, as improvements, I would recommend:

  • to put the name of the file in a dedicated variable, for better legibility and maintenability
  • to touch the file at the beginning of the script, to ensure it exists (even if empty)

Eventually, the script would look like this:

#!/bin/bash

filePath="test.txt"

[ ! -f "$filePath" ] && echo -e "ERROR: file $filePath not found. It is required for this script to work properly" >&2 && exit 1

cat "$filePath"
echo "Write del if you want delete or write save if you want save"
read s1
echo "Which symbol"
read s2

if [ "$s1" = "del" ]; then
  sed -i "/^$s2/d" "$filePath"
elif [ "$s1" = "save" ]; then
  echo "saved"
else
  echo "Error"
fi

cat "$filePath"

You can see that s1 is quoted in the 'if' to avoid issue if there are space characters.

Upvotes: 2

JNevill
JNevill

Reputation: 50219

Indenting code inside of an if block or loops can help catch issues like this. Every if needs a fi to end the block. Indenting your code:

#!/bin/bash
cat test.txt
echo "Write del if you want delete or write save if you want save"
read s1
echo "Which symbol"
read s2

if [ $s1 = "del" ]
then
    sed -i "/^$s2/d"
    cat test.txt

    if [ $s1 = "save" ]
    then
        echo "saved"
    else
        echo "Error"
    fi cat test.txt

You can see that the initial block is not closed with it's own fi. Also you have an oddball cat hanging out on the same line as your fi. That's not supposed to be there. Instead something like:

#!/bin/bash
cat test.txt
echo "Write del if you want delete or write save if you want save"
read s1
echo "Which symbol"
read s2

if [ $s1 = "del" ]
then
    sed -i "/^$s2/d"
    cat test.txt

    if [ $s1 = "save" ]
    then
        echo "saved"
    else
        echo "Error"
    fi 
    cat test.txt
fi

There's no guarantee that is going to do what you want, but it's syntactically correct.

I would HIGHLY suggest dumping this into shellcheck.net so it can point you in the right direction. Specifically the lack of double quotes in your tests that could cause some oddball behavior.


Based on @Aaron's comment you are probably looking for something like the following:

#!/bin/bash
cat test.txt
echo "Write del if you want delete or write save if you want save"
read s1
echo "Which symbol"
read s2

if [ "$s1" = "del" ]; then
    sed -i "/^$s2/d" test.txt    
elif [ "$s1" = "save" ]; then
    echo "saved"
else
    echo "Error"
fi 

cat test.txt

Upvotes: 4

Related Questions