[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cobalt-users] OT: to you script-heros...



At 02:23 PM 3/31/2004, you wrote:
"> freitag lab. ag / nafroth" wrote:

> hoi,
>
> i know that here are some good sript writers... i got a small problem and i
> think, it is 5 mins for one...
>
> maybe someone can give me a hint:
>
> i need a shell script with syntax script /path/to/folder which prints the
> content (in may case pictures.jpg) on screen or piped in a file like 1.jpg,
> 2.jpg, 3.jpg (seperated by ",").
>
> to say in words: ls on a folder, remove every info except filename.ext and
> print it...
>
> any ideas?

Many come to mind... ;-)

How about this (not quite tested) script?

#!/bin/bash
OLDDIR=`pwd`
TARGET="$1"
cd $TARGET
comma="" ; for f in *; do echo -n "$comma$f"; comma=","; done
cd $OLDDIR


The above solution will work, but only as good as ls -m (which has been suggested by a number of people), but ls -mR gives a real mess in the event it needs to be recursive...I'd suggest the following:

this solution is only marginally better than ls -m *IF* there are sub-folders which must be traversed to produce a list of files in the specified format.


#!/bin/sh
(
cd $1
find . -type f -print | sed 's/$/, /'| tr -d '\012' | sed 's/\.\///g' | sed 's/, $//'
)



No need to drop into a folder and cd back if you open a sub-shell using ( and ) - as the new shell can cd, closing the sub-shell automatically puts you back at the starting folder.

The find uses the -type f to only find files, then substitutes the end of line with the ', ' .

Next removes the './' which is the relative to the working directory path qualifier, and then removes all newlines by using tr -d (delete) to remove the \012 (the new line) - this concatenates into one long line.

Lastly, remove the very final ', ' using the final sed.