Momwe mungagwiritsire ntchito mu Bash

Momwe mungagwiritsire ntchito ntchito ku Bash kugwiritsa ntchito chinenerochi chokhazikitsidwa ndi chipolopolo cha Unix, POSIX. Monga chilankhulo, ntchito yake imakhala ndikumasulira kwa malamulo a Linux, kutilola kuti tizitha kubwereza mobwerezabwereza ndikupanga malamulo kuchokera ku machitidwe opangira. M'nkhaniyi tikambirana momwe mungagwiritsire ntchito mu bash. Ndikupangira kuwerenga nkhaniyi momwe pangani zolemba zanu pogwiritsa ntchito bash.

Muzolemba zomwe tikupangira, timagwiritsa ntchito chilankhulo cha Bash kusaka fayilo, kudziwa dzina lake. Pachifukwa ichi tigwiritsa ntchito pezani lamulo koma mothandizidwa ndi ntchito zomwe zidatchulidwa kale mu script. Muyenera kuganizira zachilendo kapena zoperewera za Bash zomwe sizipezeka m'zilankhulo zonse: kuyitanitsa ntchito kuyenera kufotokozedwa kale.

Fotokozani ntchito

Pali njira ziwiri zofotokozera ntchito: kapena popanda chidziwitso cha ntchito:

function nombre_funcion () 
{
    # codigo
}

kapena ina iyi, yomwe ndi yomwe ndimagwiritsa ntchito monga muwonera mtsogolo.

nombre_funcion ()
{
    # codigo
}

Komanso Bash imaperekanso njira yopititsira magawo ndikubwezera zotsatira. zomwe tiona m'nkhani zamtsogolo.

#!/usr/bin/env bash

# ~/.bin/encontrar
# encuentra archivos a partir de la descripción de su nombre en un directorio específico
#
# Por Pedro Ruiz Hidalgo
# version 1.0.0
# Copyright © enero 2017
#
#

EXIT_OK=0
EXIT_BAD=66

PATRON=$1
DIRECTORIO=$2

autor ()
{
 echo -e "\nPedro Ruiz Hidalgo @petrorum. Copyright © 2017\n"
}

ayuda ()
{
 echo -e "\nencontrar [PATRON] [DIRECTORIO]\n"
} 

noparams ()
{
 echo -e "\nSon necesarios dos parámetros\nencontrar -h para ayuda\n"
 read -p "¿Quieres ver la ayuda? (S|s)" -n 1 -r
 if [[ $REPLY =~ ^[Ss]$ ]];
    then
       echo ""
       ayuda
 fi
}

nodir ()
{
 echo -e "\nDirectorio no Existe\n"
}

if [[ $PATRON == "-h" ]];
then 
 ayuda
 exit $EXIT_OK
fi

if [[ $PATRON == "-a" ]];
then 
 autor
 exit $EXIT_OK
fi

