Shell programming (4) --- condition judgment based on if

Source: Internet
Author: User

Single branch if statement

If condition; then statement1 statement2fi


Dual-branch if statement

If judgment condition; then statement1 statement2 ...... Else statement3 statement4 ...... Fi


Multi-branch if statement

If condition 1; then statement1 statement2 ...... Elif judgment condition 2; then statement3 statement4 ...... Elif judgment Condition 3; then statement5 statement6 ...... Else statement7 statement8 ...... Fi


Example 1:

[[email protected] Scripts]# cat ./root.sh#!/bin/bashread -p "Please input an Username in /etc/passwd file: " Local_USERLocal_ID=$(id -u $Local_USER)if [ "$Local_ID" -eq 0 ];then        echo "The User $Local_USER is admin"else        echo "The User $Local_USER is common user."fi[[email protected] Scripts]# ./root.sh Please input an Username in /etc/passwd file: frameThe User frame is common user.[[email protected] Scripts]# ./root.sh Please input an Username in /etc/passwd file: rootThe User root is admin[[email protected] Scripts]#



This article is from the "Hezhang" blog, please be sure to keep this source http://hezhang.blog.51cto.com/1347601/1435340

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.