ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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)
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.
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.
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.
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 ^_^
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.