LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-25-2012, 01:38 AM   #1
anas_lko
LQ Newbie
 
Registered: Jan 2012
Posts: 19

Rep: Reputation: Disabled
Exclamation 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.
Old 01-25-2012, 01:44 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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
 
2 members found this post helpful.
Old 01-25-2012, 07:38 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
2 members found this post helpful.
Old 01-26-2012, 09:08 AM   #4
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
  • 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.
 
1 members found this post helpful.
Old 01-26-2012, 12:09 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,705

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by anas_lko View Post
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.
 
1 members found this post helpful.
Old 01-26-2012, 12:15 PM   #6
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

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

Kind regards
 
Old 01-27-2012, 02:49 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.

Last edited by colucix; 01-27-2012 at 02:55 AM.
 
Old 01-31-2012, 10:19 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
3 members found this post helpful.
Old 01-31-2012, 02:01 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nice, David. Something worth to learn!
 
Old 01-31-2012, 11:36 PM   #10
anas_lko
LQ Newbie
 
Registered: Jan 2012
Posts: 19

Original Poster
Rep: Reputation: Disabled
Talking Thnx

Thanks david, its really very helpful...
 
1 members found this post helpful.
Old 02-01-2012, 07:49 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
2 members found this post helpful.
Old 02-01-2012, 09:18 AM   #12
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
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

Last edited by cbtshare; 02-01-2012 at 09:31 AM.
 
1 members found this post helpful.
Old 02-01-2012, 10:59 AM   #13
EDDY1
LQ Addict
 
Registered: Mar 2010
Location: Oakland,Ca
Distribution: wins7, Debian wheezy
Posts: 6,841

Rep: Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649
@ 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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Advice for a fairly simple(?) script frankie_DJ Programming 8 10-04-2006 11:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration