Bheka ukuthi ngabe ifayili noma ifolda ikhona noma cha (nokuningi) nge-IF loop

Angikabeki lutho lwe Bash, futhi ngukuthi ngenkathi ngingena ezweni elihle lePython ngikhohliwe okuncane ngalo omunye umngani ongisize kakhulu 🙂

Lapha ngifaka okuthile kubathandi beBash, njengoba isihloko sisho ... Ngizokukhombisa ukuthi ungabheka kanjani ukuthi ngabe kukhona ifayili noma ifolda kuhlelo, usebenzisa nje: if

Sizokwakha ifayela le- .sh bese silinika izimvume zokwenza lokhu, ngoba vele ubeke umyalo olandelayo ku-terminal:

thinta test.sh && chmod + x test.sh

Lokhu akwenzile ukudala ifayela (thinta test.sh) futhi ngikunikeze nezimvume zokwenza (chmod + x test.sh)

Ngemuva kwalokho ake sibhale okulandelayo kuyo:

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

Kwenzani lokhu?

Okulula 😀

Kumugqa wokuqala - » uma [-d ​​/ opt /];

Lapha sibuza, ngabe / opt / isiqondisi sikhona? Yebo isiqondisi / ifolda, ngoba -d kusho umkhombandlela noma ifolda.
Lapho-ke uma kwenzeka sikhona sizophrinta esigungwini «Yebo ikhona.»(Lo ulayini 2 no-3)
Ngaphandle kwalokho futhi / opt / ayikho, khona-ke sizophrinta «Cha ayikho»(Imigqa 4 no-5)
Ngemuva kwalokho emgqeni wokugcina (inombolo 6) sivala i-if futhi yilokho.

I-athikili ehlobene:
Sungula ukuxhumana kwenethiwekhi phakathi kwemishini ebonakalayo ye-PC ne-Virtualbox

Yenza isivivinyo, kuzokukhombisa ukuthi / opt / is exist.

Uma ufuna ngikukhombise omunye umbhalo (lowo ongekho) shintsha / khetha / nganoma iyiphi enye ifolda engekho kusistimu yakho, ngokwesibonelo: / asd /

Njengoba ukwazi ukubona, -d isisiza ukuthi sazi ukuthi isiqondisi sikhona yini, manje manje uma siguqula -d to -f ukubheka ukuthi kukhona amafayela (-d = isiqondisi, -f = ifayela / ifayela)

Isibonelo:

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

Nalu uhlu lwezinketho:

-d - »Bheka ukuthi kukhona umkhombandlela othile

-f - »Bheka ukuthi ngabe ikhona ifayili elikhona

-w - »Bheka ukuthi ngabe ifayili elithile linezimvume zokubhala

-x - »Bheka ukuthi ngabe ifayili elithile linezimvume zokusebenzisa.

Futhi akukho okunye okuningi ongangeza u-🙂

Ngiyazi ukuthi lokhu kuzoba wusizo kakhulu futhi kuthakazelise kulabo abathanda ukuhlela eBash 😉

Phendula ngokucaphuna


Okuqukethwe yi-athikili kunamathela ezimisweni zethu ze izimiso zokuhlelela. Ukubika iphutha chofoza lapha.

Amazwana ayi-42, shiya okwakho

Shiya umbono wakho

Ikheli lakho le ngeke ishicilelwe. Ezidingekayo ibhalwe nge *

*

