That's how Linux should learn.--shell Script Basic Application

Source: Internet
Author: User

1. Receive the user's parameters:

In order to give the user more flexibility to complete the work requirement, the shell script can pass parameters when executing the command: (command name parameter 1 parameter 2 ... )

Shell pre-defined variables:

$ $ Current program name for executing shell scripts

$1-9,${10},${11} ...

$# How many parameters are there in total?

$* values for all positional variables

$? Determines whether the previous command succeeded, 0 for success, and not 0 for failure.

eg.

Create and edit demo.sh:

Vim demo.sh

Edit the contents as follows:

#! /bin/bashecho "The current script name is $ $" echo "has a total of $ #个参数, respectively $*" echo "the 1th parameter is $ $, and the 5th is $"

2. Determine the user's parameters

Shell scripts sometimes have to determine the user input parameters, such as the mkdir command, when the directory does not exist to create, if there is an error, the conditional test statement can test whether a particular expression is true, when the condition is set to return a value of 0, otherwise return other values.

Format: [conditional expression] (there is a space on both sides)

Subdivision test statements are: File test, logic test, Integer value comparison, string comparison

File test: [operator file or directory name]

-D test is a directory

-e test whether a file or directory exists

-F to determine whether the file

-R tests whether the current user has permission to read

-W tests whether the current user has permission to write

-X tests whether the current user has permission to execute

eg.

Determine if/home is a directory:

[-d/home]

Displays the return value of the previous command, not 0 is the failure, which is not the directory:

echo $?

Determine if/home/demo.sh is a file:

[-d/home/demo.sh]

The symbol && represents the logical "and", and when the previous command executes successfully, it executes the subsequent command to determine if the/home/demo.sh device exists and outputs exit if it exists:

[-e/home/demo.sh] && echo "Exit"

Logic test: [expression 1] operator [expression 2]

&& logical with, "and" meaning

|| The meaning of the logical OR, "or"

! Logic's No

eg.

The user variable is the username currently logged in:

Echo $USER

If the currently logged-on user is not root, output user and the result is empty after execution:

[$USER! = root] && echo "USER"

Log in to Ftpuser, and then output the user word for testing:

Replace the root user with a stronger version of the judgment statement, non-root user output user, if root directly output root:

[$USER! = root] && echo "USER" | | echo "Root"

Here, please think about && | | The logical meaning, because the front of the && is not tenable, all the later | | will not be executed.

Integer value comparison: [Integer 1 operator integer 2]

-eq judgment is equal to

-ne judgment is not equal to

-gt Judging whether it is greater than

-lt Judging whether it is less than

-le judgment is equal to or less than

-ge judgment is greater than or equal to

Compare 10 is greater than 10:

[10-GT 10]

Compare 10 equals 10:

[10-eq 10]

Gets the amount of memory currently available and assigns the value to the variable Freemem:

First use FREE-M to see the memory usage in M, then grep Mem: Filter out the remaining memory lines, and finally use awk ' {print $} ' filter to preserve only the third column, while the freemem= ' statement ' means to assign a value to the variable after executing the statement inside.

Freemem= ' Free-m | grep mem:| awk ' {print $} '

Freemem= ' (here is the inverted quote, TAB key above, 1 left)

Freemem= ' Free-m | grep mem:| awk ' {print $} '

Determines whether this value is less than 2048 (in units of M) and indicates insufficient memory if less than:

[$FreeMem-lt 2048] && echo "Low Memory"

string comparison: [string 1 operator String 2]

Operator:

= Compare string Contents for same

! = Compares string contents differently

-Z to determine if string contents are empty

Determine if a string variable is a null value:

[-Z $String]

The previous command executed successfully, stating that the variable string is indeed null:

echo $?

Output the current system language:

Echo $LANG

Determine if the current system language is in English, otherwise the output is "not English":

[$LANG! = "en. US "] && echo" not English "

That's how Linux should learn.--shell Script Basic Application

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.