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

  • Austin Evans

    Austin Evans

    5 AĞUSTOS 2007
  • DigitalRev TV

    DigitalRev T

    30 AĞUSTOS 2007
  • Paulo Bautista

    Paulo Bautis

    21 Aralık 2008