*

  1. Ubhekele imininingwane: Miguel Ángel Gatón
  2. Inhloso yedatha: Lawula Ugaxekile, ukuphathwa kwamazwana.
  3. Ukusemthethweni: Imvume yakho
  4. Ukuxhumana kwemininingwane: Imininingwane ngeke idluliselwe kubantu besithathu ngaphandle kwesibopho esisemthethweni.
  5. Isitoreji sedatha: Idatabase ebanjwe yi-Occentus Networks (EU)
  6. Amalungelo: Nganoma yisiphi isikhathi ungakhawulela, uthole futhi ususe imininingwane yakho.

  1.   Hugo kusho

    Enye indlela yokukwenza ngaphandle kwe- if… ke..else:

    test -f /etc/hosts && elcomando

    Noma futhi

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

    Njll

    1.    KZKG ^ Gaara kusho

      Ngakwesokudla 😀
      … O_O… UnguHugo, uHugo waseCuba? … Isebenzisa iWindows8, WTF !!!

      1.    Hugo kusho

        Yebo, ngiyajwayela le OS futhi uma kufanele ngikhulume ngokungagwegwesi, ukuphela kwento engithole ukuthi ilusizo ngumphathi wamakhophi, ngaphandle kwalokho mina ngokwami ​​ngikuthola kubuhlungu, ikakhulukazi isikhombimsebenzisi sayo se-hybridi nezinhlelo zokusebenza zemetro. Ngiyithanda ngaphansi kwe-Vista ne-7 (yize isebenza kahle kakhulu kunalezi), engingayithandanga nami. Ukuphela kwento engiyisebenzisela yona ukudlala i-NFS13 ne-Skyrim, kepha le OS ithatha isikhala esiningi kangangoba bekufanele ngisuse ukwahlukanisa kwami ​​kwe-Linux [snif].

        Kodwa-ke, ukuze ube nokuthula kwengqondo, ngizokutshela ukuthi ngayizolo ngilande i-Arch ISO kaJanuwari futhi ngihlela ukuyivivinya kancane kunguqulo yayo ye-x86-64, lapho nje sengithola i-repo (uzothanda unayo?). Manje njengoba ngibonile ukuthi isebenzisa amaphakheji asayiniwe ngokuzenzakalela, kunginikeza ukuzethemba okukhulu kokuntywila.

        Futhi ubaba wangitshela nokuthi uzongithumelela i-Sony Xperia, engifuna ukuyisebenzisa ngokunenzuzo yokufunda izinhlelo ze-Android, obekuyisifiso esilindile. Ngakho-ke yehlisa umoya, angikayi engxenyeni emnyama yamandla, buahahahaha 😉

  2.   patz kusho

    ukuhlolwa komuntu

    1.    msx kusho

      Yebo

  3.   RAW-Okuyisisekelo kusho

    Wenas! ..

    Singayenza efanayo .. .. kepha isiqondisi sidluliswe njengepharamitha ..

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

    Futhi sikubiza ngokuthi:

    ./test.sh / opt /

    Ukubingelela ..

  4.   u-savo kusho

    UMA kungewona umjikelezo xD

  5.   UGregorio Espadas kusho

    Ingifanela njengegilavu ​​yesikripthi engisenzayo. Ingabe kuvumelekile ukusebenzisa amakhadi wasendle? Isibonelo, ukuthola ukuthi kukhona yini ama-.mp4s bese usebenzisa okuthile kuwo.

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

    1.    RAW-Okuyisisekelo kusho

      Yebo, kusebenza ngokuphelele ..

      Ngisho ngamapharamitha .. "* .mp4"

      1.    RAW-Okuyisisekelo kusho

        Isibonelo,

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

        Uma uyibiza: ./test.sh * .mp4

        Uma ithola amafayela we-.mp4 iyawasusa .. ..kodwa ayiwabhalisi amafayela akuleyo nkomba ..

        1.    UGregorio Espadas kusho

          Siyabonga ngempendulo namathiphu! 🙂

        2.    Joaquin kusho

          Uphuthelwe i-f xD

          rm -rf $ 1

          Lokhu kujabulisa ngokwengeziwe!

        3.    b1tblu kusho

          Wonderful !!!, Nsuku zonke ngiyithanda kakhulu iLinux, iWindows ibuyiselwe ekhoneni elimnyama leHard Drive yami, esesizosuswa ukuze kukhanyiswe iLinux.

          1.    RAW-Okuyisisekelo kusho

            Kuhle! .. ..bese thatha isinyathelo esilandelayo .. bese uyakhipha ..

            Izinyanga eziyi-6 ezedlule cishe. Ngine-GNU / Linux kuphela kuyo yonke imishini yami futhi angidingi enye .. .. hhayi i-W $ .. noma iwayini .. .. futhi ngijabule kakhulu kanjalo .. 😉

            Futhi ngithelela indawo engizungezile .. njengoba wonke umuntu engibona ngine-Arch yami, ngiyashesha, ngisebenza kahle futhi ngijabule .. .. futhi bangicela ukuthi ngifake i-Linux .. ..yeah! .. 😀

        4.    Inukaze kusho

          Ungahlola futhi izandiso ezahlukahlukene, ungasebenzisa into efana nayo

          #! / bin / bash
          Uhla lwemibhalo = $ IKHAYA / Ukulanda

          uma [$ Directory]; lapho-ke
          echo "Nalu Uhlu Lwezinhlobo Zamafayela:"
          echo "mp1, mp2, mp3, mp4, wav, flac, flaad, aud, midi"
          ls {* .mp ?, *. wav, *. fla, *. aud * .midi}
          fi

          ukusebenzisa {} kukuvumela ukuthi ucacise izandiso eziningi kuleli cala, ukusetshenziswa kwe - "?" , yikhadi lasendle, elikuvumela ukuthi uthathe "Noma yisiphi Isici"

          Ngiyethemba kuyakusiza

        5.    Pedro kusho

          Ngincoma umyalo thola [umkhombandlela] [search_pattern] –exec [command] {} \

      2.    Carlos kusho

        Akusebenzi, ngifaka lawa mafayela enkombeni:
        AB20160420.TXT
        AB20160421.TXT
        AB20160422.TXT
        AB20160423.TXT
        AB20160424.TXT
        AB20160425.TXT
        sesha.sh

        #! / bin / bash
        INDLELA = / tmp / TEST
        cd $ INDLELA
        ls -la | i-awk '{phrinta »« $ 9}'
        uma [[-f AB.TXT]]; lapho-ke
        i-echo »Faka i-IF uma kukhona i-AB
        .UMBHALO »
        fi
        uma [-f AB20160422.TXT]; lapho-ke
        echo "okokufaka kwefayela elithile le-AB"
        fi

        lapho ngifaka [-f AB.TXT] unikeza iphutha
        lapho ngifaka [[-f AB
        .TXT]] ayinikezi iphutha kepha ayikwazi ukuthola ifayili
        kusebenza kuphela lapho ngifaka ifayili elithile, ngokwesibonelo:
        uma [-f AB20160422.TXT]; lapho-ke

        Angikwazi ukuyenza isebenze namakhadi we-wild * noma?

        1.    Pedro kusho

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

          OKUQHUBEKA

          EXIT_OK = 0
          I-EXIT_BAD = 66

          I-BOLD = »\ 033 [1m»
          I-NC = »\ 033 [0m»
          BOMVU = »\ 033 [31m»

          uma [! "$ 1"];
          ke
          bhala "ulahlekile ipharamitha"
          phuma ku- $ EXIT_BAD
          fi

          cd "$ 1"

          amafayela = »ls -h1 $ 1»

          lefayela ku- $ ($ files); yenza
          echo -e "ungenza noma yini nge- $ BOLD $ file $ nc here"
          Kwenziwe

          phuma $ EXIT_OK

  6.   msx kusho

    @KZ, tshela okuningi ngalokhu: "futhi lokho kungenxa yokuthi ngingena ezweni elihle lePython"!

    1.    KZKG ^ Gaara kusho

      HAHAHAHA nah ... kancane kancane 🙂
      Ngisengu-newbie ePython, anginaso isikhathi sokunikela ngamahora namahora kuso, ngakho-ke ngiyahamba kancane yebo, ngiyahamba kodwa ngiqinisekile ekufundeni kwami ​​😀

  7.   I-Audoban kusho

    Ngiyabonga kakhulu 😀 izolo nje bengicabanga ukuthi ngingenza kanjani lokhu.

  8.   I-ReichSk kusho

    lokhu nge-ncurses kuhle 🙂

  9.   jazeco kusho

    ngegama elingu-1… NGOKUPHELELE!
    Isikhathi eside bengiphelelisa iskripthi sokwenza okulandwayo kusuka kwi-Intanethi, kepha bengibhekene nenkinga yokuthi ilande amafayela ebiwacele i-1 kaninginingi ngaphandle kokukwazi ukuqinisekisa ubukhona be-1ro.
    I-tuto enhle kakhulu.

  10.   zulan kusho

    Umnikelo omkhulu, lapha ovela eChile uyawuthokozela.
    Nsuku zonke i-LINUX eyengeziwe iyangimangaza,
    I-Long Live Free Software MIERDAAAA.

  11.   david kusho

    khonza for
    hlola ukuthi ngabe ifayela livela enkombeni

  12.   Grovher kusho

    Uma ngifuna ukubheka ukuthi kukhona noma yiliphi ifayela elisendleleni ethi «/ tmp / receivers», ngikhiphe noma yimuphi umyalo, kungaba kanjani?

    Ngizojabula kakhulu

    Phendula ngokucaphuna

  13.   Juan kusho

    Sawubona, futhi unqume nge-IF ukuthi ngabe ifayili ligcwele ngokuphelele ???. Ngikubuza lokhu ngoba uma ugcwalisa ifayili lokudluliswa, lingayithatha noma yingxenye yedatha.

    Ukubingelela nokubonga ngempendulo yakho.

  14.   Inukaze kusho

    Ngicabanga ukuthi kufanele futhi ungeze isibonelo, esifana nokuthi "Bheka ukuthi ayikho yini" ifayili "noma" umkhombandlela "njengesibonelo

    ukubheka ukuthi ayikho, ngokwesibonelo sebenzisa okuguquguqukayo->

    Isiqondisi

    #! / bin / bash
    Uhla lwemibhalo = »$ HOME / Directory Directory»
    Ifayela = »$ IKHAYA / Ifayela Lokuhlola»

    uma [! -d "$ Directory"]; lapho-ke
    phendula "Akukho Directory $"
    nanela "Ukwakha ->" $ Directory "
    mkdir "$ Directory"
    futhi
    i-echo "Directory" $ Directory "isivele ikhona
    fi

    uma [! -d "$ Ifayela"]; lapho-ke
    phendula "Ifayela $ alikho"
    nanela "Ukudala ->" $ Ifayela "
    thinta "$ File"
    futhi
    i-echo "Ifayela" $ Ifayela "selivele likhona
    fi

    1.    Katekyo kusho

      lapho wenza isheke le-mkdir ngokuhamba kwesikhathi uma i-terminal ingayakha ngoba ingaphula amabhola uma iyifolda njengezimpande noma uhlelo

  15.   UJulio Vinachi kusho

    Sanibonani KZKG ^ Gaara umbuzo wokuthi ngingenza kanjani ukusebenzisa i-curl. Ngingathanda ukwazi ukuthi ngingayifaka kanjani i-url kusuka kukhonsoli ukubona ukuthi iyatholakala yini ngokwesibonelo http://portal.com/directorio/archivo1.php Lapho i-file1 izogcina khona ama-IPs wokuvakasha, kungenxa yokuqapha iqembu engidinga ukulifaka lelo phini njalo uhhafu wehora ukuze ngazi i-IP yomphakathi ngoba iyashintsha ngezikhathi ezithile futhi angikwazi ukuxhuma nge-ssh .

    Ngiyabingelela ngiyabonga kusengaphambili uNkulunkulu akubusise.

    1.    KZKG ^ Gaara kusho

      Sawubona, ungubani?

      Okokuqala, kufanele usho ukuthi ukuguquguquka kwe-IP (ngokwesibonelo) okuqukethwe yilelo fayela, kulokhu kufanele:
      1. Landa ifayela
      2. Buka okuqukethwe kwayo bese uthola i-IP lapho
      3. Leyo IP iyahluka

      Isibonelo:
      Lapha silanda ifayela - »wget http://portal.com/directorio/archivo1.php -o ifayela.html
      Ngemuva kwalokho sabela okuqukethwe okuguquguqukayo: - »: $ {IP: =` cat file.html`}
      .. uthatha ukuthi okuqukethwe kwalelo fayela kune-IP kuphela, uma kungenjalo, kuzofanele usebenze nge-grep, awk noma ikhanda.

      Lapho usunayo i-IP njengokuguquguquka, konke okunye kuyinto elula kunazo zonke emhlabeni 🙂

      Uma ufuna ungibhalele i-imeyili (kzkggaara [at] desdelinux [dot] net) engizokusiza ngenjabulo

      1.    UJulio Vinachi kusho

        Ngiyabonga kakhulu kzkggaara ngiyabonga kakhulu.

  16.   Natalia kusho

    Sawubona, angiqondi okuningi ngekhompyutha futhi angazi ukuthi lokhu okuthunyelwe kuhlobene yini nenkinga enginayo.

    Ngiphawula uma kungenzeka ungifunde, noma ngabe okuthunyelwe kuvela onyakeni owedlule:
    Kuvela ukuthi ngakha ifolda kudeskithophu yami ukugcina amanye amavidiyo. Ngisusile lawo mavidiyo ngoba bengingasawadingi futhi manje le folda ingitshela ukuthi ayikho (noma ibonakala kudeskithophu yami).

    - Leyo yinkinga yokuqala enginayo:
    Angikwazi ukususa ifolda.

    - Inkinga yesibili futhi ngicabanga ukuthi ibaluleke kakhulu:
    Emzameni wokususa ifolda, ngafaka ifolda ye-PHOTOS. Manje lapho ngivula ifolda "engekho" ayinalutho futhi IZITHOMBE azikho.

    Ngingabuyisa kanjani izithombe? Nginike inketho ngaphakathi kwefolda ethi "Bonisa imibhalo efihliwe" kepha ifolda ye-PHOTOS ayiveli.

    Ngiyethemba ungazi ukuthi ixazululwa kanjani.
    Ngiyabonga

    1.    valdo kusho

      Kukhona abasebenzisi ababili okuyibo okuyinhloko: impande ne-your_user (isibonelo natalia). Bobabili banamafolda abo. your_user in / home and root in / (impande lwemibhalo). Ngokuchofoza kwesokudla kufolda bona ukuthi ekabani. Uma kuyimpande nomphathi wakho wefayela usebenzisa iSudo noma i-gksu noma i-kdesu ngokuya ngemvelo oyisebenzisayo iya enkombeni yezimpande -> impande -> Ideskithophu futhi uzothola izithombe futhi ungasusa ifolda. Amanye ama-distros uma ungeyena umnikazi wefayela noma umkhombandlela futhi ungenazo izimvume zokufinyelela akutshela ukuthi umkhombandlela / ifayela alikho. Uma ungumnikazi futhi ungeyona impande, yenza okufanayo nomphathi wefayela ngeSudo, njll bese uya enkombeni yezimpande -> / home -> personal_folder -> Desktop bese ubona ukuthi izithombe zikhona nokuthi ungayisusa ifolda.
      Uma lokhu kungasebenzi, kuzofanele ubone ezinye izinketho kusuka ku-terminal (console).

  17.   UPedro Manuel Aguayo Muñoz kusho

    Sawubona, unjani? Ngenza umsebenzi ku-bash futhi ngifisa ukwazi ukuthi ngingaqinisekisa kanjani uma kukhona umkhombandlela ofakwe kusuka kukhibhodi, ngokwesibonelo

    #! / bin / bash
    funda umzila
    #Ngena ~ / Ideskithophu
    #Ngibe sengizama ukuzulazula kulowo mkhombandlela
    cd $ indlela
    # Ngithola lo mlayezo we-bash olandelayo: cd: ~ / Desktop: Ifayela noma umkhombandlela awukho

    Umbuzo wami uthi, yini engingayenza ukuze ngiyifake kusuka kukhibhodi ngiyithathe njengesiqondisi sendlela?

  18.   Miguel kusho

    Futhi ngingazi kanjani ukuthi iqembu likhona,

  19.   Heidy kusho igama kusho

    kuhle nginenkinga ku-canaima yami lapho ngicindezela inkinobho cunaguaro ithi alikho ifayili noma umkhombandlela njengoba kungenza ukuxazulula leyo nkinga futhi okunye akuvuli i-cunaguaro ngicela ukubonga.

  20.   Carlos kusho

    Wenza kanjani i- if in BASH uma igama lefayela linezikhala?
    Isibonelo:
    Idatha ka-Ephreli

    Ukubuza ukuthi ngabe ifayili likhona yini

    1.    UManuel Alcocer Jimenez kusho

      UCarlos, kanjena:

      uma [["$ variable" == Idatha \ inyanga \ ka- \ Ephreli.xlsx]]; bese unanela 'ukufanisa'; fi

      Futhi unga:
      uma [["$ variable" == 'Idatha yenyanga ka-Ephreli.xlsx']]; bese unanela 'ukufanisa'; fi

      Enye indlela:
      [["$ Variable" == 'Idatha yenyanga ka-Ephreli.xlsx']] && echo 'okufanayo' || i-echo 'ayifani'

      Kwezinye izindawo ezimnyama ze-intanethi bancoma ukusebenzisa [[]] esikhundleni se [], futhi uma uzoqhathanisa izinombolo lapho: ((variable> = 3)) && echo 'variable is greater than or equal to 3' || ukuguquguquka kwe-echo 'kungaphansi kuka-3'

      Ngakho-ke, ngezinombolo:

      uma ((inombolo! = 4)); bese unanela u - "$ {number} akayona engu-4"; enye i-echo "$ {number} ingu-4"; fi

  21.   UCesar Mateus kusho

    ungayenza kanjani ikhodi ukuze iqinisekise uma umkhombandlela unombhalo othile, ukuze ukwazi ukuwuhlela.

    isibonelo:

    uma (umbhalo1)

    ungenzi lutho

    futhi

    shintsha umbhalo1 ube umbhalo2

  22.   Miguel kusho

    umbuzo, kungaba kanjani ukubheka yonke imizuzwana engu-15 uma ngabe ifayili likhona noma cha, ngiyabonga.

  23.   udonga lomfula kusho

    Sawubona,
    lokhu:
    uma [-w / home / Komun / Logs / ti]; lapho-ke
    Thola ukuthi ngabe ifayela liyabhalwa yini. Kepha kusekelwe kuzimvume zomsebenzisi. Ngabe kungenziwa ukuthola ukuthi iqembu noma amanye anazo yini izimvume?