Shell Scripting Tutorials

Source: Internet
Author: User
Tags bit set

Http://www.runoob.com/linux/linux-shell.html

Shell Pass Parameters

In addition, there are several special characters for handling parameters:

parameter Handling Description
$# The number of arguments passed to the script
$* Displays all parameters passed to the script in a single string
$$ The current process ID number for the script to run
$! ID number of the last process running in the background
[Email protected] Same as $*, but quoted when used, and returns each parameter in quotation marks.
$- Displays the current options used by the shell, the same as the SET command function.
$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.
File Test Operators

File test operators are used to detect various properties of Unix files.

Attribute detection is described as follows:

operator Description Example
-B File Detects if the file is a block device file, and returns True if it is. [-B $file] returns FALSE.
-C file Detects if the file is a character device file, and returns True if it is. [-B $file] returns FALSE.
-D File Detects if the file is a directory, and returns True if it is. [-D $file] returns false.
-F File Detects if the file is a normal file (neither a directory nor a device file), and returns True if it is. [-F $file] returns TRUE.
-G file Detects if the file has a SGID bit set, and returns True if it is. [-G $file] returns false.
-K File Detects if the file has a sticky bit set (Sticky bit), and returns True if it is. [-K $file] returns false.
-P File Detects if the file is a named pipe, and returns True if it is. [-P $file] returns false.
-U file Detects if the file has a SUID bit set, and returns True if it is. [-U $file] returns false.
-R File Detects if the file is readable and returns true if it is. [-R $file] returns TRUE.
-W File Detects if the file is writable and returns true if it is. [-W $file] returns TRUE.
-X File Detects if the file can be executed and, if so, returns True. [-X $file] returns TRUE.
-S file Detects whether the file is empty (the file size is greater than 0) and does not return true for null. [-S $file] returns TRUE.
-E File Detects whether the file (including the directory) exists and, if so, returns True. [-e $file] returns TRUE.

Shell functions

The Linux shell can be user-defined functions and can be called casually in shell scripts.

The functions in the shell are defined in the following format:

[function][()]{ action;  [returnint;] }

Description

    • 1, can be with function fun () definition, you can also directly fun () definition, without any parameters.
    • 2, the parameter returns, can display add: return returns, if not added, will run the result as the last command, as the return value. return followed by value N (0-255)
parameter Handling Description
$# The number of arguments passed to the script
$* Displays all parameters passed to the script in a single string
$$ The current process ID number for the script to run
$! ID number of the last process running in the background
[Email protected] Same as $*, but quoted when used, and returns each parameter in quotation marks.
$- Displays the current options used by the shell, the same as the SET command function.
$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.
Shell Input/Output redirection

Most UNIX system commands accept input from your terminal and send the resulting output back to?? to your terminal. A command usually reads the input from a place called the standard input, which, by default, happens to be your terminal. Similarly, a command usually writes its output to standard output, which is also your terminal by default.

The list of REDIRECT commands is as follows:

Command Description
Command > File Redirects the output to file.
Command < file Redirects the input to file.
Command >> file Redirects the output to file in an append manner.
n > File redirect files with file descriptor N to file.
n >> File Files with file descriptor n are redirected to file in an append manner.
N >& m Merges the output file m and N.
N <& m Merges the input file m and N.
<< tag Enter the contents of the tag between tag and end tag tags as input.

Note that the file descriptor 0 is typically standard input (STDIN), 1 is the standard output (STDOUT), and 2 is the standard error output (STDERR).

Here Document

Here Document is a special redirection in the shell used to redirect input to an interactive shell script or program.

Its basic form is as follows:

<< delimiter    documentdelimiter

Its role is to pass the contents (document) between the two delimiter as input to the command.

Attention:

  • The end of the delimiter must be shelf write, the front can not have any characters, the back can not have any characters, including spaces and tab indentation.
  • The space before and after the beginning of the delimiter is ignored.

Shell Scripting Tutorials

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.