1. Character truncation:
For normal path truncation, you can use the basename and dirname tools:
Basename can intercept a file name from a file path.
For example:
Copy codeThe Code is as follows: $ basename/home/file.tar
File.tar
Dirname can be truncated from a file path to a directory path.
For example:Copy codeThe Code is as follows: $ 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:Copy codeThe Code is as follows: $ string = hellowbashshell
$ Echo $ {string # * sh}
Ell
$ Echo $ {string # * sh}
Shell
$ Echo $ {string % sh *}
Hellowba
$ Echo $ {string % sh *}
Hellowbash
"#" Indicates that it is removed from the starting part of the character. It is removed immediately after matching.
"##" Indicates to remove from the starting part of the character and search for the matching of the longest sum of the entire string.
"%" Indicates to remove from the end of a character. If it matches a public character, it is immediately removed.
"%" Indicates to remove from the end of the character and search 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:
Copy codeThe Code is as follows: 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:Copy codeThe Code is as follows :#! /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:
Copy codeThe Code is as follows: $./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.
Copy codeThe Code is as follows: $ 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.Copy codeThe Code is as follows :#! /Bin/bash
For args in./go/*./do /*
Do
Cp $ {args}./fo
Echo "copying $ {args} to./fo/$ {args }"
Done