Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

3. Input and output redirection, bash arithmetic, regular expression learning notes

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Input and output redirection and pipelin

INPUT: standard input stdin0

OUPUT: standard output stdout1

Standard error stderr2

Ipaw O redirection

Input redirection:: append output

Set-C: prohibit redirecting to existing files using overrides

Set + C: turn off the above features

> |: force the use of override redirection under the-C feature

/ dev/null: bit bucket, bit bucket

Error redirection: 2 >, 2 > >

2 >: overlay

2 > >: append

Reset both standard output and error output:

COMMAND > / path/to/outfile 2 > / path/to/errfile

COMMAND & > / path/to/somefile

COMMAND > / path/to/somefile 2 > & 1

Pipe:

COMMAND1 | COMMAND2 | COMMAND3 |...

Arithmetic Operation in bash

Declare

-I: integer variable

-x: environment variable, similar to export

Let varName= arithmetic expression

VarName=$ [arithmetic expression]

VarName=$ ((arithmetic expression))

VarName= `expr $num1 + $num2`

If there is a decimal in the calculation result, it will be rounded:

Operator: +, -, *, /,%

+ =,-=, * =, /,% =

Exercise: calculate the sum of all positive integers within 100

#! / bin/bash

#

Declare-I sum=0

For i in {1... 100}; do

Let sum=$sum+$i

Done

Echo "The sum is: $sum."

Knowledge points: single-step execution of bash:

Bash-x / path/to/script

Exercise: calculate the sum of all even numbers and odd numbers within 100 respectively

#! / bin/bash

Declare-I oddSum=0,evenSum=0

For i in `seq 1 2 100`; do

OddSum=$ [$oddSum+$i]

Done

For j in `seq 2 2 100`; do

EvenSum=$ [$evenSum+$j]

Done

Echo "The Even Sum is: $evenSum, the odd sum is: $oddSum"

Exercise: calculate the sum of the ID of all users in the current system

#! / bin/bash

Declare-I uidSum=0

For i in `cut-d:-f3 / etc/ passwd`; do

UidSum=$ [$uidSum+$i]

Done

Echo "The UIDSum is: $uidSum."

Exercise: calculate the sum of the characters in / etc/rc.d/rc.sysinit, / etc/init.d/functions, and / etc/issue files

#! / bin/bash

#

Declare-I bytesCount=0

For file in / etc/rc.d/rc.sysinit / etc/init.d/functions / etc/issue; do

Let bytesCount=$bytesCount+ `wc-c $file | cut-D''- f1`

Done

Echo $bytesCount

Exercise: create a new user tmpuser1-tmpuser10 and calculate the sum of their id

#! / bin/bash

#

Declare-I uidSum=0

For i in {1... 10}; do

Useradd tmpuser$i

Let uidSum=$uidSum+ `id-u tmpuser$ i`

Done

Knowledge points: location parameters

Location parameters:

/ tmp/test.sh 3 89

$0: script itself

$1: the first parameter of the script

$2

...

Special variables:

$#: number of location parameters

$*, $@: reference all location parameters

Knowledge points: interactive scripts

Read

-t specifies the input timeout

-p can enter a prompt

Knowledge points: give default values to variables

VarName=$ {varName:-value}

Returns the value of varName if varName is not empty; otherwise, returns value

If varName is not empty, its value remains the same; otherwise, varName uses value as its value

Exercise: through the keyboard given a directory path, the default is /, to determine the type of file content under the directory

#! / bin/bash

Read-t 5-p "Please input a dirPath:" dirPath

DirPath=$ {dirPath:- "/"}

Echo "The dirPath what you input is: $dirPath"

For filename in "$dirPath/*"

Do

File $filename

Done

Grep, egrep and fgrep of text processing tools:

