Shell script learning: Step-by-Step Basic awk statements, until loops, and test statements

Source: Internet
Author: User

Awk origin: awk statements are a set of languages developed by three researchers at Bell Labs in 1970s. awk names are the names and letters of the three researchers. However, awk was initially not intended for users, but for individual users. However, awk is still a powerful language.

In 1990s, GNU developed a fully open-source version based on awk, providing more powerful functions and even using FTP for network applications.

An awk statement is a very powerful statement. It is very powerful and can even be used as a programming language. In the future, awk statements are often used to process characters. In shell scripts, awk can be said to be one of the core. Because we are new to awk statements, we should first write down the learning experience of this preliminary awk statement.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Awk format:

Awk [Option] 'pattern{ action }'
Pattern:/Regexp // Regular Expression
Exp // expression
Begin
End
Action: Print // slice the text, $1,..., $ n
FS: // What is the delimiter used to read the file? The default Delimiter is a space character.
RS: // line break,/n
OFS // output segment Separator
ORS // output line delimiter
NF // how many disconnections are cut in a row
NR // The number of rows currently in the first row
Awk-F: // you can specify the separator ":" During input, or specify other

Here are two examples to show the power of the awk statement:
1. display the names, numbers, and vsz values of all processes whose vsz segments are greater than 4000 on the current system;

ps aux |awk '$5>4000 {print $2 "\t" $5 "\t" $11}' |grep -v "PID"   

# Send the PS process information to the awk command for processing. The space is used as the Separator by default. If the value of the 5th fields is greater than 4000, 2nd, and 5 fields are displayed, then, it is handed over to grep to print the items without "PID.

Have you seen it? The pattern item of this awk statement is an expression: $5> 4000. In awk, pttern is a regular expression. The expression uses this condition well, you can quickly match the desired text without using grep or # sed to capture the command. The $2, $5 at the end of the page indicates the 2, 5 fields, and \ t indicates the tab.

 

2. Show each user name and corresponding uid number and GID number for the linuxer1-linuxer20

# Create a user linuxer1-linuxer20 before doing this exercise

 grep "linuxer$I" $FILE | awk 'BEGIN{FS=":"}{print $1",UID:" $3",GID:"$4}'

# Begin usage: begin {} indicates that the content in begin {} is prepared before the subsequent command is executed, that is, the content in begin is executed first, and then the action is executed, the end is the opposite.

Zookeeper ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Until loop: The until loop and while loop have the following differences:
Until condition; do
...
...
Done
# When the condition is false, perform a loop until it is true. In this way, you can specify a condition. If this condition is met, the loop continues until the condition is not met.

 

Write a script:
Check whether the RedHat user has logged on to the system. If yes, notify the current script executor "RedHat is logged on .";
Otherwise, test again after five seconds of sleep until the user logs on to the system and exits;
Requirement: Use the until Loop

# Sleep 5 sleep for 5 seconds

# Wall "" notify everyone ""

#! /Bin/bash # Who | grep "RedHat" &>/dev/null // capture the current logon user. Is there redhatwho =who? // Whether the execution of the previous statement is successful. If successful, the value of who is 0 until [$ who-EQ 0]; do // unless the value of who is a variable of 0, otherwise, the loop body sleep 5 who | grep "RedHat" &>/dev/null who =$? // Test again to prevent endless loops done wall "RedHat is logged on"

 

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Test statement

Script condition judgment
Test Express

The test statement is mainly used for program execution flow or other statements that can be attached with conditions to become their condition, which enriches the flexibility of the script.

Its format:
[Expression] // a space is required, and the copy syntax is incorrect.
[[Expression]

Conditional test:

Integer Test
-Lt, [$ A-lt $ B] // whether a is less than B
-Le is less than or equal
-GT greater
-Ge is greater than or equal
-NQ equals
-Ne is not equal

String Testing
==// There must be a space around the equal sign 'W' string with single quotation marks, and the variable with "" = There are spaces on both sides
! =
>
<
-Z // determines whether a string is null or not. If it is not empty, it is false. [-Z $ A]
-N // opposite to-z

File Test
-E file // determine whether a file exists
-F // whether it is a common file
-D // whether it is a directory
-H/-l // determine whether it is a symbolic link file or soft connection
-R // whether to read the current user
-W can be written to the current user
-X indicates whether execution is executable for the current user.
-S // exists and the size is not 0
-O // determine whether the initiator of the script is the owner of the file.
-G // is the group valid?

 

For example, in the above example, until [$ who-EQ 0];-EQ is used to test whether it is equal to 0.

 

 

 

 

 

 

 

 

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.