[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

linux beginner script

Posted on 2012-09-13
2
Medium Priority
?
602 Views
Last Modified: 2012-09-13
Hi,
Below script is not a complete one.
I just want to know what is the purpose/function of  [$? == 0 ];  this.


#!/bin/bash
cat  $1 | while read  un  gn  pw fn
do
cat  /etc/group  |  egrep $gn
if [ $? == 0 ]; then
echo “Group $gn  already exists”
else


Thank you for your help.
0
Comment
Question by:bt1942
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1200 total points
ID: 38394060
This just tests the returncode ($?) of egrep for being zero ("0")

If it equals zero then the search string was found in /etc/group, and you can happily issue your "exists" message,

A nonzero returncode ("1" in this case) would mean that /etc/group does not contain the string you searched for.
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 800 total points
ID: 38395912
In general, $? contains the return code of the previous application or program. If egrep was successful in finding what you were looking for, it's return code is 0, hence the test == 0
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

831 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