Bash Shell Script Learning Summary _linux Shell

Source: Internet
Author: User
Tags comparison comparison table syslog

1. Character truncation:

If the character truncation is a generic path, you can use both the basename and dirname tools:
BaseName can truncate a filename from a file path

For example:

Copy Code code as follows:

$ basename/home/file.tar
File.tar

DirName can be truncated to a directory path from a file path
For example:
Copy Code code as follows:

$ dirname/home/file.tar
/home

Do not use external tools for character truncation
Bash has its own features to truncate variables, generally using the "# #", "#", "%", "%", "*" combination to achieve. For example:
Copy Code code as follows:

$ string=hellowbashshell
$ echo ${string##*sh}
Ell
$ echo ${string#*sh}
Shell
$ echo ${string%%sh*}
Hellowba
$ echo ${string%sh*}
Hellowbash

"#" is dropped from the beginning of the character and immediately after the match
"# #" means to remove from the beginning of the character, searching for the longest match of the entire string to remove
"%" is removed from the end of the character and immediately removed when the match is made into a public
Percent% means that the end of the character is removed and the longest match is searched for the entire character to remove
The "*" wildcard character, commonly associated with "# #" or "#", is placed on the left side of the search string, for example: ${string#*sh} (on the left of SH), and "%" or "%" will be placed on the right side of the matching string, for example: ${string%%sh*}

Common skills:

Take filename in path: ${path##*/} (same function as basename)
Directory path in Path: ${path%/*} (same function as dirname)
Fetch file extension: ${path##*.}

2. The receipt of the independent variable

Receives arguments passed in from the command line, the first parameter is represented by $, and the second argument is $ ... Analogy Note: $ represents the script filename. Another common use in shell programming is "$@", which represents all the parameters. You can use a loop to traverse this argument. If you use Java analogy, you can think of $@ as the array defined in the man function.

3.if statement:
Format:

Copy Code code as follows:

if [condition]
Then
Action
Fi

Note: Spaces are required between "if" and "[", and if you do not have spaces, the shell will report syntax errors. That's what I've been wasting for so long.

Conditon Test Type comparison table

Operator Describe Example
File comparison Operators
-e filename True if filename exists [-e/var/log/syslog]
-D FileName True if filename is a directory [-d/tmp/mydir]
-F filename True if filename is a regular file [-f/usr/bin/grep]
-L filename True if filename is a symbolic link [-l/usr/bin/grep]
-R FileName True if filename is readable [-r/var/log/syslog]
-W filename True if filename is writable [-w/var/mytmp.txt]
-X filename True if filename is executable [-l/usr/bin/grep]
filename1-nt filename2 True if filename1 is newer than filename2 [/tmp/install/etc/services-nt/etc/services]
filename1-ot filename2 True if the filename1 is older than filename2 [/boot/bzimage-ot Arch/i386/boot/bzimage]
string comparison operators (note the use of quotes, which is a good way to prevent spaces from disrupting code)
-Z string True if the string length is zero [-Z "$myvar"]
-N String True if the string length is Non-zero [-N ' $myvar]
string1= string2 True if string1 is the same as string2 ["$myvar" = "One Two Three"]
string1!= string2 True if string1 is different from string2 ["$myvar"!= "one Two three"]
Arithmetic comparison operators
num1-eq num2 Equals [3-eq $mynum]
num1-ne num2 Not equal to [3-ne $mynum]
num1-lt num2 Less than [3-lt $mynum]
num1-le num2 Less than or equal to [3-le $mynum]
num1-gt num2 Greater than [3-gt $mynum]
num1-ge num2 Greater than or equal to [3-ge $mynum]

If you feel that the IF in bash is more intelligent than some other languages, in bash, testing the existence of a file is no different than comparing the size of two numbers;)

4.for Statement

The statements in bash are always so human, so close to the natural language that you can almost iterate over any data type similar to the collection in a For statement (perhaps that's not true, but I really can't think of a better word to replace it).
Look at an example:
Copy Code code as follows:

#!/bin/bash
For args in $@
Todo
Echo $args
Done


Save the above code entry as Showargs.sh set to executable (chmod +x showargs.sh) Execution:

Copy Code code as follows:

$./showargs.sh arg1 arg2 Arg3 arg4
Arg1
Arg2
Arg3
Arg4


In this example, we use the "$@", which represents all command-line arguments. In this case, the system iterates through the $@, takes the command-line arguments out of the args, and then outputs them using the Echo $args.
For more frequent traversal of directories, the following example is used to list the names of all files and folders under the current directory

Copy Code code as follows:

$ for file in *
> Do
> Echo $file
> Done

Here the * represents the current directory, listing all the names of files and folders, where folders and files are not separate, and if you need to, you should use if [D-${file}] to make a judgment.
What's even more interesting about file traversal is that you can connect multiple expressions after "in". In other words, you can traverse multiple directories at once.

The following code copies the files in the Go folder and do folder under the current directory to the FO folder
Copy Code code as follows:

#!/bin/bash
For args in./go/*./do/*
Todo
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.