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

The skills of using "one" in shell script

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

First, determine whether the string is an integer.

["echo" 123a "| sed-r's # [0-9] # # g'" = "123a"] & & echo 1 | | echo 0

Second, monitor whether the db service is normal (multiple methods)

#! / bin/sh

If ["netstat-lnt | grep 3306 | awk-F" [:] + "]'{print $5}'" = "3306"]

# if [lsof-I tcp:3306 | wc-l-gt 0]

# if [ps-ef | grep mysql | grep-v grep | wc-l-gt 0]

# if [nc-w 2 192.168.1.189 3306 & > / dev/null & & echo ok | grep ok | wc-l-gt 0]

# if [nmap 192.168.1.189-p 3306 2 > / dev/null | grep open | wc-l-gt 0]

# if [ss-lntup | grep mysqld | wc-l-gt 0]

Third, monitor whether the web service is normal.

#! / bin/sh

If ["netstat-lnt | grep 80 | awk-F" [:] + "]'{print $5}'" = "80"]

# if [lsof-I tcp:80 | wc-l-gt 0]

# if [ps-ef | grep httpd | grep-v grep | wc-l-gt 0]

# if [ss-lntup | grep httpd | wc-l-gt 0]

Wget-T 10-Q-- spider http://192.168.0.101 > & / dev/null

Curl-s http://192.168.0.10 > / dev/null

If [$?-eq 0]

Echo "httpd is running"

Else

Echo "httpd is not running"

/ app/apache/bin/apachect1 start

Fi

Instructions for using the command:

The curl command curl is a file transfer tool that uses URL rules to work under the command line; the command line visits the website

Parameters.

-s/--silent mute mode. Just don't show errors and progress.

The wget command inux wget is a tool for downloading files, which is used on the command line

-the timeout second is set by default.

-qmam copyright does not display output information.

-- spider tests whether it can be accessed properly

-t,-- tries=NUMBER sets the maximum number of attempted links (0 means unlimited).

Fourth, the usage and explanation of ${parameter:+expression}

Note: if parameter has a value and is not empty, use the value of expression

Example: write a shell script to define the function to set the path of environment variables

Prepend () {

[- d "$2"] & & eval $1 =\ "$2\ ${$1 export $1]

}

Execute prepend PATH / opt/myapp/bin

Description:

1. Execute prepend PATH / opt/myapp/bin

Function name first parameter $1 second parameter $2

2. The execution process of the program

[- d "$2"] determine whether the second parameter $2 is a directory or not. If so, execute eval $1 =\ "$2\ ${$1VOF"\ $1}\ "

& & the first condition is established to implement the next

Eval $1 =\ "$2\ ${$1VlVlVlVO"\ $$1}

The eval command is to recalculate the contents of the parameters

Is to set the variable of the value set by the first parameter to the value of the second parameter plus ":" (path separator); then follow the original value of the first parameter

\ escape special characters such as "$"

{$1\ $1} use\ $$1 if you judge that $1 is empty.

The result after passing parameters

Eval

PATH = "/ opt/myapp/bin ${PATH:+':'$PATH}"

PATH

Fifth, change the column to the specified row

[root@localhost scripts] # cat b.log

one

two

three

four

five

six

seven

eight

nine

ten

[root@localhost scripts] # xargs-N4 / dev/null

If [$?-ne 0-a "$1"! = "- 1"]; then

Return 1

Fi

Return 0

}

Seventh, shell debugging skills

Use the dos2unix command to deal with the steps from developers under Windows

Sh-x step name

-x displays the footsteps and output of the execution to the screen and then executes. If there is an error, there will be a prompt.

Eighth, the method of obtaining the length of characters

${# a}

${a} | wc-L

Expr length "${a}"

Ninth, the bash for loop prints the words with no more than 6 letters in the following sentence.

I am alibbb teacher welcome to oldboy training class.

Script 1

#! / bin/bash

#

# array= (I am alibbb teacher welcome to oldboy training class)

# for word in ${array [*]}

For word in I am alibbb teacher welcome to oldboy training class

Do

If [${# word}-le 6]; then

Echo-n "$word"

Fi

Done

Echo

Script 2

#! / bin/bash

Array= (I am alibbb teacher welcome to oldboy training class)

# for word in ${array [*]} for (

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

Servers

Wechat

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

12
Report