[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: помогите с bash и условием



On Thu, Aug 22, 2013 at 07:43:46PM +0600, Sergey Korobitsin wrote:
> Peter Pentchev ☫ → To Debian-russian@lists.debian.org @ Thu, Aug 22, 2013 14:12 +0300
> 
> > 
> > [ ... ]
> >
> > Так что:
> > 
> > if echo "$TO_DISK_INFO" | fgrep -e "$SERIAL_SHORT" | fgrep -qe "$SERIALTO"; then
> > 	...
> > fi
> 
> Это всё, конечно, хорошо, но человек хотел, чтобы блок внутри if ... fi
> выполнился, если grep выполнится _неуспешно_. Простого способа, как это
> сделать, я сходу не нашел, если только не городить что-то типа:
> 
> if echo "$TO_DISK_INFO" | fgrep -e "$SERIAL_SHORT" | fgrep -qe "$SERIALTO" || true; then
> 	...
> fi
> 
> что, согласитесь, не очень читабельно.

Да... ошибка моя.  Я-то забыл, что он хочет отрицание.

Ну, if умеет отрицание:

if ! echo "$TO_DISK_INFO" | fgrep -e "$SERIAL_SHORT" | fgrep -qe "$SERIALTO"; then
	...
fi

В точности то, для чего создана the reserved word "!".  Из dash(1):

  If the reserved word ! does not precede the pipeline, the exit status
  is the exit status of the last com‐ mand specified in the pipeline.
  Otherwise, the exit status is the logical NOT of the exit status of the
  last command.  That is, if the last command returns zero, the exit
  status is 1; if the last command returns greater than zero, the exit
  status is zero.

Или из bash(1):

  The return status of a pipeline is the exit status of the last
  command, unless the pipefail  option  is enabled.   If  pipefail  is
  enabled, the pipeline's return status is the value of the last
  (rightmost) command to exit with a non-zero status, or zero if all
  commands exit  successfully.   If  the  reserved word  !  precedes a
  pipeline, the exit status of that pipeline is the logical negation of
  the exit sta‐ tus as described above.  The shell waits for all
  commands in the pipeline to terminate before returning a value.

Так что if ! echo ... ; then ... сделает нужное.

Всего лучшего,
Петр

-- 
Peter Pentchev	roam@ringlet.net roam@FreeBSD.org p.penchev@storpool.com
PGP key:	http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115  C354 651E EFB0 2527 DF13
This sentence no verb.

Attachment: signature.asc
Description: Digital signature


Reply to: