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.
|
Hello and welcome to LinuxQuestions,
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. Kind regards, Eric |
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. Here are a few useful bash scripting references: http://mywiki.wooledge.org/BashGuide http://www.linuxcommand.org/index.php http://mywiki.wooledge.org/BashFAQ http://mywiki.wooledge.org/BashPitfalls http://www.gnu.org/software/bash/manual/bashref.html http://wiki.bash-hackers.org/start http://ss64.com/bash/ I particularly recommend the first one. |
Here are soem more vague clues that may lead you in interesting directions:
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. |
Quote:
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. |
One reference to "urgent" and "homework" is more then enough, no?
Let's move on. Kind regards |
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.
Code:
#!/bin/bash |
Nice, David. Something worth to learn! :)
|
Thnx
Thanks david, its really very helpful... :)
|
Oh, so now you're back?
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.
|
All times are GMT -5. The time now is 10:31 AM. |