Home > Software design >  Bash program that returns 1 on input and 0 otherwise
Bash program that returns 1 on input and 0 otherwise

Time:04-22

Does it exist a standard linux terminal program that when given text input (in standard in) returns a 1 and 0 if no text is provided? (The reverse logic would also be fine).

Example

echo hello | unknown_program  # returns 1
echo | unknown_program        # returns 0

CodePudding user response:

grep -q '.' will do this. . matches any character except newline. grep returns a 0 static code (success) if there are any matches, and 1 if there are no matches.

echo hello | grep -q '.'; echo $? # echoes 0
echo | grep -q '.'; echo $? # echoes 1
  • Related