Solved

Bash script

Posted on 2011-03-11
7
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

628 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