Here are some pointers. You are of course free to ignore any you do not like
1. You supply error messages, which would generally lead me to believe others may use your code. Why then is there no instruction on how to call your code correctly? You advise the mistake but not how
to correct it??
2. You set the variable longfile, however your comment advises it is to an empty string. This is not technically correct as it now contains a space, ie. not empty. Curiously, should we bypass all
the previously caught errors you will echo this single space to the screen. Maybe you should be testing if longfile is still a single space or count is greater than zero before delivering the result.
3. Your first error message says "Not Enough Arguments", so how many is enough?? (see point 1)
4. Inside the for loop your first test is to whether or not a directory was passed and on error a message goes to stderr, but the you exit the script. So if any are not directories then none of the other
items in the list are to be processed? (again point 1 & 3 may mean only one directory is to be passed)
Also, how does this user know which one they typed in wrong??
5. I do not understand the point of the next test, ie -a "$i"? It has no else nor any associated error, so I am wondering if it is required at all??
6. See point 4 about next test advising the directory is not accessible but also no advice on which one
7. Do not parse ls for reason stated
here. As an example, if I take your snippet below and use it on my working directory I am returned a blank line for all files as they are in the
the 8th position and not the 9th which is returned by your cut command:
Code:
ls -l | grep ^- |tr -s "[:space:]"|cut -d" " -f9
Also, if the files have spaces in them you will get some very weird results.
8. You set the value for the variable 's' both before the inner for loop and immediately inside. This would seem to serve no purpose
9. Your comment on setting variable 's' inside the loop says, #SETTING MAX CHARACTER FILE INT TO S, I am not sure I understand? Are you saying that the last file in every directory will always
have the maximum number of characters? (again another reason not to parse ls)
10. After all the different points where you set the 's' variable, it is never used???
11. You test the length against your count variable, but you quote them as if they are strings and then use '-gt' for testing numbers. As advised previously, if you use round brackets for arithmetic,
it is clearer on what you are testing:
Code:
if (( ${#bn} > count ))
12. You set the count and longfile values but at the end you advise that you are displaying the count and the file name, however, the count is of only the file name whilst you are setting the
longfile value to the entire path where the file was found, ie. this will not be equal to the count shown
Well I hope some of the above is useful to you