Grep: (global search regular _ expression (RE) and print out the line

The text search tool searches the target file line by line according to the user-specified text pattern, displaying the lines that can be matched by the pattern

Format: grep [options] 'PATTERN' file,...

Common options for grep:

-v in reverse, showing lines that cannot be matched by the pattern

-o displays only the string matched by the pattern, not the entire line

-I is case-insensitive, ignore-case

-E supports extended regular expressions

-A # shows the # lines after the match line

-B # displays the # line in front of the matching line

-C # displays # lines before and after matching lines

-- the color=auto color displays the matching characters

Regular expression: a pattern written by a class of characters (pattern)

Metacharacter: does not represent the meaning of the character itself and is used to describe additional functionality

Metacharacter of basic regular expression: grep-E

Character matching:

. Match any single character

[] matches any single character within the specified range

[^] matches any single character outside the specified range

[0-9], [[: digit:]] match digits 0-9

[a murz], [[: lower:]] matches the lowercase letters amurz

[Amurz], [[: upper:]] matches the capital letters Amurz

[[: alpha:]] matches all letters a-zA-Z

[[: alnum:]] matches all alphanumeric 0-9a-zA-Z

[[: space:]] matches all white space characters

[[: punct:]] matches all punctuation marks

Number of times matching: used to specify the number of times to match the characters before it

* match any time, which can be 0 or more times

. * match any character of any length

\? Match 0 times or 1 time

\ {m\} match m times

\ {mdirection n\} match at least m times and n times at most

\ {m,\} match at least m times

\ {0cr n\} match n times at most

Position anchoring: used to specify where characters appear

^ Anchor the beginning of the line (^ char)

Anchor end of line (char$)

^ $blank line

\ char\ b)

Grouping:

\ (\)

Reference:

\ 1 backward reference, referring to the content matched by the first left parenthesis and the corresponding pattern in the right parenthesis

\ 2

...

He like his lover.

She love her liker.

He love his lover.

She like her liker.

Grep "(l.. e\). *\ 1" test.txt

He love his lover.

She like her liker.

Exercise:

1. Display lines in the / proc/meminfo file that begin with uppercase and lowercase s

# grep "^ [sS]" / proc/meminfo

# grep-I "^ s" / proc/meminfo

2. Remove the user whose default shell is non-bash.

# grep-v "bash$" / etc/passwd | cut-d:-F1

3. Select the user whose default shell is bash and whose ID number is the largest.

# grep "bash$" / etc/passwd | sort-n-t:-K3 | tail-1 | cut-d:-F1

4. Display a line in the / etc/rc.d/rc.sysinit file that begins with #, followed by at least one white space character, followed by at least one non-white space character

# grep "^ # [[: space:]]\ {1,\} [^ [: space:]]\ {1,\}" / etc/rc.d/rc.sysinit

5. Display lines in / boot/grub/grub.conf that begin with at least one white space character

# grep "^ [[: space:]]\ {1,\} [^ [: space:]]\ {1,\}" / boot/grub/grub.conf

6. Find one or two digits in the / etc/passwd file

# grep-- color=auto "\" / etc/passwd

7. Find the integer between 1 and 255 in the result of the ifconfig command

# ifconfig | grep-E-color=auto "\"

8. View all the information of root users on the current system

# grep "^ root\ >" / etc/passwd

9. Add users bash, testbash and basher, and then find out the users on the current system whose usernames are the same as the default shell

# grep-- color=auto "^\ ([[: alnum:]]\ {1,\}\)\ >. *\ 1 $" / etc/passwd

10. Find the lines that end with "LISTEN" or "ESTABLISHED" in the result of the netstat-tan command

# netstat-tan | egrep "(LISTEN | ESTABLISHED) [[: space:]] +"

11. Take out the shell of all users on the current system. It is required that each shell is displayed only once and in ascending order.

# cut-d:-f7 / etc/passwd | sort-u

Challenge: write a pattern that can match the real IP address; (1.0.0.1 Murray 223.255.255.254)

Egrep: use extended regular expressions to build patterns, equivalent to grep-E

Character matching:

. Match any single character

[] matches any single character within the specified range

[^] matches any single character outside the specified range

Number of times match:

* match any number of times, can be 0 or more times. * match any character of any length? Match 0 times or 1 time

+ match the characters in front of them at least once {m} match m times {m match n} at least m times, up to n times {m,} minimum times {0m n} most times n times

Anchor:

^ Anchor the beginning of the line (^ char)

Anchor end of line (char$)

^ $blank line

\ char\ b)

Grouping:

() Group

