Skip to main content
added 360 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

There is absolutely no reason to use an intermediate variable to store output of commands just to perform a test or output that data.

#!/bin/sh -

if grep -q -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
then
    echo 'Does not verify. Bad lines follow...'
    grep -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
fi

The regular expression has been corrected to delete the extra + after {7}. The if statement tests the exit status of grep directly. The grep command in the if statement, and later, use -x to force a whole-line match, and the first grep statement uses -q to stop at the first match without outputting anything.

The actual issue in your code is using $result unquoted, which causes the shell to split the value on spaces, tabs, and newlines, and then do filename globing on the generated words. The final set of words are then given as arguments to echo which prints them with spaces as delimiters.


If you are concerned about running grep twice, then run it only once and store the output of it to e.g. a temporary file:

#!/bin/sh -

tmpfile=$(mktemp)

if grep -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1" >"$tmpfile"
then
    echo 'Does not verify. Bad lines follow...'
    cat -- "$tmpfile"
fi

rm -f -- "$tmpfile"

There is absolutely no reason to use an intermediate variable to store output of commands just to perform a test or output that data.

#!/bin/sh -

if grep -q -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
then
    echo 'Does not verify. Bad lines follow...'
    grep -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
fi

The regular expression has been corrected to delete the extra + after {7}. The if statement tests the exit status of grep directly. The grep command in the if statement, and later, use -x to force a whole-line match, and the first grep statement uses -q to stop at the first match without outputting anything.

The actual issue in your code is using $result unquoted, which causes the shell to split the value on spaces, tabs, and newlines, and then do filename globing on the generated words. The final set of words are then given as arguments to echo which prints them with spaces as delimiters.

There is absolutely no reason to use an intermediate variable to store output of commands just to perform a test or output that data.

#!/bin/sh -

if grep -q -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
then
    echo 'Does not verify. Bad lines follow...'
    grep -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
fi

The regular expression has been corrected to delete the extra + after {7}. The if statement tests the exit status of grep directly. The grep command in the if statement, and later, use -x to force a whole-line match, and the first grep statement uses -q to stop at the first match without outputting anything.

The actual issue in your code is using $result unquoted, which causes the shell to split the value on spaces, tabs, and newlines, and then do filename globing on the generated words. The final set of words are then given as arguments to echo which prints them with spaces as delimiters.


If you are concerned about running grep twice, then run it only once and store the output of it to e.g. a temporary file:

#!/bin/sh -

tmpfile=$(mktemp)

if grep -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1" >"$tmpfile"
then
    echo 'Does not verify. Bad lines follow...'
    cat -- "$tmpfile"
fi

rm -f -- "$tmpfile"
added 4 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

There is absolutely no reason to ususe an intermediate variable to store output of commands just to perform a test or output that data.

#!/bin/sh -

if grep -q -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
then
    echo 'Does not verify. Bad lines follow...'
    grep -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
fi

The regular expression has been corrected to delete the extra + after {7}. The if statement tests the exit status of grep directly. The grep command in the if statement, and later, use -x to force a whole-line match, and the first grep statement uses -q to stop at the first match without outputting anything.

The actual issue in your code is using $result unquoted, which causes the shell to split the value on spaces, tabs, and newlines, and then do filename globing on the generated words. The final set of words are then given as arguments to echo which prints them with spaces as delimiters.

There is absolutely no reason to us intermediate variable to store output of commands just to perform a test or output that data.

#!/bin/sh -

if grep -q -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
then
    echo 'Does not verify. Bad lines follow...'
    grep -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
fi

The regular expression has been corrected to delete the extra + after {7}. The if statement tests the exit status of grep directly. The grep command in the if statement, and later, use -x to force a whole-line match, and the first grep statement uses -q to stop at the first match without outputting anything.

The actual issue in your code is using $result unquoted, which causes the shell to split the value on spaces, tabs, and newlines, and then do filename globing on the generated words. The final set of words are then given as arguments to echo which prints them with spaces as delimiters.

There is absolutely no reason to use an intermediate variable to store output of commands just to perform a test or output that data.

#!/bin/sh -

if grep -q -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
then
    echo 'Does not verify. Bad lines follow...'
    grep -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
fi

The regular expression has been corrected to delete the extra + after {7}. The if statement tests the exit status of grep directly. The grep command in the if statement, and later, use -x to force a whole-line match, and the first grep statement uses -q to stop at the first match without outputting anything.

The actual issue in your code is using $result unquoted, which causes the shell to split the value on spaces, tabs, and newlines, and then do filename globing on the generated words. The final set of words are then given as arguments to echo which prints them with spaces as delimiters.

added 8 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

There is absolutely no reason to us intermediate variable to store output of commands just to perform a test or output that data.

#!/bin/sh -

if grep -q -v -xEex -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
then
    echo 'Does not verify. Bad lines follow...'
    grep -v -xEex -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
fi

The regular expression has been corrected to delete the extra + after {7}. The if statement tests the exit status of grep directly. The grep command in the if statement, and later, use -x to force a whole-line match, and the first grep statement uses -q to stop at the first match without outputting anything.

The actual issue in your code is using $result unquoted, which causes the shell to split the value on spaces, tabs, and newlines, and then do filename globing on the generated words. The final set of words are then given as arguments to echo which prints them with spaces as delimiters.

There is absolutely no reason to us intermediate variable to store output of commands just to perform a test or output that data.

#!/bin/sh -

if grep -q -v -xEe '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
then
    echo 'Does not verify. Bad lines follow...'
    grep -v -xEe '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
fi

The regular expression has been corrected to delete the extra + after {7}. The if statement tests the exit status of grep directly. The grep command in the if statement, and later, use -x to force a whole-line match, and the first grep statement uses -q to stop at the first match without outputting anything.

The actual issue in your code is using $result unquoted, which causes the shell to split the value on spaces, tabs, and newlines, and then do filename globing on the generated words. The final set of words are then given as arguments to echo which prints them with spaces as delimiters.

There is absolutely no reason to us intermediate variable to store output of commands just to perform a test or output that data.

#!/bin/sh -

if grep -q -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
then
    echo 'Does not verify. Bad lines follow...'
    grep -v -x -E -e '[A-Z]{7}[[:space:]]+[A-Z]{3}' -- "$1"
fi

The regular expression has been corrected to delete the extra + after {7}. The if statement tests the exit status of grep directly. The grep command in the if statement, and later, use -x to force a whole-line match, and the first grep statement uses -q to stop at the first match without outputting anything.

The actual issue in your code is using $result unquoted, which causes the shell to split the value on spaces, tabs, and newlines, and then do filename globing on the generated words. The final set of words are then given as arguments to echo which prints them with spaces as delimiters.

That -- is needed with some grep implementations including GNU grep.
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading