Solved

Bash script

Posted on 2011-03-11
7
494 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…

830 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