Tips on shell programming

Source: Internet
Author: User

1. print some header information
Command <dilimiter
......
......
Dilimiter

Use the content in the demarcation symbol dilimiter as the standard input of the command
It is often used in echo commands, which avoids the use of an echo command when no line is output, and the adjustment of the output format is also simplified.
Example: echo <something_message
**********************
Hello, welcome to use my shell script

**********************
Something_message

Output on the screen:
**********************
Hello, welcome to use my shell script
**********************

2. The <decomposition symbol can also be used to automatically select menus or implement automatic ftp transmission.

That is, the menu is automatically selected based on the nature of the decomposition symbol.
For example:./menu_choose> output_file 2> & 1 <Choose
2
3
Y
Choose
The script is automatically executed step by step: 2, 3, Y
<This type of attribute determines that it is an ideal useful tool for accessing the database. You can use it to enter various choices made when facing database prompts.

3. Create an empty file with a length of 0

Run the file_name command or the touch file_name command.

4. Some common shell Variables

$ # Number of parameters passed to the script
$ * Display all parameters passed to the script with a single string (up to 9)
$ ID of the current process running the script
$! ID of the last process running in the background
$ @ Is the same as $ #, but it is enclosed by quotation marks and each parameter is returned in quotation marks.
$-Display the current options used by shell
$? Displays the exit status of the last command. 0 indicates no error (this variable is often used to print the output. It indicates whether a shell command or function is correctly executed during script debugging, $? Records the exit status of the nearest function or command. Therefore, it should be printed immediately to obtain the correct information)
$0 usage
There is a location variable $ n in the variable to store the parameters passed in when a function call or script is executed. $0 indicates the function name or Script Name. Note that, the script name contains the full-path Script Name. From $1-$9 indicates the first to ninth parameters passed in. This parameter indicates no more than nine parameters. If there are more than nine parameters, you can use the shift command to read them.
Because $0 stores the function name or script name, we can use echo $0 to output the call Information. However, because it stores the full path name, we can use a shell command to get the script name. basename $0 will get the name of $0, which is the opposite, dirname $0 returns the path section in $0.

5. Shift Application

Use the head or tail command to specify the number of rows to query.
For example, to view the first 20 rows of a file: head-20 file_name
10 rows after viewing the file: tail-10 file_name

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.