Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bash script

Posted on 2011-03-11
7
Medium Priority
?
514 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 400 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 300 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 3

Assisted Solution

by:egarciat
egarciat earned 300 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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 …
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…
Suggested Courses

715 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