basename uye dirname: mirairo miviri iwe yaunofanirwa kuziva

terminal shell Linux mirairo

Dzimwe nguva, mamwe ma tutorials anoitirwa kutsanangura mimwe mirau isinganzwisisike uye isinganzwisisike, kune rimwe divi, kune mamwe anosanganisira akapihwa serial uye haana kufarirwa se cd, ls, katsi, nezvimwewo, asi zvinongoshanda. Muchidzidzo ichi ini ndinokuratidza zvinogona kuitwa nemaviri emirairo iyi: basename ndokuudza.

Zvichida priori zvinoita kunge zvisina musoro kwauri uye kuti hazvina zvazvinobatsira, asi vanazvo chaizvo anoshanda mafomu mune dzimwe nguva, senge, semuenzaniso, muzvinyorwa pazvinofanirwa kuburitsa chimwe chikamu chegwara, senge zita refaira kana dhairekitori kuitira kuti mumwe murairo ushande pane izvi ...

Ndezvipi izvi

Iyi mirairo yakanyanya kukosha, uye mashandiro ayo Ndizvo:

  • basename: inoshandiswa kuburitsa zita refaira kubva munzira.
  • Ndiudze: yaishandiswa kuburitsa iro dhairekitori zita kubva munzira.

Mienzaniso yekushandisa

Pano unogona kuona zvimwe mienzaniso mashandisiro avo

  • Semuenzaniso, kushandisa basename ne / etc / passwd, uye kuti inodzosera zita refaira mune zvarinobuda, mune ino kesi passwd:
basename /etc/passwd

  • Iwe unogona zvakare kudoma kuwedzera saka kuti inokupa iyo zita rezita pasina kuwedzerwa. Semuenzaniso, ngatiti iwe unoda kuburitsa zita remufananidzo /home/media/test.jpg pasina iyo jpg yekuwedzera (yaizodzoka bvunzo):
basename -s .jpg /home/media/prueba.jpg

  • Unogona kunyange fambisa nzira dzakawanda panguva imwe chete zvakasiyana, nekuda kweizvi iwe unofanirwa kushandisa iyo -a sarudzo:
basename -a /etc/passwd /var/log/boot.log

  • Kuti uite zvinopesana, uye ndokupa iwe zita redhairekitori, pasina zita refaira, ipapo unofanirwa kushandisa Ndiudze. Semuenzaniso, kana iwe uchida kuishandisa munzira /var/spool/mail/test.txt uye ive nekudzoka / var / spool / mail, wozoshandisa:
dirname /var/spool/mail/prueba.txt

Kana iri a zvinoshandiswa mune script, pano une mumwe muenzaniso. Fungidzira kuti iwe une yakapusa script, uye kuti mairi mune nzira inosiyana. Asi iwe unoda kuti iratidze dhairekitori rine iyo faira, zvisinei nefaira, mune iyo kesi iwe unogona kuve nechimwe chinhu senge:

pathname="/home/usuario/data/fichero"

result=$(dirname "$pathname")

echo $result

Zviripachena, mune ino script "nzira yezita" yaizogara iri imwecheteyo yakaziviswa neyakajairwa pakutanga, asi panogona kunge paine zviitiko zvisipo, uye ndipo pari zvinova zvinoshanda. Semuenzaniso:

/*script para convertir una imagen gif en png*/

#!/bin/sh
for file in *.gif;do
    #Salir si no hay ficheros
    if [! -f $file];then
        exit
    fi
    b='basename $file .gif'
    echo NOW $b.gif is $b.png
    giftopnm $b.gif | pnmtopng >$b.png
done


Izvo zviri muchinyorwa zvinoomerera pamisimboti yedu ye tsika dzekunyora. Kuti utaure chikanganiso tinya pano.

Mhinduro, siya zvako

Siya yako yekutaura

Your kero e havazobvumirwi ichibudiswa.

*

*

  1. Inotarisira data: AB Internet Networks 2008 SL
  2. Chinangwa cheiyo data: Kudzora SPAM, manejimendi manejimendi.
  3. Legitimation: Kubvuma kwako
  4. Kutaurirana kwedata
  5. Dhata yekuchengetedza: Dhatabhesi inobatwa neOccentus Networks (EU)
  6. Kodzero: Panguva ipi neipi iwe unogona kudzora, kupora uye kudzima ruzivo rwako

  1.   Gregorio ros akadaro

    Muenzaniso wakanaka wezvinhu izvo zvekuti paunenge uchidzidza unoona zvisina basa, asi kana wasvika pachidzidzo unoona mashandiro acho.

bool (chokwadi)