if [ $# -lt 2 ];
then
 noparams
else
 if [ -d $DIRECTORIO ];
 then
 echo ""
 find $DIRECTORIO -name $PATRON*
 echo ""
 exit $?
 else 
 nodir 
 exit EXIT_BAD
 fi
fi


Kusanthula kwamalemba

Malingaliro

Za bash Njira zonse zomalizidwa bwino ziyenera kukhala ndi nambala ya "0" ngati chizindikiritso. Mzere wa 12 ndi 13 umafotokozera zolakwika zomwe zagwiridwa EXIT_OK kuchita bwino y EXIT_BAD potuluka polephera.

M'mizere 15 ndi 16, mitundu ya PATTERN ndi DIRECTORY imapatsidwa magawo oyamba ($ 1) ndi achiwiri ($ 2) omwe amapezeka pamzere wolamula pambuyo pa dzina la cholembedwacho, monga tidzawonera pambuyo pake.

Mu mzere 18 timapanga ntchito yathu yoyamba. Ntchito yotchedwa «wolemba» imawonetsa zolembalemba tikazitcha ndi "-a" kutsutsana monga momwe mukuwonera mu mizere 50 ~ 54. Mtsutso "-ndipo" kuchokera mzere 23 imalola kuwonetsa mndandanda wa «mzere wotsatira» potumiza «\ n».

Kuitana kwa noparams (mizere 28 ~ 37) ndi komwe kumayang'anira zochitika zomwe ziyenera kuchitika script ikaitanidwa popanda magawo aliwonse. Timawonetsa, yotsekedwa bwino pakati pamakhodi atsopano, uthenga wosonyeza kuti script iyenera kuchitidwa ndi magawo awiri, kenako njira (mzere 31) ikuwonetsedwa kuti igwiritsidwe ntchito werengani Zimakulimbikitsani kuti musindikize "S" kapena "s" ngati mukufuna kuwonetsa thandizo. Mzere 32 timanena kuti: 'ngati yankho (lomwe limabwera kwa ife mosiyanasiyana $ YANKHANI) ili ndi zilembo zilizonse zazikulu kapena zochepa ', kenako (mzere 33) ikuwonetsa mzere wopanda kanthu (mzere 34) ndikuchita ntchito yothandizira (mizere 23 ~ 26).

Ntchito ya nodir (mizere 39 ~ 42) idzachitika tikazindikira kuti chikwatu chomwe kusaka kusaka kulibe.

Kugwira ntchito

Ndi izi tili nazo kale inafotokozera ntchito zonse zofunika kukhazikitsa pulogalamu yathu, yomwe imayamba pamzere wa 44, kuwunika ngati gawo loyamba lomwe script imalandira ndi "-h", ngati ndi zowona, chitani ntchito yothandizira ndi kutuluka posonyeza kutha kwachizolowezi.

Ngati PATTERN (woyamba parameter monga wafotokozedwera mu mzere 15) ndi "-a", wolemba amawonetsedwa motsatira njira yomweyi yomwe yafotokozedwa mundime yapitayi posankha "-h".

Pamzere 56 ikuyang'aniridwa kuti sitinalandire magawo ochepera awiriPoterepa, ntchito ya noparams ikuchitika, ndiye kuti mu mzere wa 60 tidziwa ngati chikwatu chomwe tikufuna kusaka chilipo, ngati ilipo, mzere wopanda kanthu ukuwonetsedwa, pezani lamulo ndi adilesi ya chikwatu chomwe tikufuna kufufuza ndikutsata mtundu (kuyambira kwa dzina la fayilo yomwe tikufuna) mzere watsopano wopanda kanthu ndikugwiritsa ntchito kutuluka $? timapereka zotulutsa zathu pazotsatira zomwe tapeza. Ngati mkhalidwe wa kukhalapo kwazolemba ndikwabodza (mzere 67) timayitanitsa ntchito ya nodir ndipo timachoka posonyeza kutha kwachilendo.

Kuphedwa ndi kuyesa

$ encontrar
$ encontrar -a
$ encontrar -h
$ encontrar index aljflaskjf #directorio no existe
$ encontrar index public_html
$

En nkhani zotsatirazi zokhudza Bash tiwona njira za gwiritsani magawo mu ntchitoTionanso momwe tingachitire fotokozerani za kubwerera kuchokera momwemo.

Ndikukhulupirira ndikuyembekeza kuti positi yakuthandizani.


Zomwe zili m'nkhaniyi zikutsatira mfundo zathu za malamulo okonzekera. Kuti mufotokoze cholakwika dinani Apa.

Ndemanga, siyani yanu

Siyani ndemanga yanu

Anu email sati lofalitsidwa.

*

*

  1. Wotsogolera pazosankhazi: Miguel Ángel Gatón
  2. Cholinga cha deta: Control SPAM, kasamalidwe ka ndemanga.
  3. Kukhazikitsa: Kuvomereza kwanu
  4. Kulumikizana kwa zomwe zafotokozedwazo: Zomwezo siziziwululidwa kwa anthu ena kupatula pakukakamizidwa mwalamulo.
  5. Zosunga: Zosungidwa ndi Occentus Networks (EU)
  6. Ufulu: Nthawi iliyonse mutha kuchepetsa, kuchira ndikuchotsa zidziwitso zanu.

  1.   Yosea anati

    moni,
    zosangalatsa komanso zomveka bwino.
    Ndemanga chabe; $ akusowa pamzere 68 kutsogolo kwa zosinthika EXIT_BAD.
    Ndipitiliza kuphunzira zowonadi ndi zolemba zanu.