[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: some function problems
"Alon Kadury" <kadury@hotmail.com> writes:
> i'm new to linux, just starting to learn, and have some problems:
Your problems are not really Linux-related, but what the hell...
Just as long as you realize that it's [somewhat] off-topic.
> 1. how do i write a function that gets as an argument a name of a
> directory and prints in depth?
I am not quite sure what you need to do, but it looks like you
might use (g)awk:
depth () {
find . -type d | gawk -F/ '
{if (NF > max) { max=NF }} END {printf("%d\n",max)}'
}
This might be off by 1, depending on how you define "depth"
> 2. i have written a function that is supposed to give me the
> file/directory with the biggest name
Again, it is not clear what you want, especially from your function.
You don't list files recursively, "wc" probably gives you a wrong
length (although it might be just off by one in all cases), you
execute a part of your function in a subshell for some reason... I
would do something like (assuming we insist on using bash):
maxname () {
max=0
for f in $(find ${1:-.} -printf "%f ")
do
len=$(echo -n $f | wc -c | tr -d ' ')
if [ $len -gt $max ]
then max=$len
name=$f
fi
done
echo "Longest is $name ($max)"
}
I would probably still use awk rather than bash - cleaner and faster:
maxname () {
find ${1:-.} -printf "%f\n" | gawk '
{if ((len=length($0)) > max) { max=len; name=$0 }}
END {printf("Longest is %s (%d)\n",name,max)}'
}
I leave it up to you to learn about find and gawk (you must, if you
are serious about Linux at all), and I suppose you'll figure out why I
used "echo -n" and what the hell ${1:-.} is (no, it is not a
smiley...).
--
Oleg Goldshmidt goldshmt@netvision.net.il
BLOOMBERG L.P. (BFM) oleg@bfr.co.il