Re: list directories recursive using a function ( ksh, ksh93)

On Sat, 26 Jan 2013 19:54:05 +0000, Martijn Dekker wrote:

> In article <ke0e0n$qfm$1@news.m-online.net>,
>  Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
> 
>> > #!/bin/sh
>> > 
>> > f_lsr() (
>> >    cd "$1" || return for file in .* *; do
>> >       case "$file" in "." | ".." | ".*" | "*" ) continue ;;
>> >       esac printf '%s/%s\n' "$1" "$file"
>> >       test -d "$file" -a ! -L "$file" && f_lsr "$1/$file"
>> >    done cd ..
>> > )
>> 
>> A final "cd .." in a sub-shell context?
> 
> Yeah, that's a bit pointless, isn't it? I should have removed that.
> 
>> All the pattern expressions (like ".*" and "*") inside case quoted?
> 
> Yes, we are skipping any unresolved glob patterns which would otherwise
> be printed as literal glob patterns; this occurs if any directory
> doesn't contain files matching the pattern in question. So, so skip
> those, it's necessary to quote them to keep 'case' from interpreting
> them as glob patterns.
> 
> Quoting the "." and ".." is optional since these aren't glob pattern
> metacharacters, but it seems like good style.
> 
> - Martijn

Whilst it is good (and correct) that you are quoting the patterns, the 
logic is false.

Consider a directory which has a single file in it with a name of the 
single character *

The glob pattern in
   for file in .* *
will return ".*" (as the shell will return the glob pattern unchanged if 
there is no match) and "*" (as the "*" glob pattern will match the 
literal filename "*").

So by all means use a case statement to ignore "." and ".." (or you can 
extend the list of patterns you give to the for), you need to use 
something like "test" to see if "*" is the result of the shell returning 
"*" from globbing "*" because it doesn't match or because it matches a 
literal "*".

Some shells have an optional feature to make a failed glob expansion 
return an empty string.
0
un193 (82)
1/26/2013 8:39:16 PM
comp.unix.shell 15376 articles. 1 followers. Post Follow

1 Replies
171 Views

Similar Articles

[PageSpeed] 54

In article <U9XMs.124671$kN2.72846@fed02.iad>,
 Icarus Sparry <i.sparry+un@gmail.com> wrote:

[...]
> So by all means use a case statement to ignore "." and ".." (or you can 
> extend the list of patterns you give to the for),

The "." and ".." are ignored to prevent infinite directory traversal 
loops.

> you need to use 
> something like "test" to see if "*" is the result of the shell returning 
> "*" from globbing "*" because it doesn't match or because it matches a 
> literal "*".

Yes, I was aware of that and stated this in my original message:

> > A third bugfix: if there are no dot-files or files in a directory, one 
> > of the literal glob patterns ".*" or "*" was reported. The version below 
> > skips those, but would also skip any files literally named ".*" or "*" - 
> > something that would not usually occur but is in fact allowed. An 
> > interesting article about the problems this can cause is at: 
> > http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html

None of my Unix filesystems contain any files named "*" or ".*", nor 
should they. I didn't think it was worth the effort to do this 
"correctly" given the problematic lack of restrictions in Unix filenames.

On the other hand, your message made me think some more and it isn't 
actually that hard at all:

#!/bin/sh

f_lsr() (
   cd "$1" || return
   for file in .* *; do
      case "$file" in
      "." | ".." ) continue ;;
      ".*" | "*" ) test -e "$file" || continue ;;
      esac
      printf '%s/%s\n' "$1" "$file"
      test -d "$file" -a ! -L "$file" && f_lsr "$1/$file"
   done
)

f_lsr "$1"
0
martijn1 (31)
1/28/2013 8:23:16 PM
Reply:

Similar Artilces:

