shell programming issue

shell programming issue

Post by yusuf » Sat, 04 Mar 2006 02:49:13



Hi, I am actually trying to figure out how you would take a command
that generated multi lines of output (like ls) and then run some
function on each line that generated.

[ -d `find . -print` ]

Should the above command not compare each item from the `find . -print`
to see if its a directory then print it out?

I get a 'too many params...' message from bash. I think this is because
the whole output of 'find ...' is being sent straight to [ ] in a
single shot. How would I code something to take each line from `find
...` and do the compare, one at a time?

Thanks.

 
 
 

shell programming issue

Post by Janis Papanagno » Sat, 04 Mar 2006 03:07:22



> Hi, I am actually trying to figure out how you would take a command
> that generated multi lines of output (like ls) and then run some
> function on each line that generated.

> [ -d `find . -print` ]

> Should the above command not compare each item from the `find . -print`
> to see if its a directory then print it out?

No. I think you mean...

   find . -type d -print

i.e. let find select only directories.

Then you can either add more options to find (like -exec; see man find)
or use xargs to process a bunch of files as in

   find . -type d -print | xargs ...

Depending on how pathological your directory names can be you may need
to use -print0 and xargs -0 instead.

Janis

Quote:> I get a 'too many params...' message from bash. I think this is because
> the whole output of 'find ...' is being sent straight to [ ] in a
> single shot. How would I code something to take each line from `find
> ...` and do the compare, one at a time?

> Thanks.


 
 
 

shell programming issue

Post by bill » Sat, 04 Mar 2006 03:07:41


Think I might do it a little different.
foreach f (`find . -print`)
if ( -d $f )
  print $d
endif
end

close csh syntax

 
 
 

shell programming issue

Post by Chris F.A. Johnso » Sat, 04 Mar 2006 03:08:38



> Hi, I am actually trying to figure out how you would take a command
> that generated multi lines of output (like ls) and then run some
> function on each line that generated.

> [ -d `find . -print` ]

   I presume that this is a hypothetical example, because you would
   use the '-type d' operand to find to get directories.

Quote:> Should the above command not compare each item from the `find . -print`
> to see if its a directory then print it out?

   No. Where in the man (or info or help) page does it say that test
   (a.k.a. '[') performs a loop?

Quote:> I get a 'too many params...' message from bash. I think this is because
> the whole output of 'find ...' is being sent straight to [ ] in a
> single shot.

   Of course it is.

Quote:> How would I code something to take each line from `find
> ...` and do the compare, one at a time?

   Use a loop:

find . -print |
      while IFS= read -r file
      do
           : do whatever
      done

   Or use the -exec operand to find:

find . -exec my_command {} \;

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence

 
 
 

shell programming issue

Post by Stephane CHAZELA » Sat, 04 Mar 2006 03:18:48


2006-03-2, 09:49(-08), yusufm:

Quote:> Hi, I am actually trying to figure out how you would take a command
> that generated multi lines of output (like ls) and then run some
> function on each line that generated.

> [ -d `find . -print` ]

> Should the above command not compare each item from the `find . -print`
> to see if its a directory then print it out?

find can check for the type of the file, so you can do:

find . -type d -print

However, contrary to "[ -d" that returns "true" for a symbolic
link that point to a directory, find -type d will not, unless
you use the -L option to find (formerly -follow) but in that
case, find would follow those symlinks and recursing into the
directory it points to.

Quote:

> I get a 'too many params...' message from bash. I think this is because
> the whole output of 'find ...' is being sent straight to [ ] in a
> single shot. How would I code something to take each line from `find
> ...` and do the compare, one at a time?

[...]

That would be the wrong way to go.

If you want to execute something for every file find selects,
then simply use the -exec option:

find . -exec "[" -d "{}" "]" \; -print

Would run "[" for every file. Some systems forget to add a "["
command in /bin. So, you may have its other name "test".

find . -exec test -d {} \; -print

In that case, you can tell find to only run the "[" command only
for symbolic links as it can find by itself about directories:

find . \( -type d -o -type l -exec '[' -d '{}' ']' \; \) -print

--
Stphane

 
 
 

shell programming issue

Post by yusuf » Sat, 04 Mar 2006 03:39:28


I learned how to cycle through output using:

