Shell script Programming---conditions to determine if

Source: Internet
Author: User

Shell Script Programming---judgment

1. Condition judgment

Conditional judgment in Bash is divided into integer judgments, character judgments, and file judgments.

1-1 condition-judged expression: ①[expression] ( Note that the word prefix and the ending require spaces )

②[[expression]] (note ibid.)

③test expression

1-2 integer Comparisons

①-eq test whether two numbers are equal, equal to true, and not as false

②-ne test whether the two numbers are unequal, and vice versa.

③-GT (greater than),-lt (less than),-ge (greater than or equal to),-le (less than or equal to)

Logic with:&& logic or: | |

2. Condition judgment, control structure

if judgment condition; then

Statement1

Statement2

Statement3

.........

FI single Branch if statement.

If statements for dual branches

if judgment condition; then

Statement1

Else

Statement2

Fi

Command execution status return value: 0 is true, all other values are false.

================== The following are all examples of practice ===============

Logical Judgment 1,

1. If the user does not exist, add the user

ID USERNAME &>/dev/null | | Useradd USERNAME

(the variable content is typically defined in the script, the execution of the script ends and the variable is revoked)

Logical Judgment 2,

1, if the user exists, it shows that the user already exists, or add this user.

ID newuser && echo "NewUser exists" | | Useradd NewUser

Logical Judgment 3,

1, if the user does not exist, you need to add users, otherwise it shows that the user already exists

ID newuser &>/dev/null && echo "NewUser exist" | | Useradd NewUser

Logical Judgment 4,

If the user does not exist, add the password back, otherwise, the display user already exists

ID newuser2 && echo "newuser2 exists" | | Useradd newuser2 && echo "Newuser2" | passwd--stdin Newuser2

Condition Judgment Exercises:

Add three users, User1-user3. You need to determine in advance whether the user exists and how many users have been added. Finally shows the number of users on the current system

Exercises Two:

Write a script, use a variable to save the user name, determine whether it exists first, and if its UID is 0, say it is an administrator, if not 0 say they are ordinary users.

650) this.width=650; "Style=" border-right-0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px "title=" image "border=" 0 "alt=" image "src=" http://img1.51cto.com/attachment/201411/9/6249823 _14155247635ijw.png "width=" 530 "height=" 265 "/>

Write a simple script that accomplishes the following tasks,

Use a variable to save the user name, delete the user in the variable, and its home directory to display the "delete succeeded" message.

650) this.width=650; "Style=" border-right-0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px "title=" image "border=" 0 "alt=" image "src=" http://img1.51cto.com/attachment/201411/9/6249823 _1415524763s1yt.png "width=" 523 "height="/>

Exercises Three:

Determine if there is a user default shell for bash on the current system, and if so, how many such users are displayed, otherwise, no.

650) this.width=650; "Style=" border-right-0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px "title=" image "border=" 0 "alt=" image "src=" http://img1.51cto.com/attachment/201411/9/6249823 _1415524764g4fx.png "width=" 563 "height=" 253 "/>

======== ( Note: This one is called retval=$?) this is written after the command statement, which is executed after the command execution state returns the result. ) =========

Exercises Four:

Determine if there is a user default shell for bash on the current system, and if so, display one of the user names, otherwise, no.

650) this.width=650; "Style=" border-right-0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px "title=" image "border=" 0 "alt=" image "src=" http://img1.51cto.com/attachment/201411/9/6249823 _1415524764c8yc.png "width=" 567 "height=" 238 "/>

Exercise four: Write a script, given a file, such as/etc/inittab, to determine whether there are blank lines in the file, if any, display blank lines, otherwise, display no blank lines.

650) this.width=650; "Style=" border-right-0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px "title=" image "border=" 0 "alt=" image "src=" http://img1.51cto.com/attachment/201411/9/6249823 _1415524765yn51.png "width=" 578 "height=" 228 "/>

Practice Four improvements: Write a script, let the user randomly enter a file, determine whether the file is valid, if it is valid, then display its blank line, otherwise, display no blank line.

650) this.width=650; "Style=" border-right-0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px "title=" image "border=" 0 "alt=" image "src=" http://img1.51cto.com/attachment/201411/9/6249823 _1415524766c5rm.png "width=" 582 "height=" 301 "/>

Exercise five: To give a user, to determine whether its UID and GID is consistent, if consistent, show the user as "good guy"

Otherwise, show as bad guy

650) this.width=650; "Style=" border-right-0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px "title=" image "border=" 0 "alt=" image "src=" http://img1.51cto.com/attachment/201411/9/6249823 _1415524767tlx6.png "width=" 583 "height=" 259 "/>

Exercise SIX: Determine if the total entry of the history command in the command history is greater than or equal to 1000, and if it is greater than or equal to 1000, "need clean up" is displayed, otherwise "ok! ”

650) this.width=650; "Style=" border-bottom:0px; border-left:0px; border-top:0px; border-right:0px "title=" image "border=" 0 "alt=" image "src=" http://img1.51cto.com/attachment/201411/9/6249823_ 1415524767y9ch.png "588" height= "174"/> or 650) this.width=650; "Style=" border-bottom:0px; border-left:0px; border-top:0px; border-right:0px "title=" image "border=" 0 "alt=" image "src=" http://img1.51cto.com/attachment/201411/9/6249823_ 1415524767gzuf.png "" 587 "height=" 185 "/>

Shell script Programming---conditions to determine if

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.