Shell script learning Summary-string processing and branch statements

Source: Internet
Author: User
Tags syslog
BASH Shell script learning Summary. It is easy to determine whether a log file is larger than 2 GB. If it is larger than 2 GB, delete it. I haven't written bash for a long time, so I can't even think of it. So I reviewed it and made a summary of today's learning:
1. Character truncation:
You can use BasenameAnd DirnameThese two tools:
BasenameYou can capture a file name from a file path.
For example:

$ Basename/home/file.tar
File.tar

DirnameYou can capture a directory path from a file path.
For example:

$ Dirname/home/file.tar
/Home

Do not use external tools for character Truncation
Bash has its own function to truncation variables. It is generally implemented by combining "#", "#", "%", "%. For example:

$ String = hellowbashshell
$ Echo $ {string # * Sh}
Ell
$ Echo $ {string # * Sh}
Shell
$ Echo $ {string % sh *}
Hellowba
$ Echo $ {string % sh *}
Hellowbash

"#"Indicates that the part is removed from the beginning of the character. It is removed immediately after matching.
"##"Indicates that the string is removed from the starting part of the character, and the longest matching of the entire string is searched.
"%"Indicates to remove the part from the end of the character. It is immediately removed once it is matched to the public.
"%"Indicates that it is removed from the end of the character and will be searched for the longest match in the entire character.
"*"Unified character, usually associated with" # "or" # ", is placed on the left of the search string, for example: $ {string # * Sh} (on the left of SH ), when used together with "%" or "%", it is placed on the right of the matching string, for example: $ {string % sh *}

Common tips:

Get the file name in the path: $ {path ##*/} (same as basename)
Path: $ {PATH %/*} (same as dirname)
File Extension: $ {path ##*.}

2. Receipt of Independent Variables
Receives parameters passed in from the command line. The first parameter is represented by $1, and the second parameter is represented by $2 ,... And so on. Note: $0 indicates the script file name. In shell programming, "$ @" is often used to represent all parameters ,. You can use a loop to traverse this parameter. If you use Java for analogy, You can regard $ @ as the array defined in the man function.

3. If statement:
Format: If [condition]
Then
Action
Fi

Note: a space is required between "if" and "[". If you do not have a space, shell reports a syntax error. I was wasted on this for a while.

Conditon test type table

Operator Description Example
File comparison operator
-EFilename IfFilenameYes, true [-E/var/log/syslog]
-DFilename IfFilenameIs a directory, it is true [-D/tmp/mydir]
-FFilename IfFilenameIs a regular file, it is true [-F/usr/bin/grep]
-LFilename IfFilenameIs a symbolic link, it is true [-L/usr/bin/grep]
-RFilename IfFilenameReadable, true [-R/var/log/syslog]
-WFilename IfFilenameWritable, true [-W/var/mytmp.txt]
-XFilename IfFilenameExecutable, true [-L/usr/bin/grep]
Filename1-NTFilename2 IfFilename1RatioFilename2New, true [/Tmp/install/etc/services-nt/etc/services]
Filename1-OtFilename2 IfFilename1RatioFilename2Old, true [/Boot/bzimage-ot ARCH/i386/boot/bzimage]
String comparison operator(Please pay attention to the use of quotation marks. This is a good way to prevent space from disturbing the code)
-ZString IfStringIf the length is zero, it is true. [-Z "$ myvar"]
-NString IfStringNon-zero length, true [-N "$ myvar"]
String1=String2 IfString1AndString2Same, true ["$ Myvar" = "One Two Three"]
String1! =String2 IfString1AndString2Otherwise, true ["$ Myvar "! = "One Two Three"]
Arithmetic comparison operator
Num1-EQNum2 Equal [3-EQ $ mynum]
Num1-NeNum2 Not equal [3-ne $ mynum]
Num1-LtNum2 Less [3-lt $ mynum]
Num1-LeNum2 Less than or equal [3-Le $ mynum]
Num1-GTNum2 Greater [3-GT $ mynum]
Num1-GeNum2 Greater than or equal [3-ge $ mynum]

It seems that if in Bash is more intelligent than other languages. In bash, the existence of a file is similar to comparing the size of two numbers.

4. For statement
Bash statements are always so user-friendly and very similar to natural languages. In a for statement, almost any data type similar to a set can be iterated (maybe this is not true, but I really cannot think of better words to replace ).
Let's look at an example:

#! /Bin/bash
For ARGs in $ @
Do
Echo $ ARGs
Done

Save the above code entry as showargs. SH and set it to execute (chmod + x showargs. Sh:

$./Showargs. Sh arg1 arg2 arg3 arg4
Arg1
Arg2
Arg3
Arg4

In this example, we use "$ @", which represents all command line parameters. Here, we use for to traverse it. The system iteratively extracts the command line parameter from $ @ and puts it into args. Finally, we use echo $ ARGs for output.
For is more often used to traverse directories. The following example is used to list the names of all files and folders in the current directory $ for file in *
> Do
> Echo $ File
> Done

Here, "*" represents the current directory and lists the names of all files and folders. Here, folders and files cannot be separated. If you need, you should use if [-d $ {file}] for judgment.
For file traversal, you can add multiple expressions after "in. That is to say, you can traverse multiple directories at a time.
The following code copies the files in the go folder and do folder in the current directory to the fo folder #! /Bin/bash
For ARGs in./go/*./do /*
Do
CP $ {ARGs}./fo
Echo "copying $ {ARGs} To./FO/$ {ARGs }"
Done

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.