Shell scripting correlation, floating point, looping

Source: Internet
Author: User

Put a bunch of commands in the script into executable script execution;

Script, file name is. SH, get hostname host name

System variables:

ENV: View System Variables

Set: All variables can be printed with grep and

Export path: Define Global variables Path

Get the actual usage in memory to:

Mem_used= ' Free | awk ' Nr==3{print $ i} '

Echo $mem _use

Man test: View the relational operator, Test equals []

Relational operations:

Greater than sign (>)-gt

Less than-LT

Greater than or equal to-ge

Less than or equal to-le

equals-eq

Not equal to –ne

&& A

|| -O

Echo $? Determine if the last calculation is set up 0 is established, 1 is not established

Assignment operators:

EXPR 1 + 2 Calculator

Floating-point arithmetic:

Install the BC package first

Echo ' scale=2;30/1000 ' |bc–l:scale=2 to retain two decimal places 30 divided by 1000

To count the current memory usage:

Cut–d. F2:2 digits after intercept point number

Catalog Test

-D Test Directory is present

-E File exists

-F file exists and is an ordinary file

-H file exists and is a connection file

-W file exists and has write permission to the current user for the file

-r file exists, and the current user has read access to the file

-s file exists and is a non-empty file

String test:

-Z Empty string

-N Non-empty string

! = two strings are not equal

= Two strings equal

If statement:

Write a script that executes the script

Var= '/etc/passwd ' to determine whether this var is an ordinary file, whether it is a block file, whether it is a directory

Let the user enter the path:

After the command with parameters, the file inside the corresponding is the script after the first parameter, with a space as a separator, the other values and so on:

To pass parameters to the script:

The file name of the fetch

The PID of the process taken by the $$

$* parameter values followed by all scripts

$ #一共11个值

$?

Write a script to determine if Nginx is alive, and if it dies, start it:

The script is specific as follows:

While loop:

While: A colon is always true

The following will form a dead loop:

To refresh the results of free in real time:

Echo–e "AAA\NBBB":-e is the format of the distinction, the inside of \ n recognized is a newline character

The following script implements three value swaps:

Loop print 10 numbers, from 1 to 10:

Exit represents exiting the entire program

Loop through the user name password until you enter the correct back-out:

Breake jump out of this layer, that is, jump out of the Whiel loop

Contiune jump out of this cycle

Loop inside plus loop:

If the user name or password entry is illegal (that is, non-null), you need to make a decision:

-Z Empty string

-A and

-O or

|| Or

&& and

$cmd can add "" to form a whole, without adding the input value with a space or two values will be an error

Print numbers from 1 to 100; 1..100 is 1 to 100.

Print a-to-Z value:

command result for LS: ' Ls/boot '

Take ' ls/boot ' results each time to I

Write script to detect whether an IP is occupied:

A direct ping, the difference is no one to use

PING–C1 IP:-c1 is only ping once

$? Determines whether the previous command succeeded to 0

> Direct the results on the left to the file on the right/dev/null this is a black hole file, put the unused directly inside it can be;

Case Syntax:

Function:

Define functions: Add semicolons after each command

The ABC inside is the function name, download the following dozens of call directly

You can use functions to write installation software commands and start commands

Return is the returned value and can be artificially controlled

After executing the script, use the following to print the return value:

Inside the script, get the first argument after executing the script

Shell scripting correlation, floating point, looping

Related Article

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.