Sharing several tips in shell programming _linux shell

Source: Internet
Author: User
Tags echo command

1. Print some header information
Command << Dilimiter
......
......
Dilimiter

The standard input of the command as the content of the boundary symbol Dilimiter
Commonly used in the echo command, this avoids the need to use an echo command without outputting a line, and the adjustment of the output format is also simpler.
For example: Echo << something_message
**********************
Hello, welcome to with my shell script

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

will be printed on the screen:
**********************
Hello, welcome to with my shell script
**********************

2, the use of << decomposition of the nature of the symbol can also automatically select the menu or implement automatic FTP transmission

That is, use the nature of the decomposition symbol to automatically select the menu.
For example:./menu_choose >>output_file 2>&1 <<choose
2
3
Y
Choose
Automatically makes a choice in the process of executing scripts: 2,3,y
<< This nature determines that it is a useful tool for accessing the database, and can be used to enter the various choices made when facing database prompts.

3. Create an empty file with a length of 0

Execute > File_name command or Touch file_name command.

4. Some common shell variables

$# the number of arguments passed to the script
$* displays all parameters passed to the script in a single string (greater than 9)
The ID number of the current process that the $$ script runs
$! ID number of the last process running in the background
$@ and $ #相同, but use quotes and return each argument in quotation marks
$-Displays the current options used by the shell
$? Displays the exit status of the last command, 0 indicates no errors (this variable is often used to print out the output, mark a shell command or a function to execute correctly when the script is debugged, but note that $?) records the exit status of the most recent function or command, so printing should be printed immediately to get the correct information.
Use of $
There is a positional variable $n in a variable that holds the arguments passed in for function calls or script execution, where $ $ denotes the function name or script name, and it should be noted that the script name passes the script name that contains the full path. The first to Nineth arguments passed in from $1-$9 represent no more than nine, and if more than nine, they can be read using the shift instruction that is mentioned below.
Because $ holds the function name or script name, so we can output the invocation information through echo $, but since we have the full path name, we can use a shell command to get the script name, basename $ will get the part of the name in the $, and in contrast, dirname $ 0 will get the part of the path in $ $.

5, the use of shift

Specify the number of rows to be consulted with the head or tail instruction
For example: The first 20 lines of a lookup file: head–20 file_name
10 lines after lookup: tail–10 file_name

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.