I need advice to create a simple script (homework)
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I need advice to create a simple script (homework)
Create a script /root/foo.sh. when you execute this as /root/foo.sh redhat the output should come fedora. And when you execute script as /root/foo.sh fedora the output should come redhat. But when you execute this script without argument as /root/foo.sh output should come redhat|fedora.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Please don't use words like urgent in your title or post. It's only urgent to you and not to us who are all volunteers putting in our free time to help out others with their questions/problems in Linux. Aside from that, this definitely 'smells' like homework to me and you'll have a hard time finding someone here to do it for you. What will you learn when we provide you a ready made solution? As pointed out in the LQ Rules, you'll learn a lot more doing it yourself. Show us what you've got and where it's failing and we'll point you in the right direction. Keep in mind that your teacher (if this IS homework) might be a member here too. If you don't know where to start writing shell scripts, then have a look here: Bash Guide for Beginners Advanced Bash Scripting Guide
Looking forward to your participation in the forums. Have fun with Linux.
For the record, the listed requirements require only the most basic scripting knowledge. Hint: you need a single conditional statement, one that evaluates a single parameter for multiple possible values.
That and the links EricTRA gave above should be enough for you to figure it out.
This thread shouldn't be in 'server'; it really doesn't have anything to do with "the discussion of Linux Software used in a server related context"; newbie, or at a stretch, programming
Obvious homework: you know it says that we won't do your homework for you; well, we didn't; please carry on to tell us how you did with it
My personal rule is, unless you can see a reason for it in the explanation, if someone tells me that it is urgent that I do something, I wait 24 hours to see what will go wrong with my life, if I don't do it immediately. You might think that this is harsh, but there are a whole load of people who tell me that various things are urgent, and I can't do all of them "urgently".
Here are soem more vague clues that may lead you in interesting directions:
You want to know how your script has been invoked; this is easy enough to do, and once you have worked that out, it should become clearer how you could make progress
(Having worked that out) there are all sorts of ways that you could structure your script; to an extent, it doesn't matter which of them you adopt; some might be 'cleaner' or 'more elegant' than others, but I can think of a number that would function; really, there should be clues in the material for the course that you are doing
In the real world, you would probably also think about the case that someone invoked the script with something other than the three specified conditions; do you get a sensible error message, or do you get an incorrect message out (bad)
Sorry if you feel that this has been unhelpful, but you stand a better chance of getting what you want next time if you know these things.
Create a script /root/foo.sh. when you execute this as /root/foo.sh redhat the output should come fedora. And when you execute script as /root/foo.sh fedora the output should come redhat. But when you execute this script without argument as /root/foo.sh output should come redhat|fedora.
This may be urgent for you, but it isn't urgent for ANYONE here.
Also, this very much sounds like homework. Reference the links you were given to basic shell scripting. If you have trouble after that, use your textbooks, and if you're still stuck...ask your teacher for help.
Agree. Said enough. Please, stay on topic now. Title changed.
@anas_lko: as previously mentioned, show us what you've tried and where you're stuck. Indeed we cannot do homework for you, but only smooth some edges in your learning process.
Moved: This thread is more suitable in Linux - Newbie and has been moved accordingly to help your thread/question get the exposure it deserves.
Well, the OP has been awol for a week, so I guess it's safe to post the little 3-line version I came up with that satisfies the posted requirements, using only simple variable substitution.
Actually, what I posted likely isn't as helpful as you think. For my own amusement and edification I deliberately worked out an unconventional solution to the problem; one that would certainly be recognized by an instructor as something a beginning student would not produce.
It's also a very restricted solution that only fulfills the exact stated requirements. It doesn't take into account any variation in input and is certainly not generally applicable to other situations.
As I pointed out in my first post, the point of the assignment is undoubtedly to familiarize you with the basic conditional expressions, which I purposely avoided using. You still need to do your own homework.
yea , spent around 6 hrs reading and learning parameters and substitutions lolll and finally mastered it.Good find.any variations can be handled with if statements though lol, I actually thought about that while studying the code.If the first positional parameter is "re" it actually prints fedordahat
Trickiest part was the parameter substitutions ,had to figure out what was really happening when $a/$1 are the same , it was cool figuring out.But the documentation on the + and - in ${var:} was hardest to find.In the end knowledge gain and yet another sleepless night, of to school lol
@ David the H. I looked at the first link you posted in post #3 & it's 1 of the easiest to understand guide on Bash that I've tried to read. I haven't read the whole book yet but I will. I will definately checkout the other links & most likely bookmark them.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.