list directories recursive using a function ( ksh, ksh93)
Hi I would like to mention that i know "ls" and "find" to traverse a directory structure recursive. The goal is to get into each directory and list each directory WITHOUT using find or ls recursive! So somehow a find rewritten in shell code. Example: Directory structure looks like ../1/2/3/ ../1/3/3/ ../2/3/1/ ../2/3/2/ The result should look like 1 1/2 1/2/3 1/3 1/3/3 2 2/3 2/3/1 2/3/2 Script cd /tmp mkdir -p ./1/2/3/ ./1/3/3/ ./2/3/1/ ./2/3/2/ cd - cat lsr.sh #!/usr/bin/ksh f_lsr() { cd $1 for dir in $( find . ! -name . -type d -prune ) ; do echo "$PWD/$dir" f_lsr $PWD/$dir done } f_lsr /tmp/ The result is /tmp/./1 /tmp/1/./2 /1/2/./3 /tmp/1/2/3/./3 .... /tmp/1/2/3/./3: [No such file or directory] /tmp/1/2/3/./2 .... /tmp/1/2/3/./2: [No such file or directory] Any hints ? tia Hajo In article <c4478dea-6eef-4281-b657-bd090bcd6d97@u16g2000yqb.googlegroups.com>, Hajo Ehlers <service@metamodul.com> wrote: > Hi > I would like to mention that i know "ls" and "find" to traverse a > directory structure recursive. > > The goal is to get into each directory and list each directory WITHOUT > using find or ls recursive! > So somehow a find rewritten in shell code. > > Example: > Directory structure looks like > > ./1/2/3/ > ./1/3/3/ > ./2/3/1/ > ./2/3/2/ > > The ...

Re: exporting a list of functions for use in a C function
Hi, As recommended by others, you should use CForm. I've appended to this message a function that I use to make minor alterations in the output of CForm, such as Power -> pow, to yield a more standard form in C++. I hope it helps. Tom Burton On 11/5/03 6:17 AM, in article bob7qo$qu1$1@smc.vnet.net, "Nathan Moore" <nmoore@physics.umn.edu> wrote: > I've composed a large list of functions in Mathematica that I'd like to > have a c function evaluate. To my knowledge c and C++ reject the > convention x^2 and prefer either x*x or pow(x,2). ...

Re: How to use Thread when second argument to function is a list of lists?
small correction: ref (me) > This function takes as input the row number and the matrix itself and > returns a list that contain the non-zero elements in the that row. > > i.e. function is f[ list , matrix]:=Module[{},.....] > The above should be f[ rowNumber , matrix]:=Module[{},.....] i.e. the function f[] takes as argument a number and the matrix. The number is the row number to process. Nasser ...

