Shell scripting-handling input and output

Source: Internet
Author: User
Tags stdin

One, read user input (1) The basic Read command accepts input from a standard input or file descriptor and assigns the received data to a variable.
1 Cat read. SH 2 #!/bin/bash3# Test the Read command4echo"   "5read name6echo"Hello $name "
The-p option of the Read command allows you to specify the prompt directly on the read command line, as the previous example can be written as
"" name
Read can receive multiple user input values and assign values to multiple variables, and if the variable table is exhausted before the data, the remaining data is assigned to the last variable
"Lastecho"Hello $first. $last" 
If you do not specify a variable on the read command line, the Read command will put the data entered by the user into the special environment variable reply.    (2) Read time-out when the Read command is used, if the user does not have input, it waits all the time; Use the-t option to specify the timeout in seconds. After the read timeout, the Read command returns a status exit code other than 0. Such as:
5 "  " name
You can use the-N option to specify the number of characters to read, and the read command will automatically exit after reading the corresponding character without a carriage return. (3) Hidden mode read needs to read user input, but do not want the input displayed on the screen, you can use the-s option.
5 "  "passwd
Ii. input and output redirection (1) standard file descriptor The Linux system treats each object as a file, using a file descriptor to identify each file object. The file descriptor is a non-negative number that uniquely identifies the file opened in the reply. Each procedure can have a maximum of 9 file descriptors.        The bash Shell retains 3 file descriptors 0, 1, and 2.    0-stdin-standard input 1-stdout-standard output 2-stderr-standard error General Command the standard input is the keyboard, standard output and standard error output is the screen. Use < REDIRECT standard input, use > redirect standard output. (2) redirect error when using redirection, if an error occurs, such as
ls -l badfile > testls: unreachable badfile: No file or directory
Error prompt output to screen, test file is created, but empty. If you want to output the error prompt to a file, you need to precede the redirection symbol > with the standard error descriptor 2
ls 2>catls: Unable to access badfile: No file or directory
If you need to redirect both standard output and standard errors to a file, separate processing is required, such as:
ls 2 1> Test
This redirects the error prompt and the standard output to a different file. The bash Shell provides a special redirect &> can redirect the output of stderr and stdout to the same file. (3) Redirect output in script temporary re-training: If you need to convert stdout messages to stderr output, you can add <&2 after the output line
Echo " This is a error message " >&2
The output of this line of command is then displayed in any location pointed to by the stderr file descriptor, not stdour. Permanent redirection: You can tell the shell script to redirect a specific file descriptor during execution by using the EXEC command.
1 #!/bin/bash2#redirecting alloutput31>testfile  4echo"This isa test of redirecting Alloutput"
(4) Redirecting the input EXEC command in the script allows stdin to be redirected to a file, such as the Exec 1< file command, which tells the shell to get input from file instead of stdin.
1#!/bin/Bash2#redirectingfileinput3Exec0<testfile4Count=15  whileRead line; Do6     Echo "Line # $count: $line"7count=$[$count +1 ]8  Done
When the read command is run, the user is no longer required to enter through the keyboard, but instead reads the data directly from the Testfile file.

Shell scripting-handling input and output

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.