Shell tips (1)

Source: Internet
Author: User

1. test command

The test command is used to check whether a condition is true. It can be used to test values, characters, and files. The test characters and corresponding functions are as follows:

(1) numerical test:
-Eq: true if it is equal
-Ne: true if not equal
-Gt: true if the value is greater
-Ge: true if the value is greater than or equal
-Lt: true if the value is smaller
-Le: true if the value is less than or equal

(2) string test:
=: Equal to true
! =: True if not equal
-Z string: the pseudo-String Length is true.
-N string: the string length is true if it is not pseudo.
(3) file test:
-E file name: true if the file exists
-R file name: true if the file exists and is readable
-W file name: true if the file exists and can be written.
-X file name: true if the file exists and can be executed
-S file name: true if the file exists and contains at least one character
-D file name: true if the file exists and is a directory
-F file name: true if the file exists and is a normal file
-C file name: true if the file exists and is a special character file
-B file name: true if the file exists and is a special file.

In addition, Linux also provides , Or ("-o), non ("-a ") three logical operators are used to connect test conditions, the priority is:"!" Highest, followed by "-a", and "-o. Bash can also perform simple arithmetic operations in the following format:
$ [Expression]
Example: var1 = 2
Var2 = $[var1*10 + 1]
Then, the value of var2 is 21.
2. if Condition Statement

If [-x/sbin/quotaon]; then
Echo "Turning on Quota for root filesystem"
/Sbin/quotaon/
Elif [-x/sbin/quotaon]; then
/Usr/bin/bash
Else
Echo "OK"
Fi
3. for Loop

#! /Bin/sh
WORD = "a B c d e f g h I j l m n o p q r s t u v w x y z"
For I in $ WORD; do
Echo $ I
Done
#! /Bin/sh
FILES = 'ls/txt/*. txt'
For txt in $ FILES; do
Doc = 'echo $ txt | sed "s/. txt/. doc /"'
Mv $ txt $ doc
Done
4. while and until Loops

#! /Bin/sh
While [-f/var/run/pp1_pid]; do
Killall pppd
Done
#! /Bin/sh
Until [-f/var/run/pp1_pid]; do
Sleep 1
Done
Shell also provides two commands, true and false, to create an infinite loop structure. Their return states are always 0 or not 0.
5. Select case conditions

#! /Bin/sh
Case $1 in
Start | begin)
Echo "start something"
;;
Stop | end)
Echo "stop something"
;;
*)
Echo "Ignorant"
;;
Esac
The case expression can also use shell wildcards ("*", "?" , "[]").
6. Unconditional control statements break and continue

Break is used to terminate the execution of the current loop immediately, while contiune is used to start the execution of the next loop immediately without executing the statements following the loop. These two statements are valid only when they are placed between do and done.
7. Function Definition

You can also define functions in shell. A function is actually composed of several shell commands, so it is similar to a shell program in form. The difference is that it is not a separate process, but a part of the shell program. The basic format of the function definition is:
Functionname
{
Several command lines
}
The format of the called function is:
Functionname param1 param2 ......
Shell functions can complete some routine work and have their own exit States. Therefore, functions can also be used as conditions for control structures such as if and while. Parameters are not required for function definition, but can be included when a function is called. In this case, shell will assign these parameters to the corresponding location parameters $1, $2 ,... and $ *.


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.