SORU
20 Ocak 2011, PERŞEMBE


Nasıl dize Bash kabuğu ile dosyasında olup olmadığını test etmek için?

Dizin adlarını içeren bir dosya var:

my_list.txt :

/tmp
/var/tmp

Eğer bu ismi dosya zaten varsa, bir dizin adı ekleyeceğim önce Bash kontrol etmek istiyorum.

CEVAP
20 Ocak 2011, PERŞEMBE


grep -Fxq "$FILENAME" my_list.txt

Çıkış durum değil, bu yüzden: eğer varsa, adı, 1 bulundu 0 (doğru) (yanlış)

if grep -Fxq "$FILENAME" my_list.txt
then
    # code if found
else
    # code if not found
fi

Burada the man page for grep ilgili bölümler:

grep [options] PATTERN [FILE...]

-F, --fixed-strings
       Interpret PATTERN as a list of fixed strings, separated by  new-
       lines, any of which is to be matched.

-x, --line-regexp
       Select only those matches that exactly match the whole line.

-q, --quiet, --silent
       Quiet; do not write anything to standard output.  Exit  immedi-
       ately  with  zero status if any match is found, even if an error
       was detected.  Also see the -s or --no-messages option.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BMG Rentals Property Management

    BMG Rentals

    23 Mayıs 2011
  • Gigawipf

    Gigawipf

    18 ŞUBAT 2010
  • trickycharms

    trickycharms

    6 Aralık 2013