for f in `find / -print` ; do if [ -d "$f" ] ; then  echo "$f" ; fi ;
done

initially I was wondering if a loop was really necessary like the one
above, or could this have been done by redirecting or piping output
from find somehow.

Thx.


> 2006-03-2, 09:49(-08), yusufm:
> > Hi, I am actually trying to figure out how you would take a command
> > that generated multi lines of output (like ls) and then run some
> > function on each line that generated.

> > [ -d `find . -print` ]

> > Should the above command not compare each item from the `find . -print`
> > to see if its a directory then print it out?

> find can check for the type of the file, so you can do:

> find . -type d -print

> However, contrary to "[ -d" that returns "true" for a symbolic
> link that point to a directory, find -type d will not, unless
> you use the -L option to find (formerly -follow) but in that
> case, find would follow those symlinks and recursing into the
> directory it points to.

> > I get a 'too many params...' message from bash. I think this is because
> > the whole output of 'find ...' is being sent straight to [ ] in a
> > single shot. How would I code something to take each line from `find
> > ...` and do the compare, one at a time?
> [...]

> That would be the wrong way to go.

> If you want to execute something for every file find selects,
> then simply use the -exec option:

> find . -exec "[" -d "{}" "]" \; -print

> Would run "[" for every file. Some systems forget to add a "["
> command in /bin. So, you may have its other name "test".

> find . -exec test -d {} \; -print

> In that case, you can tell find to only run the "[" command only
> for symbolic links as it can find by itself about directories:

> find . \( -type d -o -type l -exec '[' -d '{}' ']' \; \) -print

> --
> Stphane

 
 
 

shell programming issue

Post by Stephane CHAZELA » Sat, 04 Mar 2006 04:05:01


2006-03-2, 10:39(-08), yusufm:
Quote:> I learned how to cycle through output using:

> for f in `find / -print` ; do if [ -d "$f" ] ; then  echo "$f" ; fi ;
> done

> initially I was wondering if a loop was really necessary like the one
> above, or could this have been done by redirecting or piping output
> from find somehow.

[...]

The loop above is not correct.

Please have a look at question 14 in the FAQ.
http://home.comcast.net/~j.p.h/cus-faq-2.html#14

Command substitution (`...`) has to be used with case.

Generally, it's a bad habit to write a loop in shell. It's
better to call tools that do the loop. find loops through files,
it doesn't make sense to have it do its loop, get its output,
and re-loop on it (and wrongly, morever), simply use find's loop
by using the "-exec" option.

--
Stphane

 
 
 

1. Apache, cgi forms, cgi programs - issues - restart resolves issue??

This is a very strange occurance.

Running Apache 1.3.26 or other versions (tried several)
Sun Ultra 2 w/1536 meg
Solaris 8

Same issue on other machines running similar configs

We are running several web servers on this box.
Maybe about 25 total. We also use a shopping cart program
written in cgi. Our email form scripts are written in cgi.
Ok, here is the weird and strange happenings we are running into.

Error log reports malformed headers when a form is submitted
while this is happening.

Every so often the shopping cart program will not allow
you to "view the next 25 items". Clicking that button just brings them
back to the shopping carts home page.
When this happens with the cgi forms, submitting the form
returns an error as if their is a field missing and there are not any
missing or empty fields.

Now if I "./apachectl restart", both of the above seems to work fine.
Then a couple hours, sometimes a day, sometimes a few days or even a week later
it happens again. We never know unless a client calls about the form
not working.

I'm thinking there may be some sort of process that gets killed.
Any ideas on what that process would be?
Any way to monitor the system to find out what process that would
be or what is happening to it?

I am lost with this issue.
Any help is greatly appreciated.

Thanks

2. macdef macros (FTP)

3. Installing custom shells Was Re: Csh Programming Considered Harmful - comp.unix.shell #10160 (fwd)

4. Jun 28 NEW: FreedomCAD v 1.5

5. programming program or shell for gcc ?

6. awk problem: pattern matching & getline

7. How to program a shell '#!<program>' statement.

8. How do I build the 'midi' module?

9. Shell Programming Program

10. I wanna Program = "shell to C program"

11. Executing a program (shell script) from within a c program

12. Need a shell program to print return value of a program

13. How to write a C-shell program to print return value of a program