Solved

Bash script

Posted on 2011-03-11
7
477 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now