Metal Guitarist Forums banner
1 - 12 of 12 Posts

·
Slow Money
Joined
·
14,612 Posts
Discussion Starter · #1 ·
Im working in Bash, writing a fairly simple script (print the length of the longest filename in the working directory), but im having a bit of an issue with it. I can get the names of the files/dirs in the working directory by simply running ls -1, but i then nead to read in each line, compare it to the current working longest length, and replace it with the new length if longer. The web is startlingly devoid of basic help on how to read in lines with bash.

the lesson on bash scripts from school is no help either :/

How do i read in a line/arg in bash, and how do i know when there are no more args. Ive spent like 3 hours searching this shit on google to no avail.
 

·
(╯°□°)╯︵ ┻━&#
Joined
·
1,986 Posts
Im working in Bash, writing a fairly simple script (print the length of the longest filename in the working directory), but im having a bit of an issue with it. I can get the names of the files/dirs in the working directory by simply running ls -1, but i then nead to read in each line, compare it to the current working longest length, and replace it with the new length if longer. The web is startlingly devoid of basic help on how to read in lines with bash.

the lesson on bash scripts from school is no help either :/

How do i read in a line/arg in bash, and how do i know when there are no more args. Ive spent like 3 hours searching this shit on google to no avail.
Bash loops

Lazily, something like -
Code:
for i in `ls *`; 
do wc -l $i; 
done 2> /dev/null | sort -nr | head -n 1
err... I misunderstood the requirements. The previous command will print the name of the largest file. the following will print the length of the longest filename.

Code:
for i in `ls`;  do echo $i | wc -c ;  done 2> /dev/null | sort -nr | head -n 1
 

·
I am Groot
Joined
·
32,450 Posts
Eric beat me to it. For loops are your best friend for processing lists in bash. For future reference, you can also process files this way.

Code:
for I in `cat filename`
do
        stuff to do
done
 

·
Slow Money
Joined
·
14,612 Posts
Discussion Starter · #4 ·
Eric beat me to it. For loops are your best friend for processing lists in bash. For future reference, you can also process files this way.

Code:
for I in `cat filename`
do
        stuff to do
done
That was much easier to read :lol:

so i could run

Code:
LONGEST = 0
WORKING = 0
for I in `ls`
do
        $WORKING = ${#I}
        if [ $WORKING > $LONGEST ]; then
                $LONGEST = $WORKING
        fi
done
echo $LONGEST
or no?

Fuck bash :lol:

edit:
No, that does not give me what i want :lol:
 

·
I am Groot
Joined
·
32,450 Posts
^ Yeah, that's not even close. :lol:

You need to get the number of characters in each filename. The wc command will get you there, but by default it counts the number of characters INSIDE the file, not in the filename. You need to pipe it, like Eric showed you:

Code:
echo $I | wc -c
So, if you put that in your for loop, you'll get the length of every file name:

Code:
for I in `ls`
do
        echo $I | wc -c
done
I'll let you figure out the comparison part. After all, it is an assignment. ;)
 

·
Slow Money
Joined
·
14,612 Posts
Discussion Starter · #6 ·
Erics second shot gives me "1 1 5", when the longest filename in the directory is 9 letters long :lol:

Here is where i am so far
Code:
#!/bin/bash
longest="0"
working="0"
for i in $( ls)
do
        let working = (echo $i | wc -c)
        if [ $working -gt $longest ]
         then
                let longest = working
        fi
done
echo $longest
but i suspect i am a ways off still.

Line 6 throws an error.
Code:
./hello.bash: line 6: syntax error near unexpected token `('
./hello.bash: line 6: ` let working = (echo $i | wc -c)'
But i am not sure why. If my understanding is correct, the parenthasis are necessary, and without them, the script throws very berry many errors, but wtf.
 

·
I am Groot
Joined
·
32,450 Posts
Actually, you need backwards tick marks, which is shell speak for "execute everything inside the ticks".

Try this:

Code:
#!/bin/bash
longest="0"
working="0"
for i in $( ls)
do
        let working = `echo $i | wc -c`
        if [ $working -gt $longest ]
         then
                let longest = $working
        fi
done
echo $longest
Oh, and I fixed the "$" you left off before the "working" on line 9. ;)
 

·
Slow Money
Joined
·
14,612 Posts
Discussion Starter · #8 ·
Thank you. That is the kind of critical information im missing.

now how the hell do i type a backwards tick mark? :lol:
nevermind, its under the squigly :lol:
 

·
Slow Money
Joined
·
14,612 Posts
Discussion Starter · #9 ·
Code:
#!/bin/bash
longest="0"
working="0"
for i in $`ls`;
do
        let $working = `echo $i | wc -c`
        if [ $working -gt $longest ]
         then
                let longest = working
        fi
done
echo $longest
k, ive been hunting around for an hour or so, and i keep getting this
Code:
line 6: let: =: syntax error: operand expected (error token is "=" )
each iteration of the loop.

fwiw, each iteration the value of the wc -c is 1.
 

·
Slow Money
Joined
·
14,612 Posts
Discussion Starter · #10 ·
Wirelessly posted (Hivemind: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7)

Why did nobody tell me you can't have spaces, and why do mo Internet primers say this?! :lol:
 

·
I am Groot
Joined
·
32,450 Posts
I totally found the spaces thing the hard way, too. :lol:
 

·
Slow Money
Joined
·
14,612 Posts
Discussion Starter · #12 ·
Wirelessly posted (Hivemind: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7)

I've looked at 7 differen intro to bash things and not 1 mentioned that :lol:

I found it in a primer for korn where it said "just like bash..."

Hell :lol:

Wc didn't work however, I had to use expr length
 
1 - 12 of 12 Posts
Top