Solved

Bash script

Posted on 2011-03-11
7
487 Views
Last Modified: 2012-05-11
Good day Group

I am just a beginner with bash scripting and I just need some direction in creating a script. So basically, I need to create a script where if
I execute with the script like this :

./script bob - it prints out Mary

and

./script Mary - it prints out bob

and if neither command is parsed to it, it returns to stand err

Any suggestions would be highly appreciated.
0
Comment
Question by:tuxracer
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 100 total points
ID: 35108796
try

case $1 in
bob)
      echo Mary
      ;;
Mary)
      echo bob
      ;;
*)
      echo wrong name
esac
0
 
LVL 3

Expert Comment

by:egarciat
ID: 35110734
as a complement to omarfarid's post, I would change the following:

*)
      echo wrong name

to:

*)
      echo "wrong name" >&2

That will write "wrong name" to standard error..

0
 
LVL 8

Assisted Solution

by:point_pleasant
point_pleasant earned 75 total points
ID: 35110986
Alway the if then else option
stderr redirected to stdout

if [ $1 == "bob" ]; then
        echo "Mary"
elif [ $1 == "Mary" ]; then
        echo "bob"
else
        echo "Usage script [bob|Mary]" 2>&1
fi
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 3

Assisted Solution

by:egarciat
egarciat earned 75 total points
ID: 35111089
else
        echo "Usage script [bob|Mary]" 2>&1
fi

This will redirect stderr to stdout, it will not write to stderr.

I understand that tuxracer wants errors on stderr.
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35111307
unless other wise provided for 2>&1 and >&2 are exactly the same.  typicall stderr is redirected to a file for debugging.  stderr defaults to stdout unless outerwise directed.  since tuxracer did not specify a file both 2>&1 and >&2 are exactly the same.
0
 

Author Comment

by:tuxracer
ID: 35113388
Hi Guys

Thank you all for the great responses. Its what I was looking for for.

Much appreciated!!!! Collaboration - rocks!!!

Tuxracer
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 35114206
Welcome
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question