Listing the files in a directory

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

A script that takes any number of directories as command line arguments and then lists the contents of each of these directories. The script should output each directory name prior to listing the files within that directory

  1. Relevant commands, code, scripts, algorithms:
No of dir = $#
count=1
If [ count -le $# ] then;
echo ${$count}
ls �ltr ${$count}/
count = `expr count + 1`
fi 
  1. The attempts at a solution (include all code and scripts):
No of dir = $#
count=1
If [ count -le $# ] then;
echo ${$count}
ls �ltr ${$count}/
count = `expr count + 1`
fi 
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

BITS pilani, Hyderabad, India, B.V. Prasad Babu, System Programming

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Thank you for filling out the template.

A few problems:

1) Variables can't have spaces in their names. No of dir = $# is wrong.

2) You shouldn't put spaces on either side of the equal sign. something = asdf is wrong. Do something=asdf

3) You got your if-statement nearly right, but not quite. Don't capitalize anything. Your ; is also in the wrong place.

If it helps, imagine it like this:

if [ something ]
then
...
fi

To combine any of that onto one line, you'd put ; where there's newlines, so you'd get if [ something ] ; then

4) You can't put a variable in a variable like this: ${$count} it just doesn't expand there. There's a better way, anyway.

The shift command will get rid of the first parameter. If your parameters were a b c d before, after you run shift they will be b c d . So you can loop until $# is zero, running shift at the bottom of your loop, using $1 every time instead of using $1, $2, $3...

I would also add and comment:

count=1
If [ count -le $# ] 

What is the if supposed to do here?
I see you are comparing a string (count) with a numeral since you are using -le How can you expect that to work?

if [ "$count" -le $# ]   # Here the shell know that the content "should" be an integer 
then                     # since you are using -le ...
    ...

Thnax. I wrote like this.. now it is working

if [ $# -eq 0 ]
then
  echo "Input needed"
  exit 0;
else
while (($#));
do
    echo "Dir Name ->" $1
    sleep 2
    ls -ltr $1
    shift
   echo "\n"
done
fi