The HelloWorld of Shell programming

Source: Internet
Author: User
Tags posix

The difference between/bin/sh and/bin/bash
SH: If there is an error in the previous statement, the command after the error statement does not execute
BASH: If there is an error in the previous statement, the following command will also execute
The difference between SH and bash, in fact, is that bash has no difference in the POSIX mode (following POSIX specific specifications, it is possible to include the specification: "Do not continue to explain when a line of code goes Wrong")

The first sentence of the script:
#!/bin/sh equivalent to #!/bin/bash--posix

First shell script
#!/bin/bash
#author Wangxiangyu
# 2017-05-29
Echo ' Wangxiangyu, Yuanyuan and lovely Susu is a happy family '

Attention:
#!/bin/bash represents the use of the bash version of the shell script, fixed format
Echo Output statement
If you write a shell script in a Windows system environment, the "/bin/sh^m:bad interpreter:no such file or directory" exception is reported because the two systems have some characters (such as a carriage return) that are different. Linux cannot be identified, can be used: Dos2unix hello.sh command conversion format

The system may not have a package to install this command, you can install it manually, use the command: yum-y install Dos2unix

How scripts are executed
1, give the executive permission to run directly
chmod 755 hello.sh
./hello.sh (relative path) or/test/hello.sh (absolute path)
2. Execute script via bash call
Bash hello.sh

Output redirection

Standard output redirection
Command > file writes the correct output of the command to the specified file or device in a overwritten manner
Command >> file writes the correct output of the command to the specified file or device in an additional way
Standard Error Output redirection
Error command 2> file to overwrite the error output of the command to the specified file or device, no space between 2 and >
Error command 2>> file writes the error output of the command to the specified file or device in an append way
Correct output and error output are saved simultaneously
Command > File 2>&1 or command &> files are overwritten to save the correct output and error output to the same file
Command >> file 2>&1 or command &>> file is appended to save the correct output and error output to the same file
Command >> file 2>> file Two append the correct output to the file 1 and append the wrong output to file 2
Note: If the file does not exist, it will be created in the current directory

Input redirect

Wc-c Statistics Bytes-W Statistics number of words-l count rows

Multi-command sequential execution with pipe character

; command 1; Command 2 Multiple command order execution, no logical connection between commands
&& Command 1&& command 2 logic with, when command 1 is executed correctly, then command 2 executes; When command 1 is executed incorrectly, command 2 does not execute
|| Command 1| | Command 2 logic or, command 2 does not execute when command 1 is executed correctly, and command 2 does not execute when command 1 is executed correctly
| command 1| command 2 pipe character, command 1 's correct output as command 2 action object
Command && echo yes | | Echo no command executes successfully prints Yes, command execution fails print no

DD if= input file of= output file bs= number of bytes count=
if= input file Specify source file or source device
of= output file specifies the target file or target device
bs= bytes Specifies how many bytes of input/output at a time, that is, to treat these bytes as a block of data
Number of count= specify how many data blocks to input/output
Date DD If=/dev/zero of=/root/testfile bs=1k count=102400;date That's how long it takes to create a 100M file testfile

Netstat-an | grep established view all connected networks in the current system

Wildcard characters

? Match an arbitrary character
* Matches 0 or any number of any character, that is, can match any content
[] matches any one of the characters in the brackets. For example: [ABC] must match a character, or a, or B, or C
[-] matches any one of the characters in the brackets-represents a range. For example: [A-z] stands for matching a lowercase letter
[^] logical non, indicating that a match is not a character within the brackets, for example: [^0-9] represents a character that matches a number other than a digit

Variable

Accounting

The HelloWorld of Shell programming

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.