LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-31-2008, 08:00 PM   #1
mcdcyex
LQ Newbie
 
Registered: Mar 2008
Distribution: Fedora 5
Posts: 3

Rep: Reputation: 0
Need some help with BASH scripts


Hello forum, I'm just getting into BASH scripting and need some help.

I need to write a function that takes a directory name as an argument and writes to standard output the maximum of lengths of all filenames in that directory. If the functionís argument is not a directory name, write an error message to standard output and exit with nonzero status.

Can anyone help me out? I'm sure its easy and I'm over thinking it
 
Old 03-31-2008, 09:45 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Sounds a little like homework, but here's a good place to start:

http://www.tldp.org/LDP/abs/html/fto.html

see also the following cmds:

ls -l
echo
 
Old 03-31-2008, 10:40 PM   #3
mcdcyex
LQ Newbie
 
Registered: Mar 2008
Distribution: Fedora 5
Posts: 3

Original Poster
Rep: Reputation: 0
actually thats exactly what is is... and yet both the book and the instructor haven't explained it well at all.

This is what the book gives for the answer, can someone break it down in plain English so I can understand it for the future:

$ function maxfn () {
> typeset -i max thisone
> if [ ! -d "$1" -o $# = 0 ]
> then
> echo "Usage: maxfn dirname"
> return 1
> fi
>
> max=0
> for fn in $(/bin/ls $1)
> do
> thisone=${#fn}
> if [ $thisone -gt $max ]
> then
> max=$thisone
> fi
> done
> echo "Longest filename is $max characters."
> }
 
Old 03-31-2008, 11:13 PM   #4
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
$ function maxfn () {
> typeset -i max thisone
> if [ ! -d "$1" -o $# = 0 ]
> then
> echo "Usage: maxfn dirname"
> return 1
> fi

This part checks whether enough arguments are passed. if the check fails then actual usage is echoed.

> max=0
> for fn in $(/bin/ls $1)
> do
> thisone=${#fn}
> if [ $thisone -gt $max ]
> then
> max=$thisone
> fi
> done
> echo "Longest filename is $max characters."
> }

for loop is taken which loops all the files one by one.
${#fn} - basically strings treated as array . so the total number of elements is taken up here.length of the file is got with this.
then is compared with $max. if greater then $max is swaped with the greatest one.

Gentoo

Last edited by prad77; 04-17-2008 at 04:35 AM.
 
Old 04-01-2008, 01:51 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Read/bookmark these:

http://www.tldp.org/LDP/abs/html/
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://rute.2038bug.com/index.html.gz
 
Old 04-01-2008, 02:32 AM   #6
ansumanr
LQ Newbie
 
Registered: Aug 2005
Location: India, Kolkata
Distribution: Redhat
Posts: 1

Rep: Reputation: 0
Smile Shell Scripting Help!!!!

Quote:
Originally Posted by mcdcyex View Post
Hello forum, I'm just getting into BASH scripting and need some help.

I need to write a function that takes a directory name as an argument and writes to standard output the maximum of lengths of all filenames in that directory. If the functionís argument is not a directory name, write an error message to standard output and exit with nonzero status.

Can anyone help me out? I'm sure its easy and I'm over thinking it
Dear Friend,

OK I understand ur problem regarding the bash shell scripting in Linux environment....one thing I wish to mention before I start that its quite simillar to Unix shell scripts.

Furthermore in this regard if you want any detail or in depth knopwledge I can help with a Tutorial of my own regarding the same.

In that case you can pass me ur mail id at ansumanr@gmail.com.

Thanks.
Best wishes.

Ansuman

Last edited by ansumanr; 04-01-2008 at 02:33 AM. Reason: Correcting E-mail ID
 
Old 04-01-2008, 07:25 AM   #7
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Rep: Reputation: 15
Quote:
Originally Posted by prad77 View Post
$ function maxfn () {
> typeset -i max thisone
> if [ ! -d "$1" -o $# = 0 ]
> then
> echo "Usage: maxfn dirname"
> return 1
> fi

This part checks whether enough arguments are passed. if the check fails then actual usage is echoed.

> max=0
> for fn in $(/bin/ls $1)
> do
> thisone=${#fn}
> if [ $thisone -gt $max ]
> then
> max=$thisone
> fi
> done
> echo "Longest filename is $max characters."
> }

for loop is taken which loops all the files one by one.
${#fn} - basically strings treated as array . so the total number of elements is taken up here.length of the file is got with this.
then is compared with $max. if greater then $max is swaped with the greatest one.
good explanation.
 
Old 04-01-2008, 07:39 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by ansumanr View Post
Dear Friend,

OK I understand ur problem regarding the bash shell scripting in Linux environment....one thing I wish to mention before I start that its quite simillar to Unix shell scripts.

Furthermore in this regard if you want any detail or in depth knopwledge I can help with a Tutorial of my own regarding the same.

In that case you can pass me ur mail id at ansumanr@gmail.com.

Thanks.
Best wishes.

Ansuman
This is not how to use LQ. Please post your help directly or post a link to it. This way, everyone benefits.
 
Old 04-01-2008, 08:08 AM   #9
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Rep: Reputation: 15
Cool

Quote:
Originally Posted by mcdcyex View Post
Hello forum, I'm just getting into BASH scripting and need some help.

I need to write a function that takes a directory name as an argument and writes to standard output the maximum of lengths of all filenames in that directory. If the functionís argument is not a directory name, write an error message to standard output and exit with nonzero status.

Can anyone help me out? I'm sure its easy and I'm over thinking it
let the directory name is ~/x

#!/bin/sh

ls ~/x > files.txt
wc -L files.txt



run this script it will give you maximum length of all the filenames
 
Old 04-01-2008, 08:53 AM   #10
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
General note to everyone posting code: it's much easier to read if you put it in a "Code:" block -- i.e. surround it w/ [CODE][/CODE] tags (see below).

While the intermediate text file files.txt may be helpful, it is not necessary:
Code:
ls  | wc -L
works, as does:
Code:
ls -1  | wc -L

Last edited by archtoad6; 04-01-2008 at 10:50 AM. Reason: typo
 
Old 04-01-2008, 09:59 AM   #11
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by chrism01 View Post
see also the following cmds:

ls -l
echo
Can be done without using "ls".
Code:
#!/bin/sh

print_max_file_len(){
    maxlen=0;
    tmpdir=`pwd`
    cd "$1"
    for i in *
    do
	len="${#i}"
	#echo "$i"
	#echo "$len"
	if [ $len -gt $maxlen ]; then
	    maxlen=$len
	fi
    done
    cd "$tmpdir"
    echo "$maxlen"
}

print_max_file_len "/"
I recommend reading this:
http://www.grymoire.com/Unix/Sh.html

Last edited by ErV; 04-01-2008 at 10:00 AM.
 
Old 04-01-2008, 10:48 AM   #12
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
mcdcyex,
Consider re-doing the script in light the above. I am not sure if I would recommend showing your result to your instructor, as that may put you on his/her bad side. Nor would I recommend sharing any criticisms we/I have of the text book.

The 1st & most major flaw w/ the script, as you presented it, is that it has no indentation for showing its structure. While there are various schools of programming style, I believe no professional would condone teaching no indentation style at all. Here is how I would have done it:
Code:
function maxfn () {
   typeset -i max thisone
   if [ ! -d "$1" -o $# = 0 ]
   then
      echo "Usage: maxfn dirname"
      return 1
   fi

   max=0
   for fn in $(/bin/ls $1)
   do
      thisone=${#fn}
      if [ $thisone -gt $max ]
      then
         max=$thisone
      fi
   done
   echo "Longest filename is $max characters."
}
"function" is redundant, the "()" is sufficient.

I have never had to use "typeset".

Using /bin/ls instead of plain ls could actually break the script & adds no security w/o extensive other testing. If I were being sarcastic I would label it something like "pedantic BS". Furthermore $(/bin/ls $1) isn't necessary at all -- $1/* (as in for F in $1/*) will work just as well, possibly better.

Personally, I like to start bash function names w/ a '_', that makes them instantly recognizable when called in other parts of the script.

I also abhor long variable names in short programs. They may be fine for 1,000+ line C programs, but they are unnecessary in the average Bash script. I also like the convention that Bash variable names should be all capital letters. Again, it makes them instantly recognizable.

This is a bad example because the author either does not know about wc -L, or does not want to teach it at this point in the text. If the latter is true, then a different problem should have been selected to illustrate whatever principle he/she is trying to teach.


Everyone,
In the real world wc -L is too simple & compact not to use. AFAIAC, any looping through file names counting their length is bogus; unless, of course, the teacher prefaces setting the problem w/ a statement to that effect & tells you to do it anyway for the practice.

And thanks for the great links.
 
Old 04-01-2008, 12:18 PM   #13
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by archtoad6 View Post
Everyone,
In the real world wc -L is too simple & compact not to use.
It all depends on what instructor wants to see/what the original task was.

If instructor teaches how to use scripts in general, then "ls" will do.
But if instructors teaches about more complicated things, looping constructs, conditional statements, name substitution, and the original homework was something like "implement ls using unix shell", then usage of ls won't be acceptible, since in this instructor wants to see knowledge about looping constructs, etc, not the knowledge about GNU text manipulation utilities. I personally suspect that won't like "ls" usage(I might be wrong here, though), since in several universites in my area homework of the kind "implement ls using unix shell" is quite common.

Quote:
Originally Posted by archtoad6
And thanks for the great links.
I recommend to read/learn whole unix (http://www.grymoire.com/Unix/index.html) section on grymoire, since there is a lot of very usefull information. It's a pity when people use linux for 6 months or more and don't know these things... (I'm not talking about someone in particular )
 
Old 04-01-2008, 12:50 PM   #14
mcdcyex
LQ Newbie
 
Registered: Mar 2008
Distribution: Fedora 5
Posts: 3

Original Poster
Rep: Reputation: 0
See thank you all! I at least understood 3/4 of what you all have said :-)

This was only the 3rd question we have tried to do in BASH scripting, I'll look at all those link you all have posted. Thanks again, Ill post what the instructor has said the correct script should be when I get it.
 
  


Reply

Tags
bash, links


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need some help with BASH scripts. BoB4ik Programming 10 12-20-2007 05:06 PM
bash scripts hoffmanyew Programming 3 08-11-2005 02:27 AM
Need Help With Bash Scripts the_woelf Linux - Software 4 06-30-2004 10:09 AM
Bash Scripts Skute Programming 7 03-12-2004 11:17 AM
Bash scripts? BajaNick Programming 3 07-05-2003 11:13 PM


All times are GMT -5. The time now is 11:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration