[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Re: String handling i bash - SOLVED



On Fri, 18 Sep 2009 20:14:31 +0200
"Jesper K. Pedersen" <sslug@sslug> wrote:

> On Fri, 18 Sep 2009 19:38:17 +0200
> sslug@sslug (Adam Sjøgren) wrote:
> 
> > On Fri, 18 Sep 2009 15:22:59 -0200, Frank wrote:
> > 
> > > Undlad at bruge basename da den skal starte en ny proces.
> > 
> > Det kan være en ulempe hvis hans script kører det rigtigt, rigtigt
> > mange gange.
> >  
> > > Hans forslag, newdir=${latest##*/}, er klart den bedste måde at
> > > håndtere det på.
> > 
> > Men kræver en speciel shell der kan den syntaks, dvs. bash.
> > 
> > [...] 
> > > Man kan også lave andre smarte ting med variabler.
> >  
> > > F.eks. dette i et script:
> >  
> > > Comment="Dette er en kommentar!"
> > > echo "${Comment// /_}"
> >  
> > > giver dette resultat: Dette_er_en_kommentar!
> > 
> > Ikke i alle shells, i f.eks. dash, der følger POSIX specifikationen
> > og vejer væsentligt mindre end bash (ca. 1/8), får du:
> > 
> >   $ Comment="Dette er en kommentar!"
> >   $ echo "${Comment// /_}"
> >   dash: Bad substitution
> >   $ 
> > 
> 
> Løser de her ting spørgerens udfordring? - hans spørgsmål gik på at
> fjerne en del af en streng fra en anden - men baggrund af en streng de
> indeholdt en "root" sti - og en anden et filnavn med fuld sti.
> 
> Såvidt jeg kan se opstår der problemer med løsningerne hvis der er
> underbiblioteker involveret - f.eks. :
> 
>   root = /var/mitdir
>   filnavn_med_sti = /var/mitdir/et_eller_andet_underbibliotek/min_fil
> 
> Og kan dette løses direkte med shell streng håndtering (mit shell
> scripting er stort set "ikke eksisterende") ?
>

Fik så lige en hurtig lektion i bash strenghåndtering 

  echo ${latest/#$root/}

Hvis indholdet af $root variablen findes i starten af $latest variablen
så erstattes den med ingenting og giver derfor det rigtige svar - også
selv om man ved et uheld skulle have en "latest" fil der ligger udenfor
"root" området (men det er så en helt anden historie).

Mvh
  JesperKP


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2009-10-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *