Shell script Programming selection execution if statement

Source: Internet
Author: User

Single-branch if statement:

if test condition

Then

Code Branch

Fi


Two-branch if statement:

if test condition; Then

Branch that is executed when the condition is true

Else

Branch that is executed when the condition is false

Fi


Example: Pass a user name to the script through a parameter, add it when this user is not saved;

#!/bin/bash#if [$#-lt 1];    Then echo "At least one username." Exit 2fiif grep "^$1\>"/etc/passwd &>/dev/null; Then echo "User $ exists." Else Useradd echo $ | passwd--stdin $ &>/dev/null echo "ADD user $ finished." Fi


Exercise 1: Given two numbers by command-line arguments, output a larger number;

#!/bin/bash#if [$#-lt 2];    Then echo "the integers." Exit 2fiif [$1-ge]; Then echo "Max Number: $." else echo "Max number: $." Fiecho "Max number: $max."



This article is from the "Wang Liming" blog, make sure to keep this source http://afterdawn.blog.51cto.com/7503144/1915972

Shell script Programming selection execution if statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.