Prev: Sed & Awk (was: Re: Printf - Columns/Tables)
Next: building the printf format for a variable number of args
From: Sidney Lambe on 29 Mar 2010 13:34 On comp.unix.shell, moonhkt <moonhkt(a)gmail.com> wrote: > On Mar 29, 9:03=A0pm, pk <p...(a)pk.invalid> wrote: >> Thomas 'PointedEars' Lahn wrote: >> >> #!/bin/ksh >> >> MDIR=3D/phx/migration >> >> for i in $MDIR/load $MDIR/src >> >> > Can be compacted to >> >> > =A0 for i in $MDIR/{load,src} >> >> > But you really don't want to use `for' here (consider $IFS in filenames= > ). >> >> If what you're saying was true, then >> >> for i in * >> >> will never work when filenames have $IFS in them. > > Thank a lot. > > #!/bin/ksh > # aix 5.3 > MDIR=3D/phx > for i in $MDIR/{load,utility,src} > do > cd $i > find $i ! -type d -print > done > > Result as below, But How to filter out backup copy *.yyyymmdd_hhss ? > $ test_ls.ksh > /phx/load/abc > /phx/load/abc.20100326_2301 > /phx/load/abc.20100327_1338 > /phx/load/abcd > /phx/utility/abc1.ksh > /phx/utility/ut001.p > /phx/utility/ut002.p > /phx/src/abc.p You didn't say anything about filtering out certain files in your original post. And find will descend into subdirectories unless you use -maxdepth 1, which is only found in GNU find. You didn't respond to my first post so I won't provide a solution here. Sid
From: Janis Papanagnou on 29 Mar 2010 12:40 Thomas 'PointedEars' Lahn wrote: > moonhkt wrote: > >> On Mar 29, 9:03 pm, pk <p...(a)pk.invalid> wrote: >>> Thomas 'PointedEars' Lahn wrote: >>>>> #!/bin/ksh >>>>> MDIR=/phx/migration >>>>> for i in $MDIR/load $MDIR/src >>>> Can be compacted to >>>> >>>> for i in $MDIR/{load,src} >>>> >>>> But you really don't want to use `for' here (consider $IFS in >>>> filenames). >>> If what you're saying was true, then >>> >>> for i in * >>> >>> will never work when filenames have $IFS in them. > > The point is that $MDIR/{load,src} may contain $IFS. The point is that you should quote it "$MDIR"/{load,src} The 'for' construct has no problem with that. Janis > >> [...] >> MDIR=/phx >> for i in $MDIR/{load,utility,src} >> do >> cd $i >> find $i ! -type d -print >> done If load, utility, and src are in the same directory this won't work, because in the first iteration you change directory to 'load' and then try to continue _from there_ to 'utility', and finally to 'src'. But you can perform the commands in a subshell for i ... do ( cd $i find ... ) done And you should check whether the cd succeeded before continuing. for i ... do ( cd $i && find ... ) done But why, in the first place, do you perform the cd if you're providing the absolute path to find anyway? Janis > > That is pointless, and a standalone -print is superfluous. You only need > this one line (and no script file at all): > > find $MDIR/{load,utility,src} ! -type d > >> Result as below, But How to filter out backup copy *.yyyymmdd_hhss ? > > man find | less -p '-regex pattern' > > > PointedEars
From: Thomas 'PointedEars' Lahn on 29 Mar 2010 13:12 Janis Papanagnou wrote: > Thomas 'PointedEars' Lahn wrote: >> moonhkt wrote: >>> pk wrote: >>>> Thomas 'PointedEars' Lahn wrote: >>>>>> #!/bin/ksh >>>>>> MDIR=/phx/migration >>>>>> for i in $MDIR/load $MDIR/src >>>>> Can be compacted to >>>>> >>>>> for i in $MDIR/{load,src} >>>>> >>>>> But you really don't want to use `for' here (consider $IFS in >>>>> filenames). >>>> If what you're saying was true, then >>>> >>>> for i in * >>>> >>>> will never work when filenames have $IFS in them. >> The point is that $MDIR/{load,src} may contain $IFS. > > The point is that you should quote it > > "$MDIR"/{load,src} > > The 'for' construct has no problem with that. The quoted parameter does not expand equally in every case. For example, with MDIR='/windows/c/Documents and Settings/PointedEars' (and a corresponding file existing) the `for' parameter "$MDIR"/Pointed* expands to '/windows/c/Documents and Settings/Pointed*' while $MDIR/Pointed* expands to /windows/c/Documents and Settings/PointedEars (only that space is in $IFS so this is not feasible either.) Your suggestion "$MDIR"/{load,src} only "works" because pathname expansion is _not_ performed there, only parameter and brace expansion. >>> [...] >>> MDIR=/phx >>> for i in $MDIR/{load,utility,src} >>> do >>> cd $i >>> find $i ! -type d -print >>> done > > If load, utility, and src are in the same directory this won't work, > because in the first iteration you change directory to 'load' and then > try to continue _from there_ to 'utility', and finally to 'src'. > > But you can perform the commands in a subshell > [...] > And you should check whether the cd succeeded before continuing. > [...] > But why, in the first place, do you perform the cd if you're providing > the absolute path to find anyway? Why, in the first place, are they using `for' here and not > [...] >> find $MDIR/{load,utility,src} ! -type d > [...] ? PointedEars
From: pk on 29 Mar 2010 13:41 Thomas 'PointedEars' Lahn wrote: >>>>> If what you're saying was true, then >>>>> >>>>> for i in * >>>>> >>>>> will never work when filenames have $IFS in them. >>> The point is that $MDIR/{load,src} may contain $IFS. >> >> The point is that you should quote it >> >> "$MDIR"/{load,src} >> >> The 'for' construct has no problem with that. > > The quoted parameter does not expand equally in every case. For example, > with > > MDIR='/windows/c/Documents and Settings/PointedEars' > > (and a corresponding file existing) the `for' parameter > > "$MDIR"/Pointed* > > expands to > > '/windows/c/Documents and Settings/Pointed*' No, it expands to that only if no matching file exists, otherwise it expands to a list of all the matching files. > while > > $MDIR/Pointed* > > expands to > > /windows/c/Documents and Settings/PointedEars > > (only that space is in $IFS so this is not feasible either.) Your > suggestion > > "$MDIR"/{load,src} > > only "works" because pathname expansion is _not_ performed there, only > parameter and brace expansion. Of course pathname expansion is performed there, as usual, altough the result after the expansion is the same as it was before as there are no wildcards.
From: Thomas 'PointedEars' Lahn on 29 Mar 2010 14:22 Thomas 'PointedEars' Lahn wrote: > Janis Papanagnou wrote: >> [...] >> The point is that you should quote it >> >> "$MDIR"/{load,src} >> >> The 'for' construct has no problem with that. > > The quoted parameter does not expand equally in every case. For example, > with > > MDIR='/windows/c/Documents and Settings/PointedEars' > > (and a corresponding file existing) Sorry, I meant: MDIR='/windows/c/Documents and Settings/' and the file /windows/c/Documents and Settings/PointedEars existing. > the `for' parameter > > "$MDIR"/Pointed* > > expands to > > '/windows/c/Documents and Settings/Pointed*' > > while > > $MDIR/Pointed* > > expands to > > /windows/c/Documents and Settings/PointedEars > > (only that space is in $IFS so this is not feasible either.) [...] -- PointedEars
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Sed & Awk (was: Re: Printf - Columns/Tables) Next: building the printf format for a variable number of args |