Bincika idan fayil ko babban fayil akwai ko babu (kuma ƙari) tare da IF madauki

Ban saka komai na ba Bash, kuma shine alokacin da nake shiga duniyar ban mamaki ta Python na manta kadan daga wannan abokin wanda ya taimaka min sosai 🙂

Anan na sanya wani abu ga masu sha'awar Bash, kamar yadda taken yake ... Zan nuna muku yadda ake bincika ko babu fayil ko babban fayil a cikin tsarin, kawai amfani da: if

Zamu kirkirar fayil din .sh kuma mu bashi izinin aiwatarwa, saboda wannan ya isa sanya umarnin mai zuwa a cikin m:

taɓa gwajin.sh && chmod + x gwajin.sh

Abin da wannan yayi shine ƙirƙirar fayil ɗin (taba gwajin.sh) kuma kuma ba ku izinin izini (chmod + x gwajin.sh)

To, bari mu rubuta mai zuwa a ciki:

if [ -d /opt/ ];
then
echo "Sí, sí existe."
else
echo "No, no existe"
fi

Menene wannan?

Mai sauki 😀

A layin farko - » idan [-d / ficewa]];

Anan muke tambaya, shin akwai / opt / directory? Ee shugabanci / babban fayil, saboda -d na nufin shugabanci ko babban fayil.
Sannan idan har akwai shi zamu buga a cikin m «Ee, akwai shi.»(Wannan layi 2 da 3)
In ba haka ba kuma / zaɓi / ba ya wanzu, to za mu buga «A'a babu shi»(Lines 4 da 5)
Sannan a layin karshe (lamba 6) zamu rufe idan kuma hakane.

Labari mai dangantaka:
Kafa haɗin cibiyar sadarwa tsakanin PC da injunan kama-da-wane Virtualbox

Yi gwajin, zai nuna muku cewa / ficewa / wanzu.

Idan kana son in nuna maka sauran rubutun (wanda babu shi) canza / ficewa / ga duk wani babban fayil da babu shi a cikin tsarin ka, misali: / asd /

Kamar yadda kuke gani, -d yana taimaka mana sanin idan akwai kundin adireshin, yanzu idan muka canza -d zuwa -f shine a bincika wanzuwar fayiloli (-d = kundin adireshi, -f = fayil / fayil)

Alal misali:

if [ -f /etc/hosts ];
then
echo "Sí, sí existe."
else
echo "No, no existe"
fi

Ga jerin zaɓuɓɓuka:

-d - »Duba ko akwai wani kundin adireshi

-f - »Bincika idan akwai takamaiman fayil

-w - »Bincika idan wani fayil yana da izinin izini

-x - »Duba idan wani file yana da izinin aiwatarwa.

Kuma da kyau babu sauran ƙari da yawa 🙂

Na san wannan zai zama da amfani sosai kuma mai ban sha'awa ga waɗanda suke son yin shirin a Bash 😉

gaisuwa


Abubuwan da ke cikin labarin suna bin ka'idodinmu na ka'idojin edita. Don yin rahoton kuskure danna a nan.

42 comments, bar naka

Bar tsokaci

Your email address ba za a buga. Bukata filayen suna alama da *

*

