Grep issues with if statement in shell script

PJ Palomaki

I'm having an issue with tail & grep in shell script if statement. If I run tail -5 mylog.log | grep -c "Transferred: 0" in shell, it runs as it should, but in this shell script if statement:

# Parse log for results
if [ tail -1 "$LOGFILE" | grep -c "Failed" ] ; then
        RESULT=$(tail -1 "$LOGFILE")
elif [ tail -5 "$LOGFILE" | grep -c "Transferred:            0" ] ; then
        RESULT=""
else
        RESULT=$(tail -5 "$LOGFILE")
fi

I get

... [: missing `]'
grep: ]: No such file or directory

for both of the grep lines.

It's clearly to do with the closing ] being seen as part of the grep (and thus missing) but I'm using the correct whitespace so I can't figure out what's going on? What am I doing wrong here?

Thanks, PJ

Charles Duffy

Immediate Issue / Immediate Fix

Take out the brackets.

if tail -1 "$logfile" | grep -q "Failed" ; then

[ is not part of if syntax. Rather, it's a synonym for the command named test (which is typically both available as a shell builtin and an external binary, like /bin/test or /usr/bin/test).

Thus, your original code was running [ tail -1 "$logfile", and piping its result to grep -q "Failed" ]. The first [ was failing because it didn't see an ending ] -- which is mandatory when invoked by that name rather than with the name test -- and also because its parameters weren't a test it knew how to parse; and the second grep didn't know what the ] it was being piped meant, trying to find a file by that name.


Other Notes

Try to run external commands -- like tail -- as little as possible. There's a very significant startup cost.

Consider the following, which runs tail only once:

#!/bin/bash
#      ^^^^- IMPORTANT: bash, not /bin/sh

last_5_lines="$(tail -5 "$logfile")"
last_line="${last_5_lines##*$'\n'}"
if [[ $last_line = *Failed* ]]; then
  result=$last_line
elif [[ $last_5_lines =~ 'Transferred:'[[:space:]]+'0' ]]; then
  result=''
else
  result=$last_5_lines
fi

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using grep and if statement in Shell Script

From Dev

grep command on shell script

From Dev

Grep command in shell script

From Dev

Add "not" to if statement in shell script

From Dev

Add "not" to if statement in shell script

From Dev

Beginner Unix shell script issues

From Dev

Issues in getting $PWD in shell script

From Dev

Beginner Unix shell script issues

From Dev

Linux Shell script - grep . filename

From Dev

shell script, grep in combination with dialog

From Dev

shell script of grep single line in bash script

From Dev

Tilde (~/) not working on if then statement in Shell script

From Dev

nested case statement in shell script

From Dev

Combining relational operators with grep -q in shell IF statement?

From Dev

Having issues with Select statement in PHP script

From Dev

Shell script issues - pipe to variable, compare float

From Dev

Power Shell Script SMTP Email Issues

From Dev

IF-THEN Statement used with grep not fuctioning in UNIX Script

From Dev

Use literal (*) in grep pattern within shell script

From Dev

How to find this pattern with grep in a shell script?

From Dev

How use echo with grep in a Unix shell script?

From Dev

Use literal (*) in grep pattern within shell script

From Dev

Using grep in shell script to search for exact string

From Dev

Shell script to grep multiple occurrence of a word in logs

From Dev

Using grep with wc in shell script if block

From Dev

grep on a shell command in a pipeline scripted script

From Dev

shell :How to use UDV in this Grep structure without having issues?

From Dev

How to use goto statement in shell script

From Dev

How to use 2 variable for "for" statement in Shell Script

Related Related

HotTag

Archive