Bash: Yanayi (idan-sa'annan kuma)

Barka dai 😀

A wannan karon zan nuna muku yadda ake yin rubutu da yanayi a ciki Bash, wanda aka fassara shine:

Idan X abinda kake so ya cika, Y aikin yayi, idan kuma bai cika ba sannan wani aikin yayi.

Bayani mai sauki babu 😀

Yanzu, Na yi tunani na aan mintoci game da wani misali don amfani da shi a cikin wannan koyarwar, mawuyacin halin / matsala / halin da ake ciki ya same ni:

Muna kan hanyar sadarwar kamfaninmu, kuma muna son sanin idan kwamfutar X tana haɗe da cibiyar sadarwar. Don wannan muna yin script me zai yi ping zuwa ga waccan kwamfutar, kuma idan tana kan hanyar sadarwa ne (wato, idan ta dawo da ping) zai gaya mana cewa EH, yana kan hanyar sadarwa, in ba haka ba (ma'ana, ba a kan hanyar sadarwar ba) zai gaya mana cewa BA a kan hanyar sadarwar bane.

Da zarar an gama wannan, yanzu zan bayyana yadda ake zagayawa tare da yanayi 🙂

Ga lambar:

ping -c 1 DIRECCION-IP
if [ $? -ne 0 ]; then
echo "No está en red"
else
echo "Sí está en red"
fi

Kar ku damu, zan muku bayani dalla-dalla 😉

ping shine umarnin da zamuyi amfani dashi, kuma zai gaya mana idan wannan PC ɗin tana kan hanyar sadarwa. Don faɗi wace PC ɗin da muke so mu bincika ko a kan hanyar sadarwa take, dole ne mu canza IP ADRESS ta bayyane, adireshin IP na PC ɗin da muke son bincika.

Kamar yadda kake gani, na sanya «-c ku 1«, Wanne ya zama dole a gare mu. Lokacin da muke yin ping na kwamfuta, wannan aikin baya tsayawa (ping) har sai mun matsa kanmu [Ctrl] + [C], don haka sa «-c ku 1»Mun gaya masa yayi tabbaci ɗaya ne kawai (kawai ƙoƙarin ping) kuma ba wani ba, wannan zai sa ya tsaya nan take, wannan shine… zai bincika ko kwamfutar tana kan hanyar sadarwa sau ɗaya kawai.

Idan kuna da shakku game da wannan, babu ciwo ... sun faɗi haka kuma zan yi farin cikin sake bayyana musu 😉

Yanzu ya sake zagayowar, saboda abin da kawai na bayyana ba komai bane face umarni / aiki na al'ada 😀

if [ $? -ne 0 ]; then
echo "No está en red"
else
echo "Sí está en red"
fi

Don ku fahimci wannan, zan yi bayani mai mahimmanci game da Bash 🙂

Mafi masanin batun zai iya nuna ni a matsayin mai kuskure ko wani abu makamancin haka, amma hey, na rubuta wannan ne don sababbin ko ƙwararrun masana, matuƙar sun fahimce shi sosai 😉

Yana faruwa kamar yadda a ciki Bash wani abu ne kamar 0 y 1, ma'ana, ko kuna raye ko kun mutu, lokacin da aka aiwatar da umarni ko aiki: Ko dai kashe shi da kyau Babu matsala (1), ko akwai wasu matsala ko kuskure (0).

Muna aiwatar da aikin X ko umarni, kuma abin da muka aikata mai yiwuwa an aiwatar dashi da kyau ko mara kyau, yana iya samun kuskure ko a'a, kuma anan ne bayanin yake 😉

Idan abin da muka aika don yin (a wannan yanayin: ping -c 1 IP-ADDRESS) bai ba kuskure ba kuma ya ci nasara, saboda haka zai dawo da ƙimar: 1 . In ba haka ba, kuma idan aikin (wato, ping) bai yi nasara ba, zai dawo da ƙimarsa 0.

 Kuma a ƙarshe, abin da lambar da ke sama ke nufi ita ce:

Idan an dawo da darajar 0 to

Nuna rubutun: «Ba akan hanyar sadarwa ba»

In ba haka ba (kuma dawo BA 0, amma 1)

Nuna rubutun: «Idan yana cikin network«

Wannan da na ɗan bayyana muku, zai yi mana hidimar nan gaba don abubuwa da yawa, saboda yana da amfani sosai a ce idan aikin X ya ba da kuskure, yi aikin Y, kuma idan aikin X bai ba da kuskure ba, bari aikin Z.

Na san cewa wasu za su iya shiga ciki, don haka na yi ƙoƙari na bayyana ta ta hanyoyi daban-daban, da ƙoƙarin fahimtar da kowa fahimtarsa ​​ta wata hanyar. Idan har kowa yana cikin shakku, bari na sani.

Yanzu, bari mu sanya rubutun mu 😀

Dole ne mu bi matakai a cikin wannan koyawa: Bash: Yadda ake yin rubutun aiwatarwa

To, bari mu kwafe lambar mai zuwa cikin wannan fayil ɗin (rubutun.sh), kuma a karshen bari mu ce «fita»(Ba tare da ambaton ba):

ping -c 1 DIRECCION-IP
if [ $? -ne 0 ]; then
echo "No está en red"
else
echo "Sí está en red"
fi

Ya kamata yayi kama da wannan (tuna cewa tabbas sun canza IP-ADDRESS zuwa IP ɗin da suke so):

Anan zaku iya ganin rubutun gudu:

% CODE1%

 

Kamar yadda kake gani, a ƙarshe ya gaya mana «Ee yana cikin hanyar sadarwa»🙂

Abu mai mahimmanci a nan shine sun fahimci wannan yanayin sosai, don haka na bar muku wata lambar don sake bayyana ta, amma daga wani ra'ayi.

read "texto"
if [ "$texto" = "3" ]; then
echo "Correcto"
else
echo "Incorrecto"
fi

Abin da wannan ke nufi mai sauki ne, Na bar layin bayani ta layi:

Layi na 1: Abin da muka rubuta, wannan zai zama darajar mai canzawa «rubutu»(Ba tare da ambato ba).

Layi na 2: Bincika idan abubuwan mai canzawa (abin da muka rubuta yanzu) shine 3.

Layi na 3: Idan akwai 3, zai nuna mana rubutun «Correcto»(Ba tare da ambato ba).

Layi na 4: In ba haka ba (wato, idan har ba mu rubuta 3 ba).

Layi na 5: Zai nuna mana rubutun «Ba daidai ba»(Ba tare da ambato ba).

Layi na 6: Ofarshen yanayin.

Kamar yadda suka iya fahimta, idan muka sanya Kira sannan tsakanin faɗakarwa biyu («) rubutu, wannan zai sa a nuna wannan rubutun a cikin tashar. Wannan shine, idan muka sanya:

echo "esto es una prueba"

Zai nuna mana rubutu a cikin tashar: wannan jarabawa ce

Amma idan muka koma ga wannan misali na biyu, zan nuna muku fa'idar (da aiwatarwa) na wannan rubutun na biyu tare da wani abu mai SOSAI 😀… hankula «Nawa ne 1 + 2?«

Na bar lambar cikakken rubutun:

#!/bin/bash
# -*- ENCODING: UTF-8 -*-
echo "¿Cuánto es 1 + 2?"
read "texto"
if [ "$texto" = "3" ]; then
echo "Correcto"
else
echo "Incorrecto"
fi
exit

Ga yadda rubutun yake:

% CODE2%

 

Kuma da kyau ... babu wani abu da za a ƙara.

Wannan wani abu ne na farko, mai sauki a, amma duk da haka nayi kokarin bayyana shi kamar yadda ya kamata, saboda ba kowa bane yake da ruhin mai shirye-shiryen, kuma sau da dama muna bukatar yin rubutun kamar wadannan (ko makamancin haka), a kowane hali ina fatan wannan labarin zai kasance mai amfani a gare ku wani 🙂

Duk wani shakku ko tambaya, korafi ko shawara don Allah, ku bar shi anan, zan amsa muku da farin ciki kuma don haka, duk mun ɗan ƙara fahimtar 😀

gaisuwa


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

26 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.   Jalas m

    Kai ne pro !!! * ko *

    1.    KZKG ^ Gaara m

      Nah ba komai 😀
      Idan yayi maka aiki kuma abin sha'awa ne, na gamsu 😉

      Assalamu alaikum aboki

  2.   dace m

    @ KZKG ^ Gaara,
    yana magana game da shirye-shiryen bash Ina da tambaya:
    Menene bambanci tsakanin bayyana canji kamar wannan VAR = hello da bayyana shi kamar haka VAR = $ {VAR: -hello}?

    Na bayyana:
    http://pastebin.com/a3cfWXeD

    gaisuwa 😉

    1.    KZKG ^ Gaara m

      Barka dai 😀
      Da kyau, Ni ba gwani bane a bash kwata-kwata ... a zahiri, ban san LOL ba !!
      Kamar yadda na bayyana masu canji kamar haka:
      : ${OPTFOLDER:="/opt/"}

      Amma saboda koyaushe nayi shi haka, ban san menene bambancin zai iya zama gaskiya 🙁

  3.   dace m

    @ KZKG ^ Gaara
    Da kyau na yi tambaya saboda gaskiyar ita ce ban sani ba, yawanci ina rubuta SlackBuilds kuma gaskiyar ita ce cewa masu canji sun bayyana kamar yadda yake a VAR1 suna da yawa a cikin waɗannan rubutun. Wani bincike na google mai sauri ya taimaka min in bayyana wannan tambayar, ina raba shi ga kowa domin dukkanmu mu koya:

    Rubuta:
    http://pastebin.com/faAQb35w

    Bayanin:
    Bayyana masu canji na nau'ikan VAR = $ {VAR: -default_value} na nufin cewa mai canzawa VAR zai ɗauki darajar tsoho_value idan kuma idan ƙimar ta zama banza ko babu.

    Misali mai amfani:
    Lokacin aiwatar da rubutun, ana neman shigar da ƙimar da za a adana a cikin VAR mai canzawa, idan an shigar da wani abu zai nuna abin da aka shigar. Idan ba mu shigar da komai ba kuma danna latsa muna bayyana mai canza VAR a matsayin mara amfani, saboda haka yana nuna darajar_default.

    Na gode.

    1.    KZKG ^ Gaara m

      HAHA Ban san hakan ba 😀
      Yayi, na gode sosai aboki ... wannan shine ma'anar karshen labarin, banyi niyyar koyar da wani abu ba kuma hakane, na sani cewa koyaushe zan koya sabon abu 😀

      Gaisuwa da godiya a sake.

      1.    dace m

        kana da gaskiya, mutum zai koyi abubuwa da yawa anan.

        gaisuwa da barka da hutu !! 😀

  4.   Lucas Matthias m

    Mai ban tsoro da kyakkyawan bayani + 1, lokacin da zai dauke ku ...
    amma na cancanci ƙoƙari 😉

    1.    KZKG ^ Gaara m

      A zahiri na rubuta 80% a cikin kwana 1 kacal, ya dau lokaci mai tsawo don buga shi saboda kawai yanar gizo na ba zata bari na ba.
      Na gode da bayaninka 😉

  5.   Hugo m

    Wata hanyar yin hakan ita ce ta amfani da DNS, tunda wasu lokuta cibiyoyin sadarwa suna da yarjejeniyar ICMP:

    (host -ta IP-ADDRESS> / dev / null 2> & 1) && echo "Haɗa zuwa cibiyar sadarwa"

    Hakanan zaku iya lura cewa a cikin wannan misalin kuɗin sake dawowa yana bayyane 😉

    1.    KZKG ^ Gaara m

      Kai kamar kowane aboki, tare da nasihohin da ban sani ba gaba daya HAHAHA.
      Na gode, an yaba da bayanin kuma hehe… wani sabon abin da na koya 😀

  6.   Datux m

    Gracias

    🙂

  7.   Dan Kasan_Ivan m

    Kodayake kwanaki da yawa sun shude tun lokacin da aka buga wannan batun, ya yi min aiki mai yawa, yanzu da na yi rubutu a cikin bash .. Godiya Gaara ..

  8.   Edgar navarro m

    Doc. Mun gode da taimakon da kuka yi min a fili.

    Abin tambaya kawai, ta yaya zan yi don haka lokacin da kwamfuta ɗaya ta daina ping ɗin ta atomatik ɗayan ya canza IP. Ina da wannan

    don canza IP
    #! / bin / bash
    ping -c 10 192.168.1.50 # Idan baya ping kai tsaye
    ifconfig eth0 192.168.1.50 netmask 255.255.255.0 watsawa 192.168.1.0
    ifconfig eth0 ƙasa
    ifconfig eth0 sama

  9.   Ibrahim m

    saboda yayin kwatanta da idan kayi amfani da alamar tambaya? menene bambanci tsakanin sanya wannan? wani harafi

    1.    KZKG ^ Gaara m

      $? na nufin 'fitowar da ta gabata', wato, idan sakamakon umarnin da ya gabata ... 🙂

      1.    Andres m

        Akwai wata hanyar don samun sakamako iri ɗaya, ana ba da umarnin ping kai tsaye azaman hujja don idan:

        idan ping -c 1 IP-ADDRESS; to
        amsa kuwwa "Ee yana kan yanar gizo"
        kuma
        amsa kuwwa "Ba a kan hanyar sadarwa ba"
        fi

        Abinda ya faru shine idan aka kimanta darajar komar da kuka zartar a matsayin hujja, idan ta dawo 0 gaskiya ne, komai kuma karya ne. Sasannin murabba'i masu kwatankwacin umarnin gwaji. Amma zaka iya zartar da kowane umarni azaman hujja (muddin umurnin ya dawo da wani darajar).

  10.   gaisuwa m

    Barka dai, ta yaya zan aiwatar da script.sh tare da mai amfani X a cikin rubutun Na ƙirƙiri mai amfani Y, kuma wannan mai amfani Y yana ci gaba da aiwatar da rubutun.sh

    Shin ana iya yin hakan ??

  11.   Kuktos m

    Mai ban sha'awa sosai, godiya!

  12.   Francisco m

    Godiya ga gudummawa, Na kasance awa ɗaya don fahimtar shi xD, amma na fahimce shi !!!!.

  13.   Eloy m

    Rubutun yayi kyau. A lissafi kuma ana iya aiwatar dashi azaman ($? == 0) idan ya yi daidai da sifili ba ya kan hanyar sadarwar, in ba haka ba yana kan hanyar sadarwar. Kuma idan har yanzu muna son sanya shi ɗan ma'amala, muna iya cewa:
    amsa kuwwa -n Shigar da IP:
    karanta ip
    ping -c 1 $ ip

  14.   Dario m

    Barka dai, ni sabon abu ne ga wannan, Ina kokarin yin lambar tikiti (baƙaƙe) ta hanyar karatu kuma ina so idan abin da aka shigar yana da tsari daidai (ABC-123456) ya zartar da umarnin "x" kuma ban san yadda zan yi ba, za ku iya taimake ni?

    amsa kuwwa "Shigar da tikiti"
    karanta -p tikiti

    idan $ tikiti = "babu ra'ayi (tsarin ABC-123456"); sannan cp file.txt $ tikiti; sake amsa kuwwa "Tsarin da ba daidai ba, sake gwadawa"; karanta -p; fi.

    Tabbas yana da ban tsoro kuma suna dariya haha, amma kamar yadda na ce ina farawa da wannan.

    Idan nayi bayani mara kyau to ku fada mani kuma nayi kokarin yin mafi kyau.

    Rungume kowa.

  15.   Humberto Y m

    Kyakkyawan bayani, gaisuwa

  16.   agile m

    Ina da ɗan shakku da ko, da sauransu da sauransu.
    Ina son rubutun don duba cewa fayil ya wanzu (ɗayan logs) kuma in ba haka ba, don ƙirƙirar shi kuma daga baya rubuta masa. Amma idan akwai, ina so ku rubuta masa kawai.

    Abin da nake da shi shine:

    kwanan wata = `kwanaki -R`
    #Na gwada kwanan wata mai canzawa, wanda ba a sabunta shi tsakanin farkon a
    # tsari kuma yana ƙarewa, wani lokacin sa'a ɗaya na iya wucewa kuma lokacin daidai ba ya fitowa.

    idan [-f /home/user/logs/test.log];
    sa'an nan
    touch /home/usuario/logs/test.log
    kuma
    echo "$ kwanan wata: An sabunta" >> /home/user/logs/test.log
    amsawa «———————————————» >> /home/user/logs/test.log
    fi

    A ka'idar ya kamata ya zama lafiya, amma gaskiyar ita ce ba a sabunta shi ba idan fayil ɗin da aka ambata ya riga ya wanzu

    1.    agile m

      Yi hakuri, na ga ba a aika ba kuma ya ninka