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 |
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 |
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." > } |
$ 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 |
|
Shell Scripting Help!!!!
Quote:
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 |
Quote:
|
Quote:
|
Quote:
#!/bin/sh ls ~/x > files.txt wc -L files.txt run this script it will give you maximum length of all the filenames |
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 Code:
ls -1 | wc -L |
Quote:
Code:
#!/bin/sh http://www.grymoire.com/Unix/Sh.html |
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 () { 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. |
Quote:
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:
|
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. |
All times are GMT -5. The time now is 01:56 AM. |