Test switch and special parameters in Shell script, shell script

Source: Internet
Author: User
Tags file separator

Test switch and special parameters in Shell script, shell script
1. Test Switch

The following table lists the meanings of the test commands and whether they can be used in test commands, bash, and ksh.

Switch

Test

Bash

Ksh

Definition

-A FILE   Supported Supported Only files exist
-B FILE Supported Supported Supported The file exists. It is a block file similar to a disk device under/dev.
-C FILE Supported Supported Supported The file exists and is a character file similar to the TTY device in/dev.
-D FILE Supported Supported Supported The file exists and is a standard directory.
-E FILE Supported Supported Supported Only files exist
-F FILE Supported Supported Supported File exists and is a standard file similar to a flat file
-G FILE Supported Supported Supported The file exists and is a set-group-ID. This is a file license that changes the valid group of users whose files are executable.
-G FILE Supported Supported Supported File exists, and its group ownership is the user's valid group ID
-H FILE Supported Supported Supported The file exists, and it is a symbolic link. It is the same as-L.
-K FILE Supported Supported Supported File exists, and it is set to stick bits. This means that only the file owner or directory owner can delete the file.
-L STRING Supported     Compare the string length with the numeric values similar to/usr/bin/test-l STRING-gt 5 & echo.
-L FILE Supported Supported Supported The file exists, and it is a symbolic link. It is the same as-h.
-N STRING Supported Supported Supported The STRING length is non-zero.
-N FILE   Supported Supported File exists, but it has been modified since the last read.
-O OPTION Supported Supported Supported If the shell OPTION is enabled, the return result is true, for example, set-x.
-O FILE Supported Supported Supported The file exists, and its owner is determined by the valid user ID.
-P FILE Supported Supported Supported The file exists, and it is a named pipe (or FIFO)
-R FILE Supported Supported Supported The file exists and is readable.
-S FILE Supported Supported Supported File exists, and its size is greater than 0 bytes
-S FILE Supported Supported Supported File exists, and it is a socket
-T [FD] Supported Supported Supported Open FD (file descriptor) on a terminal. The default value is stdout.
-U FILE Supported Supported Supported File exists, and set-user-ID is set
-W FILE Supported Supported Supported The file exists and can be written.
-X FILE Supported Supported Supported The file exists and can be executed
-Z STRING Supported Supported Supported The STRING length is 0.

 

2. Special Parameters

Special parameters in shell are shell internal variables, which must be prefixed with $.

For example, echo $? Is to get the shell internal variable "? "Value.

 

Content variables:

Parameters

Definition

* Starts from 1 and completes the list of all location variables. If it is enclosed by double quotation marks, it becomes the word with the first character split of the IFS (internal file separator) Value
@ Starts from 1 and completes the list of all location variables. If it is enclosed by double quotation marks, each location parameter is changed to a single word.
# Number of positional parameters in decimal format
? The return code of the last or most recent foreground task. if the task is killed by a signal, the return value is 128 plus the signal value. For example, if the standard kill signal value is 15, the return value is 143.
- All Flags sent to shell, or flags provided by the set command
$ Shell process ID. If it is in the sub-shell, it is extended to the Current shell value, not the sub-shell value.
! Process ID of the command recently executed in the background
_ Extended to the last parameter of the previous command
0 Expanded to shell or shell script name
1... 9 Location parameters provided to shell, function, or script. values greater than 9 can be accessed using $ {number }.

How does a shell script Replace the values in the script file with parameters input from the command line?

Hello!

You can use the read command to read the entered value.
Then use the $ symbol to reference it.

For example:
Echo-n "enter a character :"
Read Char

Echo $ Char

If you need to overwrite the variable, directly convert the variable =$ Char

Hope to help you!

What does $ # In a shell script mean?

During shell script execution, the first string is processed as the Script Name (or program name), and the second and third strings are processed... Are processed as parameters. Shell assigns these parameters to variable 1 2 3 by default... So what you see when echo $1 $2 is the value of the parameter variable.
To answer the question $ #: Here $ # is the number of input parameters.
There are also some useful algorithms: $ @ represents all input parameters; $ * same as above

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.