LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   question in shell script (https://www.linuxquestions.org/questions/programming-9/question-in-shell-script-899967/)

abood1190 08-28-2011 10:44 PM

question in shell script
 
Hell guys,
I need a little help to solve this question

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.

EricTRA 08-29-2011 12:16 AM

Hello and Welcome to LinuxQuestions,

Nobody here will do your homework for you, that is, if this is homework. It sure looks like that to me. Why not? Because you'll learn a lot more doing it yourself. Of course when you encounter errors are get stuck writing your script, that's when you turn to us for guidance. Basically it all comes down to: Show us what you've got and where it's failing, then you'll get dedicated answers. A good starting point are these guides:
Bash Guide for Beginners
Advanced Bash Scripting Guide

Looking forward to your participation in the forums. Have fun with Linux.

Kind regards,

Eric

abood1190 08-29-2011 12:56 AM

I tried to solve

this is my code ( its wrong ! )


Quote:

#!/bin/sh

echo " Command line argument is :" $#

if [ -d $1 ]
then
echo " The number of files in the directory is " ls -l | wc -l

if [ -f $1 ]
then
echo " The execute permission for the file owner is" ls $1

else

echo " It is neither a file or directory "

fi

if [ -z $1 ]

echo " There is no command line argument ! "

fi

rodrifra 08-29-2011 01:59 AM

Hi there.

You missed the else for the first if, besides, you have missed one "fi".

For the number of files of the directory you are counting files AND directories, I don't know if that is the required answer, if you only want files you will have to check the first caracter in the permisions, if it is a "d" it is a directory, not a regular file.

In case of a file your are getting the file ls, no permissions are shown in that command, you should use ls -l to get an output with permissions, besides, question is to retrieve execute permission for the owner, so you will have to get the exact part of the answer retrieved with ls -l.

Use man to know a command's usage and its parameters:

man ls

The commands you might be interested in checking with man are:

cut, grep, awk(swiss knife, you won't need a number of other commands if you know how to use this one)

colucix 08-29-2011 02:36 AM

Apart from the syntax errors in the if/then/else construct, What you missed is command substitution to retrieve the output of commands and print them out (or assign them to a variable). Check it here and here.

XavierP 08-29-2011 06:30 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

grail 08-29-2011 07:17 AM

Personally I would go back to your instructor and ask what are the suggested mechanisms to be used and how strict is the testing meant to be.

The reason I say the above is you are using the standard test facility, ie [ is synonymous with test. Both utilise the -d and -f options but should you be testing a symbolic link,
which strictly speaking is neither a file nor a directory, your tests will return true, ie if symbolic link is to a directory and you use -d it will return true as it is testing
what the link is pointing to and not the link itself.

Your order leaves a bit to be desired as well seeing you are testing what is in $1 at the end, plus what if there are more than one arguments??

Lastly, I think you need to look up what $# does as your use does not look correct to me.

abood1190 08-29-2011 08:57 AM

rodrifra
colucix
XavierP
grail


Thank you guys for replying, i started to love it

i tried and im stuck with last two points.

this is my work :

Quote:

#!/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

grail 08-29-2011 09:21 AM

I will ask you some questions:

1. Where are your argument test(s)? You have no test now for the number of arguments so if user enters nothing or more than 1 your code will not show an error or exit.

2. You test for a directory but by using 'ls -l' you assume that the are no sub-directories. This should be checked against your requirements.

3. How does doing an 'ls -l' test whether or not a file has the execution bit set?

Lastly, if you use code tags instead of quote tags your formatting of the code will be maintained.

abood1190 08-29-2011 12:10 PM

Quote:

Originally Posted by grail (Post 4456139)
I will ask you some questions:

1. Where are your argument test(s)? You have no test now for the number of arguments so if user enters nothing or more than 1 your code will not show an error or exit.

2. You test for a directory but by using 'ls -l' you assume that the are no sub-directories. This should be checked against your requirements.

3. How does doing an 'ls -l' test whether or not a file has the execution bit set?

Lastly, if you use code tags instead of quote tags your formatting of the code will be maintained.

To be honest with you.
I typed my previous code according to my lecture notes !
I know nothing about ls -l and arguments !!
I need help gentlemen ,,,, due date for submission on Wednesday ^_^

TB0ne 08-29-2011 02:16 PM

Quote:

Originally Posted by abood1190 (Post 4456264)
To be honest with you.
I typed my previous code according to my lecture notes !
I know nothing about ls -l and arguments !!
I need help gentlemen ,,,, due date for submission on Wednesday ^_^

Ok..then you should be reading the man pages on the "ls" command, which will tell you what you're missing. And the point of an assignment is to make you think, learn, and be able to do the work. If you're just typing it in, and getting us to fix the problems without UNDERSTANDING HOW, you won't learn, and it'll make every assignment from now on, harder. You won't understand this step, so the next steps will be even MORE unclear.

chrism01 08-29-2011 08:48 PM

Read the links provided by EricTRA in post #2; read this http://rute.2038bug.com/index.html.gz; more accurately search those docs for relevant info/examples.
Also, check the exact usage of each cmd here http://linux.die.net/man/

I suggest you book mark all these links; you are going to need them.

grail 08-29-2011 09:13 PM

Couldn't agree more with TB0ne. There is no pass mark or degree waiting for any of us for passing YOUR assignment.

Also, if you read a little closer through the replies you will see that solution suggestions have been given, plus I am
sure things haven't changed too much since I studied that the teacher / lecturer is asking you to do something
that has not been covered.

abood1190 08-31-2011 06:25 PM

TB0ne
thats what was i trying to find out
but the problem is most of the things i dont understand it !
Thank you

chrism01
i couldnt find post 2 ,,,
Thank you

grail
Thank you for helping me

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

grail 08-31-2011 07:52 PM

Quote:

chrism01
i couldnt find post 2 ,,,
The post where you wrote this was 14 which is the number on the far top right corner of your post.
So look for the one with a 2 in this position :)
Quote:

the time for submission is up !
Well after it is marked and you get the results and solutions, come back and ask more questions.


All times are GMT -5. The time now is 08:00 PM.