Вопрос по bash, if-statement – Как сделать «если не истинное состояние»?

254

Я хотел бы иметьecho команда выполняется когдаcat /etc/passwd | grep "sysa" неправда.

Что я делаю неправильно?

if ! [ $(cat /etc/passwd | grep "sysa") ]; then
        echo "ERROR - The user sysa could not be looked up"
        exit 2
fi
Error: User Rate Limit Exceeded!Error: User Rate Limit Exceeded[ ! EXPR ] acraig5075
Error: User Rate Limit Exceeded Charles Duffy

Ваш Ответ

5   ответов
1

Error: User Rate Limit Exceeded

if getent passwd "$username" >/dev/null; then
    printf 'User %s exists\n' "$username"
else
    printf 'User %s does not exist\n' "$username"
fi 

Error: User Rate Limit Exceeded


Error: User Rate Limit Exceededgrep -q "$username" /etc/passwdError: User Rate Limit Exceeded

Error: User Rate Limit ExceededpasswdError: User Rate Limit Exceeded

build:*:21:21:base and xenocara build:/var/empty:/bin/ksh

Error: User Rate Limit ExceededcaraError: User Rate Limit ExceededenocError: User Rate Limit Exceeded

Error: User Rate Limit ExceededgrepError: User Rate Limit Exceeded/etc/passwdError: User Rate Limit Exceeded

if cut -d ':' -f 1 /etc/passwd | grep -qxF "$username"; then
    # found
else
    # not found
fi

Error: User Rate Limit Exceeded:Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededbashError: User Rate Limit Exceeded
5

What am I doing wrong?

$(...)Error: User Rate Limit ExceededvalueError: User Rate Limit ExceededsysaError: User Rate Limit Exceededif ! [ $(true) ]; then echo false; fiError: User Rate Limit ExceededfalseError: User Rate Limit ExceededtrueError: User Rate Limit Exceededif ! grep ...; then.

Error: User Rate Limit Exceededcat /etc/passwd | grep "sysa" || echo errorError: User Rate Limit ExceededError: User Rate Limit Exceeded: grep "sysa" /etc/passwd || echo error.

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededgrep "sysa" /etc/passwd || echo error
Error: User Rate Limit Exceeded
1

Here is an answer by way of example:

Error: User Rate Limit ExceededcronError: User Rate Limit Exceeded

#!/bin/bash
#
if ! ping -c 1 SOLAR &>/dev/null
then
  echo "SUBJECT:  SOLAR is not responding to ping" | ssmtp [email protected]
  echo "SOLAR is not responding to ping" | ssmtp [email protected]
else
  echo "SOLAR is up"
fi
#
if ! ping -c 1 OUTSIDE &>/dev/null
then
  echo "SUBJECT:  OUTSIDE is not responding to ping" | ssmtp [email protected]
  echo "OUTSIDE is not responding to ping" | ssmtp [email protected]
else
  echo "OUTSIDE is up"
fi
#

Error: User Rate Limit ExceededError: User Rate Limit Exceeded


Error: User Rate Limit Exceeded&>/dev/nullError: User Rate Limit Exceeded/dev/null

Error: User Rate Limit Exceededexit statusError: User Rate Limit ExceededpingError: User Rate Limit Exceeded

Error: User Rate Limit ExceededcronError: User Rate Limit ExceededrootError: User Rate Limit Exceededsudo pingError: User Rate Limit ExceededcronError: User Rate Limit Exceeded

376

Error: User Rate Limit Exceeded

if ! grep -q sysa /etc/passwd ; then

grepError: User Rate Limit ExceededtrueError: User Rate Limit ExceededfalseError: User Rate Limit Exceeded

Error: User Rate Limit Exceededfalse == true.

ifError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded$( ... )Error: User Rate Limit Exceededecho $(cat /etc/passwd | grep "sysa")Error: User Rate Limit Exceeded-cError: User Rate Limit Exceededif ! [ $(grep -c "sysa" /etc/passwd) -eq 0 ] ; thenError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

if ! (( $(grep -c "sysa" /etc/passwd) == 0 )) ; then ...`

Error: User Rate Limit Exceeded==,<,>,>=,<=,%Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

if ! (( $(grep -c "sysa" /etc/passwd) )) ; then ....

Error: User Rate Limit Exceeded

if (( ! $(grep -c "sysa" /etc/passwd) )) ; then ....

Error: User Rate Limit ExceededawardError: User Rate Limit ExceededUseless Use of Cat (UUOC)Error: User Rate Limit ExceededgrepError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded(( $( cat file | grep regex | wc -l ) ? 0 : 1 ))
Error: User Rate Limit Exceededstackoverflow.com/a/30400327/912236] grep "^$user:" /etc/passwdError: User Rate Limit Exceededgrep -vError: User Rate Limit Exceeded-vError: User Rate Limit Exceeded
30

Error: User Rate Limit Exceeded

grep sysa /etc/passwd || {
    echo "ERROR - The user sysa could not be looked up"
    exit 2
}

Error: User Rate Limit Exceeded

$ grep sysa /etc/passwd || { echo "ERROR - The user sysa could not be looked up"; exit 2; }

Error: User Rate Limit Exceeded'grepError: User Rate Limit Exceeded-qError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded1>&2Error: User Rate Limit ExceededechoError: User Rate Limit Exceededstderr ?
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded!: not found

Похожие вопросы