| | conC | the result of cat is conC or cat;con (C | c) at is conCat or concat |

Reference:

\ 1 to the reference, to reference the content matched by the first left parenthesis and the corresponding pattern in the right parenthesis.

Exercise: write a script to count the number of lines starting with # and the number of blank lines in / etc/rc.d/rc.sysinit, / etc/init.d/functions, and / etc/fstab files, respectively

#! / bin/bash

For fileName in / etc/rc.d/rc.sysinit / etc/init.d/functions / etc/fstab

Do

Line1= `egrep "^ #" $fileName | wc-l`

Line2= `egrep "^ $" $fileName | wc-l`

Echo "$fileName #: $line1 space:$line2"

Done

Exercise: write a script that copies the / etc/rc.d/rc.sysinit, / etc/init.d/functions and / etc/fstab files to the / tmp directory with the original name followed by the current date

For example, the name of the first file is / tmp/rc.sysinit-2014-02-16

#! / bin/bash

For fileName in / etc/rc.d/rc.sysinit / etc/init.d/functions / etc/fstab

Do

Cp $fileName / tmp/ `basename $fileName`-`date "+% F" `

Done

Exercise: write a script

Displays the user name, UID, and line number in the / etc/passwd file of all users on the current system whose default shell is bash

#! / bin/bash

For userName in `grep "bash$" / etc/passwd | cut-d:-f1`

Do

LineNum= `cat-n / etc/passwd | egrep "[[: space:]] + $userName" | egrep-o "[0-9] + [: space:] +" `

UserId= `egrep "^ $userName" / etc/passwd | cut-d:-f3`

Echo "$lineNum $userName $userId"

Done

Condition judgment of bash programming: determine whether the prerequisites for subsequent operations are met.

Common types of tests for conditional judgment:

Integer test

Character test

File testing

How to do testing in bash:

Test EXPRESSION

[EXPRESSION]

`EXPRESSION'

If is used for conditional judgment in bash:

Single branch:

If condition; then

Branch 1

Fi

Double branches:

If condition; then

Branch 1

Else

Branch 2

Fi

Multi-branch:

If condition 1; then

Branch 1

Elif condition 2; then

Branch 2

Elif condition 3; then

Branch 3

...

Else

Branch n

Fi

If the file has blank lines, the number of blank lines is displayed; otherwise, there are no blank lines in the file.

#! / bin/bash

#

Read-p "Enter a file path:" fileName

If grep "^ $" $fileName & > / dev/null; then

LinesCount= `grep "^ $" $fileName | wc-l`

Echo "$fileName has $linesCount space lines."

Else

Echo "$fileName hava no space line."

Fi

Bash programming: integer testing

Binary test:

Num1 OPRAND num2

-gt: greater than [$num1-gt $num2]

-lt: less than

-ge: greater than or equal to

-le: less than or equal to

-ne: not equal to

-eq: equal to

Custom script exit of bash knowledge points:

Exit [n]

Exercise: determine which of the two numbers is greater or less. the integer is passed through the command line argument.

#! / bin/bash

#

If [$#-lt 2]; then

Echo "Stupid..."

Echo "Usage: `basename $0` argu1 argu2"

Exit 4

Fi

If [$1-gt $2]; then

Echo "The max num is $1."

Else

Echo "The max num is $2."

Fi

Bash knowledge point: as long as a command is used as a condition, it means that the reference is the result of its status (that is, whether the execution is successful or not), not the output of the command, so you cannot use command substitutes.

Grep "^ root\ >" / etc/passwd

Id root

Exercise: write a script to achieve the following functions:

1. Let the user enter a user name through the keyboard

2. If the user exists, the user name and UID are displayed.

3. Otherwise, it shows that the user does not exist

#! / bin/bash

Read-t 10-p "Enter a username:" userName

# userName=$ {userName:-root}

If id $userName & > / dev/null; then

UserID= `id-u $userName`

Echo "$userName: $userID"

Else

Echo "$userName not exist."

Fi

Exercise: write a script to achieve the following functions:

1. Let the user enter a user name through the keyboard and exit if the user does not exist

2. If a user's UID is greater than or equal to 500, it means that it is an ordinary user

3. Otherwise, it means that this is an administrator or a system user

#! / bin/bash

# exit 6--

Read-t 10-p "Enter a username:" userName

If! Id $userName & > / dev/null; then

Echo "$userName not exist."

Exit 6

Fi

UserID= `id-u $userName`

If [$userID-ge 500]; then

Echo "A common user."

Else

Echo "Admin or System user."

Fi

Knowledge points of bash:

Combinatorial condition testing: doing logical operations on conditions

And: condition 1 & condition 2

If condition 1 is false, the final result must be false. Therefore, condition 2 will not be executed.

If condition 1 is true, the final result depends on the latter condition, so condition 2 must be executed.

Or: condition 1 | | condition 2

If condition 1 is true, the final result must be true, so condition 2 will no longer be executed.

If condition 1 is false, the final result depends on the latter condition, so condition 2 must be executed.

Non:! Conditions

Exercise: write a script to achieve the following functions:

1. Let the user enter a user name through the keyboard and exit if the user does not exist

2. If its UID equals its GID, it is said to be a "good guy"

3. Otherwise, say it is a "bad guy"

#! / bin/bash

# exit 6--

Read-t 10-p "Enter a username:" userName

If! Id $userName & > / dev/null; then

Echo "$userName not exist."

Exit 6

Fi

If [`userName`-u $id-eq `userName`-g $userName`]; then

