question in shell script

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:

Write a Bourne shell script which:
� Has one command line argument.
� If the command line argument is a directory then the script should output the number of files in the directory.
� If the command line argument is an ordinary file then the script should output whether or not the file has execute permission for the file owner.
� If the command line argument is neither a file or directory then the script should output an appropriate error message.
� If no command line argument is supplied then the script should output an appropriate error message.

  1. Relevant commands, code, scripts, algorithms:

  2. The attempts at a solution (include all code and scripts):

  3. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Curtin university, sedney, Australia, 101.

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).

I notice that the section with your attempts is alarmingly blank. Did you try anything yet? Or are you hoping that someone here will do your work for you?

Aside from that, as far as I know it's spelled Sydney. And what 101 course are you taking?

sorry you are right ! its Sydney

i tried and im stuck with last two points.

this is my work :

#!/bin/sh

echo " Command line argument is :" $1

if [ -d $1 ]
then
    echo " The number of files in the directory is ` ls -l | wc -l` "
else
    echo " It is not a dirctory "
fi

if [ -f $1 ]
then
    echo " The execute permission for the file owner is ` ls -l  $1`"
else
    echo " There is no execute permission "
fi

First of all, I don't think your script behaves as required, at least I don't see a requirement to tell the user that the argument isn't a directory, or doesn't have the executable bit set.

Try to get the logical flow down for the script first, and jot down some pseudo-code, eg

if argument given
  if directory
    count files
    exit
  else if file
    if executable
      say so
    else
      say not executable
  else
    argument is something else
else
  say error

For the tests, man test (POSIX) (shorthand: [ ... ] ) has some very valuable operators to check for files, directories, executables, and if a string has zero length.

BTW, with your method of counting you'll have a one-off error. Compare the output of ls -l | wc -l and ls | wc -l . I'll leave it to you to find the source of that error :wink:

I notice that you posted the identical question on LQ.org. Here is a reply from there.

Thank you for helping me

the time is up for submission !
i will try to revise again. i hope i can share my knowledge with you soon ...

yes, i was trying to gather info as much as i can
Thank you ...