LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I need advice to create a simple script (homework) (https://www.linuxquestions.org/questions/linux-newbie-8/i-need-advice-to-create-a-simple-script-homework-925585/)

anas_lko 01-25-2012 01:38 AM

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.

EricTRA 01-25-2012 01:44 AM

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

David the H. 01-25-2012 07:38 AM

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.

salasi 01-26-2012 09:08 AM

  • 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.

TB0ne 01-26-2012 12:09 PM

Quote:

Originally Posted by anas_lko (Post 4583807)
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.

repo 01-26-2012 12:15 PM

One reference to "urgent" and "homework" is more then enough, no?
Let's move on.

Kind regards

colucix 01-27-2012 02:49 AM

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.

David the H. 01-31-2012 10:19 AM

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

a=${1:-redhat|fedora}
b=${1:+redhat}
echo "${a/$1}${b/$1/fedora}"

Simple, yes. But it took me a surprisingly long time to get the concept working right, then to boil it down to its basic essence. :cool:

colucix 01-31-2012 02:01 PM

Nice, David. Something worth to learn! :)

anas_lko 01-31-2012 11:36 PM

Thnx
 
Thanks david, its really very helpful... :)

David the H. 02-01-2012 07:49 AM

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.

cbtshare 02-01-2012 09:18 AM

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 :)

EDDY1 02-01-2012 10:59 AM

@ 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.