Echo "Good guy."

Else

Echo "Bad guy."

Fi

Extension: determine whether all users on the current system are Good guy or Bad guy.

For userName in `cut-d:-F1 / etc/ passwd`; do

Done

Exercise: write a script that performs the following functions:

1. Add 10 user stu1-stu10;, but first determine whether the user exists.

2. If it exists, use red to show that it already exists

3. Otherwise, add the user and display it in green

4. Finally, it shows how many users have been added.

#! / bin/bash

#

Declare-I userCount=0

For i in {1... 10}; do

If id stu$i & > / dev/null; then

Echo-e "\ 033 [31mstu$i\ 033 [0m exists."

Else

Useradd stu$i & & echo-e "add\ 033 [32mstu$i\ 033 [0m finished."

Let userCount++

Fi

Done

Echo "Add $userCount users."

Exercise: find the sum of positive integers of all integer multiples of 3 within 200

#! / bin/bash

Declare-I sum=0

For i in {1... 200}; do

If [$[$I% 3]-eq 0]; then

Let sum+=$i

Fi

Done

Echo "The sum is: $sum."

Grep [optinos] "pattern" file...

Basic:

Character matching:., [] [^]

Times match: *,\?,\ {m\},\ {mrecoery n\}

Position Anchor:\, ^, $

Grouping:\ (\)

Forward reference:\ 1,\ 2

Extend:

Character matching:., [] [^]

Times match: *,?, {m}, {m ~ (1) n}, +

Position Anchor:\, ^, $

Grouping: ()

Forward reference:\ 1,\ 2

Or a | b

Conditional judgment

If condition;then

Statement1

....

Fi

If condition;then

Branch 1

Else

Branch 2

Fi

If condition;then

Branch 1

Elif condition;then

Branch 2

...

Else

Branch n

Fi

Conditional testing:

Bash: every command has a return value for its execution status

Success: 0

Failed: non-0

$?

The status return value of the script: the last command executed by the script:

Custom script status return value: exit [n]

Reference the execution result of the command: use `command` or $(command)

Refer to the status result of whether the command execution is successful or not: the command must be executed directly. At this point, you usually need to redirect the execution result to / dev/null

Conditional testing:

Test test expression

[test expression]

[[]]: keywords in bash

Knowledge points of bash:

Combinatorial condition testing: doing logical operations on conditions

And: condition 1 & condition 2

If condition 1 is false, the final result must be false. Therefore, condition 2 will not be executed.

If condition 1 is true, the final condition result depends on the latter condition, so condition 2 must be executed.

Or:

If condition 1 is true, the final result must be true. Therefore, condition 2 will not be executed.

If condition 1 is false, the final condition result depends on the latter condition. Therefore, condition 2 must be executed.

Not:

The priority of and is greater than or, or is higher than non-priority.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report