4.Shell determine the user's parameters

Source: Internet
Author: User

1.Shell determine the user's parameters

When the system executes the mkdir command, it will judge the information entered by the user, that is, whether the user-specified folder name already exists, if there is an error, and the other is automatically created.

The conditional test syntax in the shell script can determine if the expression is true, and returns the number 0 if the condition is true, otherwise it returns other random values.

condition Test Syntax: the execution format is shown in 4-16. Remember that there should be a space on both sides of the conditional expression

  

 According to the test object to divide, the condition test statement can be divided into 4 kinds:

File test statement;

logical test statement;

Integer value comparison statement;

A string comparison statement.

 A file test is an operator that uses the specified criteria to determine whether a file exists or whether the permission satisfies the condition, as shown in table 4-3:

Operator Role
-D Test whether the file is a directory type
-E Test whether the file exists
-F Determine if it is a generic 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
2: Example

Use the file test statement below to determine if/etc/fstab is a directory-type file, and then through the shell interpreter, the $? variable displays the return value after the previous command was executed. If the return value is 0, the directory exists;

If the return value is nonzero, it means that the directory does not exist:

[[Email protected] ~]# [-d/etc/echo $? ] 1

Then use the file test statement to determine whether/etc/fstab is a generic file, and if the return value is 0, the file exists and is a generic file:

[[Email protected] ~]# [-f/etc/echo $? ] 0

  logical statements are used for logical analysis of test results , and different effects can be achieved based on test results. For example, in the shell terminal, the logical "and" operation symbol is &&, which means that the command after the previous command execution succeeds,

It can therefore be used to determine if the/dev/cdrom file exists, and if so, the output exist word.

Echo " Exist " Exist

In addition to the logical "and", there is also a logical "or", its operation symbol in the Linux system is | |, indicating that after the execution of the previous command failed to execute the command after it, it can be used to combine the system environment variable user to determine the current login

is not a user of the administrator identity:

SH Echo  sh]#  echo"user"shsu -  echo"user"user

The third logical statement is "non", and the operation symbol in the Linux system is an exclamation mark (! ), which represents the inverse value of the judgment in the condition test. That is, if the result of the original test is correct, it becomes wrong;

The result of the original test error is turned into the correct.

[Email protected] ~echo"Administrator"Administrator

To determine whether the user variable name of the current logged-on user is equal to root, and then use the logical operator "non" to reverse the operation, the effect becomes to determine whether the currently logged on users are non-administrator users. Finally, if the condition is established, it will be based on logos

The "and" operator outputs the word "user", or if the condition is not satisfied, the word "root" will be output through the logical "or" operator, and if the previous && is not established then the following | | Symbol.

Echo " User " Echo " Root " Root

Use the canonical integer comparison operator to manipulate numbers, not to manipulate numbers with strings, files, and so on

Operator Role
-eq is equal to
-ne is not equal to
-gt is greater than
-lt is less than
-le is equal to or less than
-ge is greater than or equal to

Let's test whether 10 is greater than 10 and 10 is equal to 10 (judging by the output return value content):

Ten Ten  Echo $? 1   ten echo $? 0

The free command, which can be used to obtain information about the amount of memory that the current system is using and available. The next step is to use the FREE-M command to see the memory usage (in megabytes) and then filter out the remaining amount of storage by using the grep Mem: command.

Use the awk ' {print $4} ' command to keep only the fourth column, and finally assign the result of execution in the statement to the variable in the form of freemem= ' statement '

[Email protected] ~]# Free-m Total used Freeshared buffers Cachedmem:1826      1244     582      9          1           413-/+ Buffers/cache:830 996Swap:2047      0        2047[[Email protected]~]# Free-M |grepMem:mem:1826      1244     582      9[[Email protected]~]# Free-M |grepMem: |awk '{print $4}'582[[Email protected]~]# freemem= ' Free-M |grepMem: |awk '{print $4}'' [[email protected]~]#Echo$FreeMem582

  We use the integer operator to determine if the value of memory available is less than 1024, and if it is less then it will prompt the words "Insufficient memory" (out of RAM):

1024x768 Echo " Insufficient Memory " Insufficient Memory

A string comparison statement is used to determine whether a test string is a null value, or whether two strings are the same. It is often used to determine whether a variable is undefined (that is, the content is null), and it is easier to understand.

The common operators in string comparisons are shown in table 4-5:

Operator Role
= Compare strings for the same content
!= Compare strings for different content
-Z Determines whether the string contents are empty

The next step is to determine if the variable is defined by determining whether the string variable is a null value:

[[Email protected] ~]# [-echo $? ] 0

Try introducing a logical operator again. When the environment variable value lang is not English (en.us) used to save the current language family, the logical test condition is met and output "not en." US "(non-English) words:

Echo $LANGen _us. UTF-8"en. US"echo"noten. US"noten. US

  

4.Shell determine the user's parameters

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.