Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the output of a command in shell script

Tags:

bash

shell

I'm writing a very simple shell scripts that would looked at the log of all failed tests, and print out all the name of all files in the current directory that are in the log

 1  #! /bin/sh

 2  for file in *
 3  do
 4      echo "checking: $file"
 5      if [$(grep $file failed.txt -c) -ne 0]
 6      then
 7          echo "$file FAILED"
 8      fi
 9  done

When I execute it, I get this error:

line 6: [0: command not found

Does anyone have any idea why?

Thanks!!

like image 245
One Two Three Avatar asked May 14 '12 15:05

One Two Three


People also ask

How can I see the output of a shell script?

STDOUT - the standard output of the shell. By default, this is the screen. Most bash commands output data STDOUT to the console, which causes it to appear in the console. The data can be redirected to a file by attaching it to its contents using the command >> .

How do you grep the output of a command in shell script?

Using Grep to Filter the Output of a CommandA command's output can be filtered with grep through piping, and only the lines matching a given pattern will be printed on the terminal. You can also chain multiple pipes in on command. As you can see in the output above there is also a line containing the grep process.

How do you echo the output of a command?

The echo command writes text to standard output (stdout). The syntax of using the echo command is pretty straightforward: echo [OPTIONS] STRING... Some common usages of the echo command are piping shell variable to other commands, writing text to stdout in a shell script, and redirecting text to a file.

How can I determine whether a command executed successfully?

$ echo $? If a command succeeded successfully, the return value will be 0. If the return value is otherwise, then it didn't run as it's supposed to. Let's test it out.


3 Answers

[ is actually a command in linux (like bash or cat or grep).

$(grep $file failed.txt -c) is a command substitution which in your case evaluated to 0. Thus the line now reads [0 -ne 0], which is interpreted as run a program called [0 with arguments -ne 0].

What you should write instead is [ $(grep $file failed.txt -c) -ne 0 ]. Shell scripts require that there be spaces between the opening and closing square braces. Otherwise you change the command that is executed (the closing ] indicates that there are no more arguments to be read.

So now the command evaluates to [ 0 -ne 0 ]. You can try executing this in your shell to see what happens. [ exits with a value of 0 if the expression is true and 1 if it is false. You can see the exit value by echoing $? (the exit value of the last command to be run).

like image 84
Dunes Avatar answered Oct 06 '22 00:10

Dunes


Instead of testing the count, you can test the return code of grep:

if grep -q $file failed.txt &>/dev/null
like image 31
l0b0 Avatar answered Oct 06 '22 00:10

l0b0


The script can be

#!/bin/sh

for file in *; do
    echo "checking: $file"
    grep failed.txt $file && echo "$file FAILED"
done

or, as an one-liner in user shell command history:

for file in *; do { echo "checking: $file" && grep failed.txt $file && echo "$file FAILED"; done

in man grep

EXIT STATUS
The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2. (Note: POSIX error handling code should check for '2' or greater.)

like image 26
Rony Avatar answered Oct 06 '22 00:10

Rony