*

  1. Mai alhakin bayanan: Miguel Ángel Gatón
  2. Dalilin bayanan: Gudanar da SPAM, gudanar da sharhi.
  3. Halacci: Yarda da yarda
  4. Sadarwar bayanan: Ba za a sanar da wasu bayanan ga wasu kamfanoni ba sai ta hanyar wajibcin doka.
  5. Ajiye bayanai: Bayanin yanar gizo wanda Occentus Networks (EU) suka dauki nauyi
  6. Hakkoki: A kowane lokaci zaka iyakance, dawo da share bayanan ka.

  1.   Hugo m

    Wata hanyar da za a yi ba tare da idan… to .. a duba:

    test -f /etc/hosts && elcomando

    Ko kuma

    test ! -f /etc/hosts && echo "no se encontró el archivo"

    Da dai sauransu

    1.    KZKG ^ Gaara m

      Dama 😀
      … O_O… Shin kai Hugo ne, Hugo daga Cuba? … Ta amfani da Windows8, WTF !!!

      1.    Hugo m

        To haka ne, na saba da wannan OS din kuma idan har zan fada a fili, abin da kawai na gano yana da amfani shine mai sarrafa kwafin, in ba haka ba ni da kaina na same shi kwaro, musamman kayan aikin da yake hada su da kuma aikace-aikacen metro. Ina son shi kasa da Vista da 7 (duk da cewa ya fi wadannan inganci), wanda ni ma ban so su ba. Duk abin da nake amfani da shi don kunna NFS13 da Skyrim, amma wannan OS ɗin yana ɗaukar sarari da yawa wanda ya zama dole in share bangare na Linux [snif].

        Koyaya, don kwanciyar hankalinku, zan gaya muku cewa washegarin jiya na zazzage Arch ISO a watan Janairu kuma ina shirin ba shi ɗanɗano a cikin sigar x86-64 ɗin, da zarar na sami repo (kuna da shi?). Yanzu kamar yadda na gani yana amfani da kunshin da aka sanya hannu ta tsohuwa, yana ba ni ƙarfin gwiwa don nutsewa a ciki.

        Kuma mahaifina ma ya gaya mani cewa zai aiko mini da Sony Xperia, wanda nake so in yi amfani da shi don koyon wasu shirye-shiryen don Android, wanda ya kasance bege na jiran. Don haka ku kwantar da hankalinku, har yanzu ban tafi gefen duhu na karfi ba, buahahahaha 😉

  2.   patz m

    mutum gwajin

    1.    msx m

      Tabbatar.

  3.   RAW-Basic m

    Wenas! ..

    Zamu iya yin guda daya .. ..amma an wuce kundin adireshin a matsayin siga ..

    if [ -d $1 ];
    then
    echo "Sí, sí existe."
    else
    echo "No, no existe"
    fi

    Kuma muna kiran shi kamar haka:

    ./test.sh / ficewa

    Gaisuwa ..

  4.   saba m

    IF ba sake zagayowar ba xD

  5.   Gregory Swords m

    Ya dace da ni kamar safar hannu don rubutun da nake yi. Shin yana da amfani don amfani da katunan daji? Misali, don sanin ko akwai .mp4 to gudanar da wani abu akansu.

    idan [-f * .mp4];
    ...

    1.    RAW-Basic m

      Ee, yana da cikakken aiki ..

      Ko da ta hanyar sigogi .. "* .mp4"

      1.    RAW-Basic m

        Alal misali,

        if [ -f $1 ];
        then
        rm -r $1
        else
        ls
        fi

        Idan ka kirashi: ./ gwajin.sh * .mp4

        Idan ya sami fayiloli .mp4, zai share su ... ... amma ba ya lissafin fayilolin a cikin kundin adireshin ...

        1.    Gregory Swords m

          Godiya ga ra'ayi da tukwici! 🙂

        2.    Joaquin m

          Ka rasa f xD

          rm -rf $ 1

          Wannan ya fi ban sha'awa!

        3.    b1tblu3 m

          Abin al'ajabi !!!, Kowace rana nakan ƙara soyayya da Linux, an mayar da Windows zuwa wata kusurwa mai duhu na Hard Drive, wanda ake shirin sharewa don ba da hasken Linux.

          1.    RAW-Basic m

            Madalla! .. ..sannan ka dauki mataki na gaba .. ka fitar dashi ..

            6 watanni da suka wuce kimanin. Ina da GNU / Linux ne kawai a kan dukkan injina kuma bana bukatar kari .. ..ba W $ .. ko Wine ba .. ..kuma nafi farin ciki haka .. 😉

            Kuma ina cutar da kewayena .. tunda kowa ya ganni da Arch dina, da sauri, ingantacce kuma mai farin ciki .. ..kuma suna tambayata in girka Linux .. ..yeah! .. 😀

        4.    Inukaze m

          Hakanan zaka iya bincika su don ƙarin kari, zaku iya amfani da wani abu kamar

          #! / bin / bash
          Littafin Adireshi = $ HOME / Zazzagewa

          idan [$ Directory]; to
          amsa kuwwa "Ga jerin Nau'in Fayil:"
          amsa kuwwa "mp1, mp2, mp3, mp4, wav, flac, flaad, aud, midi"
          ls {* .mp ?, *. wav, *. fla, *. aud * .midi}
          fi

          ta amfani da {} tana baku damar saka ƙarin fadada a wannan yanayin, ta amfani da "?" , alama ce ta daji, wacce ke baka damar ɗaukar "Duk Wani Hali".

          Ina fatan zai taimaka muku

        5.    pedro m

          Ina ba da shawarar umarnin nemo [directory] [search_pattern] –exec [command] {} \

      2.    Carlos m

        Ba ya aiki, Na sanya waɗannan fayilolin a cikin kundin adireshin:
        AB20160420.TXT
        AB20160421.TXT
        AB20160422.TXT
        AB20160423.TXT
        AB20160424.TXT
        AB20160425.TXT
        bincika.sh

        #! / bin / bash
        TAFIYA = / tmp / GWAJI
        cd $ TAFIYA
        ls -la | awk '{bugawa »« $ 9}'
        idan [[-f AB.TXT]]; to
        amsa kuwwa »Shigar da IF idan akwai wani AB
        LITTAFI
        fi
        idan [-f AB20160422.TXT]; to
        amsa kuwwa "shigar da takamaiman fayil na AB"
        fi

        lokacin da na sanya [-f AB.TXT] yana bada kuskure
        lokacin da na sanya [[-f AB
        .TXT]] baya bada kuskure amma bai sami fayil din ba
        yana aiki ne kawai lokacin da na sanya takamaiman fayil, misali:
        idan [-f AB20160422.TXT]; to

        Ba zan iya samun sa don yin aiki tare da katunan daji ba * ko?

        1.    pedro m

          #! / usr / bin / env bash
          #.sauya = utf-8.

          KYAUTA

          EXIT_OK = 0
          FITARWA_BAD = 66

          BOLD = »\ 033 [1m»
          NC = »\ 033 [0m»
          RED = »\ 033 [31m»

          idan [! "$ 1"];
          sa'an nan
          amsa kuwwa "rasa siga"
          fita $ EXIT_BAD
          fi

          cd "$ 1"

          fayiloli = »ls -h1 $ 1»

          na fayil a $ ($ fayiloli); yi
          amsa kuwwa -e "za ku iya yin komai tare da $ BOLD $ fayil $ nc nan"
          aikata

          fita $ EXIT_OK

  6.   msx m

    @KZ, ba da ƙarin bayani game da wannan: «kuma shine zan shiga kyakkyawar duniyar Python»!

    1.    KZKG ^ Gaara m

      HAHAHAHA nah ... kadan kadan 🙂
      Har yanzu ni sabon shiga ne zuwa Python, da kyar na sami lokaci don sadaukar da awowi da awanni a gareshi, don haka a hankali nake, a hankali amma tabbas a cikin ilimina 😀

  7.   aureban m

    Na gode sosai 😀 jiya kawai ina tunanin yadda ake wannan.

  8.   ReichSk m

    wannan tare da jinya yana da kyau 🙂

  9.   jazo m

    a cikin kalma 1… KYAU!
    Na daɗe ina kammala rubutun don yin abubuwa daga Intanet, amma ina da matsalar yadda ta zazzage fayilolin da ta buƙaci 1 kuma ba tare da na iya tabbatar da kasancewar 1ro ba.
    Kyakkyawan tuto.

  10.   zulan m

    Gudummawa mai yawa, anan daga Chile ana yaba.
    Kowace rana LINUX yana ba ni mamaki,
    Longer Free Software MIERDAAAA.

  11.   david m

    bauta wa
    duba idan fayil daga shugabanci ne

  12.   Grovher m

    Idan ina son duba wanzuwar kowane fayil a cikin hanyar «/ tmp / masu karɓa», aiwatar da kowane umarni, yaya abin zai kasance?

    Zan yi matukar godiya

    gaisuwa

  13.   Juan m

    Masoyi, kuma don ƙayyade ta IF idan fayil ɗin yana cike da jama'a ???. Nayi wannan saboda lokacin da kuke cike fayil ɗin don watsawa, zai iya ɗauka koda da rabin bayanan ne.

    Gaisuwa da godiya saboda amsawarku.

  14.   Inukaze m

    To ina ga yakamata ku ƙara misali, kamar "Duba cewa babu" "fayil" ko "kundin adireshi" kamar

    don bincika cewa BA ya wanzu, misali amfani da m->

    Littafin Adireshi

    #! / bin / bash
    Littafin Adireshi = »$ HOME / Littafin Gwaji»
    Fayil = »$ HOME / Fayil din Gwaji»

    idan [! -d "$ Directory"]; to
    amsa kuwwa "Babu $ $"
    amsa kuwwa "Kirkira ->" Adireshin $ "
    mkdir "$ Directory"
    kuma
    amsa kuwwa "Directory" $ Directory "ya riga ya wanzu
    fi

    idan [! -d "$ Fayil"]; to
    amsa kuwwa "Fayil $ babu"
    amsa kuwwa "Kirkira ->" Fayil $ "
    taba "$ Fayil"
    kuma
    amsa kuwwa "Fayil" $ Fayil "ya wanzu
    fi

    1.    Katekyo m

      lokacin da zakayi bincike na mkdir daga baya idan tashar na iya kirkirar ta saboda tana iya karya kwallayen idan folda ce kamar tushe ko system hhah

  15.   Julius vinachi m

    Gaisuwa KZKG ^ Gaara tambaya kamar yadda zan yi don amfani da curl. Ina so in san yadda zan yi daga na'ura mai kwakwalwa zuwa ping url don ganin ko akwai shi misali http://portal.com/directorio/archivo1.php Inda file1 zai adana IPs ɗin ziyarar, don sa ido ne ga ƙungiyar da nake buƙatar yin wannan ƙwanƙolin kowane rabin sa'a don ina sane da IP ɗin jama'a tunda yana canzawa lokaci-lokaci kuma ba zan iya haɗuwa da ssh ba .

    Gaisuwa na gode a gaba Allah ya saka da alheri.

    1.    KZKG ^ Gaara m

      Hi yadda ake tafiya

      Da farko, dole ne ku ce cewa canjin IP (alal misali) shine abun cikin wannan fayil ɗin, don wannan dole ne ku:
      1. Zazzage fayil ɗin
      2. Duba abubuwan da ke ciki kuma sami IP daga can
      3. Wannan IP shine mai canzawa

      Alal misali:
      Anan mun zazzage fayil - »wget http://portal.com/directorio/archivo1.php -ko fayil.html
      Sannan mu sanya maɓallin canji abin da ke ciki: - »: $ {IP: = 'cat file.html`}
      .. ana zaton cewa abun cikin wannan fayil ɗin yana da IP kawai, idan ba haka ba, dole ne ku yi aiki tare da grep, awk ko kai.

      Da zarar kun riga kun sami IP azaman mai canzawa, sauran abin da ya fi sauƙi a duniya 🙂

      Idan kana so ka rubuto min email (kzkggaara [at] desdelinux [dot] net) cewa zan yi farin cikin taimaka maka

      1.    Julius vinachi m

        Na gode sosai kzkggaara sosai godiya.

  16.   Natalia m

    Barka dai, ban fahimta sosai game da sarrafa kwamfuta ba kuma ban sani ba idan wannan post ɗin yana da alaƙa da matsalar da nake da ita.

    Nayi tsokaci idan har kun karanta ni, duk da cewa sakon daga shekara daya da ta gabata ne:
    Ya zama cewa na ƙirƙiri babban fayil a kan tebur ɗina don adana wasu bidiyo. Da kyau, na share waɗannan bidiyon saboda ban buƙata su ba kuma yanzu wannan babban fayil ɗin yana gaya mani cewa babu shi (duk da cewa ana gani akan tebur na).

    - Wannan ita ce matsala ta farko da nake da ita:
    Ba zan iya share fayil ɗin ba.

    - Matsala ta biyu kuma ina tsammanin mafi mahimmanci:
    A cikin yunƙurin share babban fayil ɗin, na sanya babban fayil ɗin HOTO. Yanzu idan na bude folda da "babu shi" babu komai a ciki kuma HOTUNAN basa nan.

    Ta yaya zan iya dawo da hotunan? Na bayar da zabin a cikin "Nuna boyayyun takardu" amma na PHOTOS din bai bayyana ba.

    Ina fatan zaku iya sanin yadda ake warware ta.
    Gode.

    1.    valdo m

      Akwai masu amfani guda biyu waɗanda sune manyan su: tushen da your_user (misali a matsayin misali). Dukansu suna da manyan fayiloli na sirri. your_user a / gida da tushe a / (tushen kundin adireshi). Ta danna dama kan babban fayil din ga wanda nasa yake. Idan yana da tushe tare da mai sarrafa fayil dinka ta amfani da sudo ko gksu ko kdesu gwargwadon yanayin da kake amfani da shi sai kaje kundin adireshi -> tushen -> Desktop kuma da alama zaka samu hotunan kuma zaka iya share jakar. Wasu hargitsi lokacin da kai ba mamallakin fayil ɗin ba ko kundin adireshin kuma baka da izinin izini suna gaya maka directory / file babu. Idan kai ne mai shi kuma ba ka da tushe, yi haka tare da mai sarrafa fayil tare da sudo, da sauransu sannan ka je zuwa tushen adireshin -> / home -> personal_folder -> Desktop ka gani ko hotunan suna wurin kuma idan zaka iya share jakar.
      Idan wannan baiyi aiki ba, zaku ga wasu zaɓuɓɓuka daga tashar (na'ura).

  17.   Pedro Manuel Aguayo Muñoz m

    Barka dai, yaya kake? Ina yin aiki a bash kuma zan so sanin yadda ake tantance idan akwai kundin adireshi da aka shigar daga keyboard, misali

    #! / bin / bash
    karanta hanya
    #Shigar ~ / tebur
    #Saboda haka nayi kokarin kewaya wannan kundin adireshin
    cd $ hanya
    # Na sami saƙo mai zuwa kamar haka: cd: ~ / Desktop: Fayil ko kundin adireshi babu shi

    Tambayata ita ce, menene zan iya yi don in shigar da shi daga maballin kuma in dauke shi azaman hanyar jagora?

  18.   Miguel m

    Kuma ta yaya zan iya sanin idan akwai ƙungiyar,

  19.   m m

    da kyau Ina da matsala a canaima lokacin da na latsa maballin cunaguaro yana cewa babu fayel ko shugabanci kamar yadda zai yi don magance wannan matsalar sauran kuma ba sa bude cunaguaro don Allah na gode.

  20.   Carlos m

    Yaya zaka yi idan a cikin BASH idan sunan filen yana da sarari?
    Alal misali:
    Afrilu data.xlsx

    Don tambaya idan fayil ɗin ya wanzu

    1.    Manuel Alcocer Jimenez m

      Carlos, kamar haka:

      idan [["$ canji" == Bayani \ watan \ na \ April.xlsx]]; to amsa kuwwa 'wasa'; fi

      Hakanan zaka iya:
      idan [["$ canji" == 'Bayanai na watan Afrilu.xlsx']]; to amsa kuwwa 'wasa'; fi

      Wani zabin:
      [["$ Mai canji" == 'Bayanai na Afrilu.xlsx']] && echo 'matches' || amsa kuwwa 'bai daidaita ba'

      A wasu yankuna masu duhu na intanet suna ba da shawarar amfani da [[]] maimakon [], kuma idan za ku kwatanta lambobi to: ((m> = 3)) && echo 'canji ya fi girma ko daidaita da 3' || amsa kuwwa 'mai canji bai wuce 3 ba'

      Saboda haka, don lambobi:

      idan ((lamba! = 4)); to amsa kuwwa "$ {lambar} ba 4 ba ce"; kuma amo "$ {lambar} 4 ce"; fi

  21.   Cesar Mateus m

    Ta yaya zaku iya yin lamba don inganta idan kundin adireshi yana da takamaiman rubutu, don iya shirya shi.

    misali:

    idan (rubutu1)

    yi komai

    kuma

    canza rubutu1 zuwa rubutu2

  22.   Miguel m

    tambaya, yaya zai kasance a bincika kowane dakika 15 idan fayil ɗin ya wanzu ko babu, na gode.

  23.   bakin kogi m

    Sannu,
    wannan:
    idan [-w / gida / Komun / Rajistan ayyukan / ti]; to
    Gano idan fayil din na iya rubuwa. Amma ya dogara ne akan izinin mai amfani. Shin za a iya yin hakan don gano ko ƙungiyar ko wasu suna da izini?