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. |
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 |
I tried to solve
this is my code ( its wrong ! ) Quote:
|
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) |
|
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
|
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. |
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:
|
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. |
Quote:
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 ^_^ |
Quote:
|
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. |
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. |
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 ... |
Quote:
So look for the one with a 2 in this position :) Quote:
|
All times are GMT -5. The time now is 08:00 PM. |