Re: Define function using lists or tables
Hello. Possibly this is a very simple question. Is there a way to define a function over a (BIG) list of ordered pairs? I mean, suppose that I have a list, created using a Table command or something like that, of the form [{a,b},{c,d},etc.] such that, after naming it f, say, f = [{a,b},{c,d},etc.], then, afterwards, whenever there is a command such that one needs to compute f[a] (or f[c]), the program immediately replaces f[a] by b,and f[c] by d? Thank you. Hi, Joaquim, One way may be like the following. This is a list to play with: lst = Partition[Map[ToExpression, CharacterRange["a", "z"]], 2] {{a, b}, {c, d}, {e, f}, {g, h}, {i, j}, {k, l}, {m, n}, {o, p}, {q, r}, {s, t}, {u, v}, {w, x}, {y, z}} Let us take, for example, o and try with it. Its position is Position[lst, o] {{8, 1}} We only need the position of the corresponding pair: Position[lst, o][[1, 1]] 8 Then the second pair member, p, has the coordinate [[8,2]]: lst[[Position[lst, o][[1, 1]], 2]] p Now we can write a function doing all this: f[lst_, x_] := lst[[Position[lst, x][[1, 1]], 2]] Let us try it: Map[f[lst, #] &, {a, e, i, w}] {b, f, j, x} Have fun, Alexei Alexei BOULBITCH, Dr., habil. IEE S.A. ZAE Weiergewan, 11, rue Edmond Reuter, L-5326 Contern, LUXEMBOURG Office phone : +352-2454-2566 Office fax: +352-2454-3566 mobile phone: +49 151 52 40 66 44 e-mail: alexei.boulbitch@iee.lu ...

which function should I use to list all of the FILE SYSTEMS in UNIX??
I want to make a program to minitor the usages of all of the file systems on my AIX or Linux systems. But I will not use shell commands like "df -k" for some FILE SYSTEMS maybe not mounted and "df -k" will not collect info about that FILE SYSTEM ,so I want to obtain theses info from system calls or lib functions . First I want to list all the FILE SYSTEMS on my OS ,which c function should I use ? Thanks! Tommy <oract666@gmail.com> wrote: > I want to make a program to minitor the usages of all of the file systems on > my AIX or Linux systems. > But I wi...

which function should I use to list all of the FILE SYSTEMS in UNIX??
I want to make a program to minitor the usages of all of the file systems on my AIX or Linux systems. But I will not use shell commands like "df -k" for some FILE SYSTEMS maybe not mounted and "df -k" will not collect info about that FILE SYSTEM ,so I want to obtain theses info from system calls or lib functions . First I want to list all the FILE SYSTEMS on my OS ,which c function should I use ? Thanks! Tommy wrote: > I want to make a program to minitor the usages of all of the file systems on > my AIX or Linux systems. comp.unix.programmer is down the h...

Re: Define function using lists or tables #2
Hello. Possibly this is a very simple question. Is there a way to define a function over a (BIG) list of ordered pairs? I mean, suppose that I have a list, created using a Table command or something like that, of the form [{a,b},{c,d},etc.] such that, after naming it f, say, f = [{a,b},{c,d},etc.], then, afterwards, whenever there is a command such that one needs to compute f[a] (or f[c]), the program immediately replaces f[a] by b,and f[c] by d? Thank you. Hi, Joaquim, One way may be like the following. This is a list to play with: lst = Partition[Map[ToExpression, CharacterRange["a", "z"]], 2] {{a, b}, {c, d}, {e, f}, {g, h}, {i, j}, {k, l}, {m, n}, {o, p}, {q, r}, {s, t}, {u, v}, {w, x}, {y, z}} Let us take, for example, o and try with it. Its position is Position[lst, o] {{8, 1}} We only need the position of the corresponding pair: Position[lst, o][[1, 1]] 8 Then the second pair member, p, has the coordinate [[8,2]]: lst[[Position[lst, o][[1, 1]], 2]] p Now we can write a function doing all this: f[lst_, x_] := lst[[Position[lst, x][[1, 1]], 2]] Let us try it: Map[f[lst, #] &, {a, e, i, w}] {b, f, j, x} Have fun, Alexei Alexei BOULBITCH, Dr., habil. IEE S.A. ZAE Weiergewan, 11, rue Edmond Reuter, L-5326 Contern, LUXEMBOURG Office phone : +352-2454-2566 Office fax: +352-2454-3566 mobile phone: +49 151 52 40 66 44 e-mail: alexei.boulbitch@iee.lu ...

Re: [ques]use recursive function to print out n!
�� �ޭz�manoopkammaran@gmail.com (AK)�n���ʨ��G > Hi > This is quite easy. You don't need 2 for-loops for that. > When you get questions like this, keep in mind that all the elements > need not be put in a single loop. > Some of them maybe formaing a series while others have to be done > seperately. > In your case, the first 10! can be done seperately. The rest forms a > series, as > 10!, (10-1)! > 9!, (9-1)! > ..... > ...... > ......... > 1!,(1-1)! > Since you already have a recursive function to computer the factorial > of a number called fac...

Re: How to use Thread when second argument to function is a list
On 3/10/08 at 2:04 AM, petsie@dordos.net (Peter Pein) wrote: >Nasser Abbasi schrieb: >>I have a function which I want to call repeatedly for all rows of a >>matrix to find non-zero elements in each row. This function takes >>as input the row number and the matrix itself and returns a list >>that contain the non-zero elements in the that row. >this is a task for Map: >In[1]:= findNonZeroElements[row_, p_] := >Select[p[[row]], #1 != 0 & ] >mchain[p_] := >(findNonZeroElements[#1, p] & ) /@ Length[p]] >p = {{1, 0, 0, 0, 0}...

Re: How to use list of variables inside Catx function ??
Thank you Nat it worked I forgot "OF" inside the Catx function SL On Tue, Sep 1, 2009 at 5:12 PM, Nathaniel Wooding <nathaniel.wooding@dom.com > wrote: > Learner: > > First, remove the space between col and the colon. It should read col: > > Then, an "of" in front of it. > > data learner; > col1='a'; > col2='b'; > col3 = 'c'; > Txdecod = Catx(',',of Col: ); > proc print;run; > > > Nat Wooding > > -----Original Message----- > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] ...

Re: Using sas dataset from different Unix directory
Dear SAS-L-ers, Rathindronath posted the following: > I need to use a sas dataset that is located in a different > unix directory (I am also using unix sas). How can I get that > dataset from a different unix directory to merge it with a > dataset that is in my present working directory. Thanks > Rathindronath, without meaning to hurt your feelings: This seems like such a basic question, that I am not sure whether you have not phrased it correctly, I just don't get it, or it is really a basic question. No matter what the situation, I so vow that you shall not leave this...

RE: List arguments to functions
Chris, If one wants to do parallel operations on the elements of two equal length lists, and then do something with the resulting outputs, the magic command is Inner. For example, to add the elements of the two lists and then leave them in a list (not what you want)... Inner[Plus, {1, 2, 3}, {4, 5, 6}, List] {5, 7, 9} To get what you want use... Inner[Plus, {1, 2, 3}, {4, 5, 6}, Plus] 21 If you wanted to take 2 times the element in the first list and add it to the square of the corresponding element in the second list and then multiply the results, you could use... Inner[2#1 + #2^2 &, {1, 2, 3}, {4, 5, 6}, Times] 21924 For some reason it took me a long time to get a simple understanding of the Inner command. Basically it is a way to make a parallel combination of the elements of two equal length lists and then do something with the results. David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ From: Chris Rozell [mailto:crozell@rice.edu] I have basic experience with Mathematica, but this problem has stumped me for the last day or so. Any help would be appreciated. I have a function that needs to take two lists as arguments, perform operations on them, and then sum the resulting list. However, by including the Plus function, it operates on each list item separately (which is exactly the opposite of what I need. For a simple example, >myfunc[x_, y_] = Apply[Plus, x + y]; >myfunc[{1, 2, 3}, {4, 5...

Searching Windows Directories Using Unix Shell Script
Hi, I want to get inputs from you all, if below task can be done through batch scripting or VB scripting. I have a unix script currently running on Sun Solaris (SunOS 5.10 Generic_142900-15 sun4u sparc SUNW,Sun-Fire-V440). This script takes directory name as input and searches for some patterns (both Regular expression based and strings) in all files and directories under the passed directory location. My questions are =96 1) Can I use this unix script to search the patterns on windows? Basically I want to pass windows directory location and get the output. 2) If yes, how to go...

Re: How to use list of variables inside Catx function ?? #2
Learner: First, remove the space between col and the colon. It should read col: Then, an "of" in front of it. data learner; col1='a'; col2='b'; col3 = 'c'; Txdecod = Catx(',',of Col: ); proc print;run; Nat Wooding -----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of SAS_learner Sent: Tuesday, September 01, 2009 8:05 PM To: SAS-L@LISTSERV.UGA.EDU Subject: How to use list of variables inside Catx function ?? Hello all, I am getting list of Txdecod's after transposing data set, Right now I know that ...

Re: Applying a function to a list of functions
> I'm sure that what I want here is extremely simple to do and I'm just > missing a CL function... I'd like to pass the same argument to a list > of functions and apply those results (as a list) to another function -- > at runtime. For example: > > ; Some functions > (setf alpha #'alpha-char-p) > (setf digit #'digit-char-p) > > ; A list of the above functions > (setf functions (list alpha digit)) > > ; Test to see if 'c' is true for any item in the list > (defun is-alpha-numeric (c) > (apply #'or (map...

Re: Re: function of a function
On 30 Nov 2005, at 14:07, Narasimham wrote: > Jens-Peer Kuska wrote: > >> it can't work because f [0] ==1 given in your differential equation >> f ' [0]==f [1] and NDSolve[] can't find the value for >> f[1] until it has integrated the equation. > > ??? > >> The nested dependence is equivalent to an infinite >> system of ordinary differential equations and it seems to be >> hard to do this by a finte computer. > > I cannot understand this. In the following two examples the first one > works, not the second. > > Clear[x,f,EQ]; > EQ={f'[x] == f[Cos[x]],f[0]== 1}; > NDSolve[EQ,f,{x,0,4}]; > f[x_]=f[x]/.First[%]; > Plot[f[x],{x,0,4}]; > > Clear[x,f,EQ]; > EQ={f'[x] == Cos[f[x]],f[0]== 1}; > NDSolve[EQ,f,{x,0,4}]; > f[x_]=f[x]/.First[%]; > Plot[f[x],{x,0,4}]; Surely, you mean the second one works, the first one does not!? Also, I think I agree with Jens. These cases are quite different and the problem he mentione does not arise in the second case. Ine the second case the derivative at a point x is defined only in terms of the value of the function at x. Thus values of the function, it's derivative, function etc, can be computed sequentially. In the first case, however, in order to compute the derivative at x you need to know the value of the function at Cos[x], which in general will not be known yet. This ...

Re: Using sas dataset from different Unix directory #2
On Thu, 18 May 2006 12:58:47 -0400, Rathindronath <mehedisas@YAHOO.COM> wrote: >I need to use a sas dataset that is located in a different unix directory >(I am also using unix sas). How can I get that dataset from a different >unix directory to merge it with a dataset that is in my present working >directory. >Thanks I am not going to provide a direct answer but I would encourage you to look at the LIBNAME statement in the online doc. This will tell you how to associate a path to a LIBREF to refer to a dataset in a particular location. http://support.sas.com/onlinedoc/...

Re: How to use lookup function to merge data from a standard list
I think that what you mean is that you have two data sets with an equal = number of rows that you want to merge together, and they match row for = row, but you don't have an ID field on data 1. =20 Try this: data data1; obsnum + 1; run; data data2; obsnum + 1; run; data mergeset; merge data1 data2; by obsnum; run; -Mary ----- Original Message -----=20 From: jn mao=20 To: SAS-L@LISTSERV.UGA.EDU=20 Sent: Wednesday, April 16, 2008 2:27 PM Subject: How to use lookup function to merge data from a standard list = file Hello SAS-Ls, I have a standard list file...

Re: How to use lookup function to merge data from a standard list #3
No, the data1 has 100 category and 200 data, var1 has data with fall in 100 category (category). Data2 has 400 subjects. I need to find each subject fall in which category according to var2. Mary <mlhoward@avalon.net> wrote: I think that what you mean is that you have two data sets with an equal number of rows that you want to merge together, and they match row for row, but you don't have an ID field on data 1. Try this: data data1; obsnum + 1; run; data data2; obsnum + 1; run; data mergeset; merge data1 data2; by obsnum; run; -M...

Re: How to use lookup function to merge data from a standard list #4
I geez Ill give you the easy version (however id normally do this with a Hash). Proc SQL ; Create Table Need As Select Tbl2.* , Tbl1.Category From Table2 As Tbl2 Left Join Table1 As Tbl1 On Tbl1.Var1 = Tbl2.Var2 ; Quit ; Toby Dunn "Don't bail. The best gold is at the bottom of barrels of crap." Randy Pausch "Be prepared. Luck is where preparation meets opportunity." Randy Pausch > Date: Wed, 16 Apr 2008 12:55:10 -0700 > From: jn_mao@YAHOO.COM > Subject: Re: How to use lookup function to merge data from a standard...

Re: How to use lookup function to merge data from a standard list #2
Or just do this: data mergeset; merge data1 data2; run; And avoid some completely unnecessary I/O. On Wed, Apr 16, 2008 at 3:39 PM, Mary <mlhoward@avalon.net> wrote: > I think that what you mean is that you have two data sets with an equal number of rows that you want to merge together, and they match row for row, but you don't have an ID field on data 1. > > Try this: > > data data1; > obsnum + 1; > run; > > data data2; > obsnum + 1; > run; > > data mergeset; > merge data1 data2; > by obsnum; > run; > > -Mary > &...

Re: How to use lookup function to merge data from a standard list #5
I see. Let me see if I can guess what your problem is. Data 1 has 100 categories (variables?) and 200 observations. Or, data 1 has a variable (var1) that has 100 different values and 200 observations. Data2 has 400 subjects (400 observations?). I think I don't know enough about var2. On Wed, Apr 16, 2008 at 3:55 PM, jn mao <jn_mao@yahoo.com> wrote: > No, the data1 has 100 category and 200 data, var1 has data with fall in 100 category (category). Data2 has 400 subjects. I need to find each subject fall in which category according to var2. > > > > Mary <mlhoward@a...

Re: How to use lookup function to merge data from a standard list #6
jn mao wrote: > Hello SAS-Ls, > > I have a standard list file with 100 category. And I have another > data2 file with all subjects. In data2, var2 need be matched to the > list file, and find the category it belongs. > > data1 (list file): > var1 category > aa 1 > bb 1 > cc 2 > > data2: > Id var2 > 1 aa > 2 cc > 3 bb > > I need add category to data2. When I tried use merge, I lost all ID > data which means all ID data become missing. Could someone help me > to add the categ...

Re: How to use lookup function to merge data from a standard list #7
"Richard A. DeVenezia" <rdevenezia@wildblue.net> wrote: jn mao wrote: > Hello SAS-Ls, > > I have a standard list file with 100 category. And I have another > data2 file with all subjects. In data2, var2 need be matched to the > list file, and find the category it belongs. > > data1 (list file): > var1 category > aa 1 > bb 1 > cc 2 > > data2: > Id var2 > 1 aa > 2 cc > 3 bb > > I need add category to data2. When I tried use merge, I lost all ID > data which means all ID data become missing. Could someone help me > to...

Web resources about - Re: list directories recursive using a function ( ksh, ksh93) - comp.unix.shell

List of web directories - Wikipedia, the free encyclopedia
Open Directory Project (a.k.a. ODP or dmoz) – The largest directory of the Web. Its open content is mirrored at many sites, including the Google ...

To Complement Its Search Engine, Facebook Launches People, Pages, and Places Directories
Facebook is now closer than ever to becoming the phone book for the internet. The site recently launched a Directory of people , Pages , and ...

Directories - ARN
The source for IT industry news, views and analysis across the channel, business and technology

Princess Diana leaked royal directories to Murdoch tabloid, court hears
The former royal editor of Rupert Murdoch's now defunct British tabloid the News of the World told a court on Thursday that the late Princess ...

Telstra slashes 800 jobs from directories arm Sensis
TELSTRA is slashing 800 jobs from its struggling directories arm Sensis just one month after the telco giant sold a 70 per cent stake in the ...

Government To Stop Publishing Contact Information In Blue Pages Of Phone Directories
... has decided to stop publishing contact information for all of its departments and agencies in the blue pages section of telephone directories.A ...

File System Programming Guide: About Files and Directories
Explains how to create and manage files and directories.

Fixing directories
Web directories are one of the key points of differentiation between plans on the Exchanges. A single company can offer half a dozen networks ...

Pittsburgh Business Times Local Directories
... email newsletters, events, print subscription, digital subscription or other special offers. Welcome to Pittsburgh Business Times Local Directories. ...

Bradenton and Lakewood Ranch Guides - Directories and guides
The Bradenton Herald is your source for Bradenton and Lakewood Ranch directories and guides.

Resources last updated: 3/10/2016 9:22:30 AM