Bash Script Programming:
If statement, Bash-n, Bash-x
CONDITION:
Bash command:
The execution status result of the command;
Success: True
Failure: flase
The meaning of success or failure: Depending on the command used;
Single branch:
if CONDITION; Then
If-true
Fi
Dual Branch:
if CONDITION; Then
If-true
Else
If-false
Fi
Multi-branch:
if CONDITION1; Then
If-true
Elif CONDITION2; Then
If-ture
Elif CONDITION3; Then
If-ture
...
Esle
All-false
Fi
By the condition of judgment, the first encounter as a "true" condition, the execution of its branches, and then end;
Example: User type file path, script to determine file type;
#!/bin/bash
#
Read-p "Enter a file path:" FileName
If [-Z "$filename"]; Then
echo "Usage:enter a file path."
Exit 2
Fi
if [!-e $filename]; Then
echo "No such file."
Exit 3
Fi
If [-f $filename]; Then
echo "A common file."
elif [-D $filename]; Then
echo "A directory."
elif [-l $filename]; Then
echo "A symbolic file."
Else
echo "Other type."
Fi
Note: If statements can be nested;
Loops: For, while, until
Loop body: The code to execute, possibly N times;
Entry conditions:
Exit Criteria:
For loop:
For variable name in list; Do
Loop body
Done
Implementation mechanism:
Assigns the element in the list to the "variable name" in turn; The loop body is executed once each assignment; Until the elements in the list are exhausted, the loop ends;
Example: Add 10 users, user1-user10; password with user name;
#!/bin/bash
#
if [! $UID-eq 0]; Then
echo "Only root."
Exit 1
Fi
For i in {1..10}; Do
If ID user$i &>/dev/null; Then
echo "user$i exists."
Else
Useradd user$i
If [$?-eq 0]; Then
echo "User$i" | passwd--stdin user$i &>/dev/null
echo "Add User$i finished."
Fi
Fi
Done
List Generation Method:
(1) give the list directly;
(2) List of integers:
(a) {start: End
(b) $ (SEQ [start [step]] end)
(3) command to return the list;
$ (COMMAND)
(4) Glob
(b) Variable references;
[email protected], $*
Example: Determine the type of all files under a path
#!/bin/bash
#
For file in $ (Ls/var); Do
If [-f/var/$file]; Then
echo "Common file."
elif [-l/var/$file]; Then
echo "Symbolic file."
elif [-d/var/$file]; Then
echo "Directory."
Else
echo "Other type."
Fi
Done
Example:
#!/bin/bash
#
Declare-i estab=0
Declare-i listen=0
Declare-i other=0
For state in $ (Netstat-tan | grep "^tcp\>" | awk ' {print $NF} '); Do
If ["$state" = = ' established ']; Then
Let estab++
elif ["$state" = = ' LISTEN ']; Then
Let listen++
Else
Let other++
Fi
Done
echo "Established: $estab"
echo "LISTEN: $listen"
echo "Unkown: $other"
In the practice 1:/ETC/RC.D/RC3.D directory, there are several files starting with K and beginning with S, respectively;
Read each file separately, the output of the file starting with K is the file plus stop, and the file output with s begins with the filename plus start;
"K34filename Stop"
"S66filename Start"
Exercise 2: Write a script that uses the ping command to detect the online status of the host between the 172.16.250.1-254;
This article is from the "Liang blog" blog, make sure to keep this source http://7038006.blog.51cto.com/7028006/1829381
Bash script programming for