Introduction to Shell command-line parameter usage _linux shell

Source: Internet
Author: User
Tags exit in python script

A shell script, what is its command parameter?

First, shell scripting and scripting like Python can use parameters as well;
Secondly, the shell parameters are divided into positional parameters and internal parameters;

Where, positional parameter: is the parameter provided by the system, is that we generally say that an array of the first 1,2,3 ... element; you can get a parameter in the form of a $i, which is obviously $ is the program itself, and it's just the number one argument. Here's the thing to note, even if you execute the script with SH xxx.sh, $ 0 is still xxx.sh, not sh! This is consistent with our Perl python.

Internal parameters:

Copy Code code as follows:

$-----The name of the current program is actually an internal parameter, different from $1,$2 ... because it has to be!
$#----The total number of arguments passed to the program, which is the legendary array size
$? ----the previous code or shell program to exit in the shell, if the normal exit is returned 0, otherwise not 0 value.
$*----A string of all the parameters passed to the program.
$@----with "parameter 1" "Parameter 2" ... Save all parameters in the form
$$----This program's (process ID number) PID
$! ----The PID of the previous command

In this way, even with a shell, you can write powerful, interactive, and user-friendly scripting programs.

Finally, note that Python performs system commands in two different ways:
Os.system (CMD): direct one or a group of system commands cmd; do not return execution output that is the result; if executed under the Python command line, the output is output directly.
For example: Os.system (' ls-l ')

Os.open (cmd[, mode= ' R ' [, BufSize]]): The document's comment is to open a pipe from cmd, or open a pipe to CMD, and return the output as a file Object!
For example: t = os.popen (' ls *.gff '). ReadLines (); Print T

Summary, combined with the above two parts, you can flexibly handle the Python script and shell interaction.

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.