formatting ls's output to create script files

formatting ls's output to create script files

Post by Erick Parso » Sun, 12 Nov 1995 04:00:00



I am trying to find a way to add strings to ls's output in order
to create a script file to be used with bash. My trusty old Amiga
would do this handily with an lformat option using list. What would
this accomplish ?

For starters it would allow you to perform a function on every
file in a particular directory.

I'm hoping that this can be accomplished handily from within a
shell environment.

ADV thanks ANCE...

 
 
 

formatting ls's output to create script files

Post by d.. » Mon, 13 Nov 1995 04:00:00


: For starters it would allow you to perform a function on every
: file in a particular directory.

In sh(1) (or bash, etc.) you can use a construct resembling:

for i in `ls`
do
  # do whatever you need with the file ($i)
done

Each filename in turn in the directory will stand for $i for
each trip through the for loop above.

--
------------------------------------------------------------------------
David E. Fox                 Tax              Thanks for lettimg me


-----------------------------------------------------------------------

 
 
 

formatting ls's output to create script files

Post by Tad McClell » Tue, 14 Nov 1995 04:00:00





: }I am trying to find a way to add strings to ls's output in order
: }to create a script file to be used with bash. My trusty old Amiga
: }would do this handily with an lformat option using list. What would
: }this accomplish ?
: }
: }For starters it would allow you to perform a function on every
: }file in a particular directory.
: }
: }I'm hoping that this can be accomplished handily from within a
: }shell environment.
: }
: }ADV thanks ANCE...

: If i understand you, you want to do something to every file in a directory
: so in your script

: ls > /tmp/myfile
: for i in `cat /tmp/myfile`
: do
: commad you want to do
: done

no need to create an intermediate file and do a UUOC:
-----------------------------------
for i in `ls`
do
commad you want to do
done

--
  Tad McClellan,      Logistics Specialist (IETMs and SGML guy)

  All I want, is a little more than I'll ever get.

 
 
 

formatting ls's output to create script files

Post by Bill Marc » Thu, 16 Nov 1995 04:00:00




Quote:>If i understand you, you want to do something to every file in a directory
>so in your script

>ls > /tmp/myfile
>for i in `cat /tmp/myfile`
>do
>commad you want to do
>done

Useless use of temp file.
You could do
ls | (while read i; do command; done)
or
for i in *; do command; done
Of course, it isn't clear what the previous poster wanted.  Maybe he
wanted to do something with the output from 'ls -l'.  Awk would be useful
for that.

--
"Goodness me, could this be industrial disease?"  Dire Straits

 
 
 

formatting ls's output to create script files

Post by Stanislaw Streln » Sat, 18 Nov 1995 04:00:00



: I should have been a little clearer in my original post.
: /tmp/myfile should be edited before the for loop is run on it.
: If you don't, there may be a few problems;
:       1. If you are doing something like rm $i, your script will get
:        blown away before all the rest of the files in the directory.
:        I like to be sure I am working on the files I  think I am working
:        on.

:       2. If you have ls aliased or a function in your .profile like;
:               ls()
:               {
:               ls -xFr
:               }
:               your `ls` will not return a single column that can be
:               passed to the for loop.
:       3. Yes, for i in `ls` may work, but after you waste an important
:         file by not checking your list, you may find an extra process
:         is not always a bad thing!:-)
: --
: Doug
: --------------------------------------------------------------------------------


#!/bin/sh

for i in *
do
if [ $i != $0 ]; then   #or whatever condition you need
your_commands_here
fi
done

   Does it solve the problem?

--

          St.Strelnik

There's no future in time travel