Error in shell script if condition [duplicate]

2019-08-03 05:22发布

问题:

This question already has an answer here:

  • Bash if statement syntax error [duplicate] 3 answers

I didnt understand what he error here as iam new to shell scripting. Please help me

./bpr: line 8: syntax error near unexpected token `then'
./bpr: line 8: `    if[$(grep -o BPR $file | wc -l) == 1]; then '

回答1:

You need to add spaces between your [ ], try this:

if [ $(grep -o BPR $file | wc -l) == 1 ]; then


回答2:

You need a space around your condition:

if [ $(grep -o BPR $file | wc -l) == 1 ]; then
    ^                                 ^

1) If you are using bash, you can use the built-in [[ ..]] instead of test ([ ...]) command.

2) You can also avoid wc by using -c option of grep.

if [[ $(grep -c -o BPR $file) == 1 ]]; then


回答3:

Aside from your syntax errors, you don't need wc either if you don't care that there may be multiple occurrances of BPR in the file:

if grep -o BPR "$file"; then


回答4:

A couple of things:

  • You need spaces around [ and ].
  • You probably don't want to use [ and ].

The if statement runs the command you give it. If the command returns a zero, the then portion of the if statement is executed. If the command returns a non-zero, the else portion (if it exists) is executed.

Try this:

$ if ls some.file.name.that.does.not.exist
> then
>     echo "Hey, the file exists!"
> else
>     echo "Nope. File isn't there"
> fi

You'll get an output:

ls: some.file.name.that.does.not.exist: No such file or directory
Nope. File isn't there

That first statement, of course is the output of your ls command. The second one is the output from the if statement. The ls ran, but couldn't access that file (it doesn't exist) and returned e 1. That caused the else clause to execute.

Try this:

$ touch foo
$ if ls foo
>     echo "Hey, the file exists!"
> else
>     echo "Nope. File isn't there"
> fi

You'll get an output:

foo
Hey, the file exists!

Again the first line is your output from ls. Since the file exists, and is statable, ls returned a 0. This caused the if clause to execute, printing the second line.

What if I want to test whether or not a file exists?

You can use the test command:

$ if test -e foo
> then
>     echo "Hey, the file exists!"
> else
>     echo "Nope. File isn't there"
> fi

If the file foo exists, the test command returns a 0. That means the echo "Hey, the file exists!" will execute. If the file doesn't exist, test will return a 1, and the else clause will execute.

Now do this:

$ ls -il /bin/test /bin/[
10958 -rwxr-xr-x  2 root  wheel  18576 May 28 22:27 /bin/[
10958 -rwxr-xr-x  2 root  wheel  18576 May 28 22:27 /bin/test

That first number is the inode. If two matching files have the same inode, they are hard linked to each other. The [... ] are merely another name for the test command. The [ is an actual command. That's why you need spaces around it. You also see that if tests whether or not a command succeeds, and doesn't really do boolean checking (the exception is if you use double square brackets like [[ and ]] instead of [ and ]. These are built into the shell and not as builtin commands.)

What you probably want to do is:

if grep -q "BPR" "$file"
then
    echo "'BPR' is in '$file'"
fi

The -q flag tells grep to shut its yap. The grep command will return a 0 if the pattern you give it is in the file, and a non-zero (exact value doesn't matter -- as long as it isn't 0) if it can't.

Note I don't need [ ... ] because I am using the output of the grep command to see if I should execute the if clause of that statement.



回答5:

if you only need to know if the string matches without showing the actual match use

if grep -q 'anystring' file ; then


标签: bash shell unix