Learn Shell (2) Output and others together

Source: Internet
Author: User

Learn Shell (2) Output and others together
Echo and printf I/0 redirection: echo [-ne] [String] or echo [-- help] [-- version] supplementary instructions: echo sends the input string to the standard output. The output strings are separated by blank characters, and the line number is added at the end. Parameter number:-n do not wrap the line at the end-e. If the string contains the following characters, it will be specially processed and will not be used as a general text output: \ a sends a warning; \ B Delete the previous character; \ c does not end with a line break; \ f returns a line break but the cursor remains at the original position; \ n returns a line break and moves the cursor to the beginning of the line; \ r move the cursor to the beginning of the line without line breaks; \ t Insert the tab; \ v is the same as \ f; \ insert \ character; \ nnn insert nnn (octal) the ASCII character represents.-help indicates help.-version indicates that version information cannot be memorized. We cannot remember so many parameters. The best way is to request help, man echo-help can no longer be used by Baidu. The specific function of echo is to enter information. If a prompt is displayed, it prompts the user where to enter the information. This is more suitable for adding the \ a parameter, so as to let the user know what to do, but it is better to have less interaction with more scripts. \ N line feed, I want to output the information for the sake of beauty. Or prompt the user information to which the error occurred before or after. Printfr wt Description: This COPY Copies the functions of the printf () Library Program in the C library, so it can basically be used. Unlike echo, it does not provide line feed operations. % S is used for string output such as ------------------------------ [root @ localhost test] # cat printfprintf "hello 123 '% s, % S'" Love 51cto [root @ localhost test] #. /printfhello 123 'love, 51cto '[root @ localhost test] # ------------------------------------ does not provide line breaks, so the output information is connected together. The role of this % s is that it is not bad to define multiple texts in the same format, which saves a lot of code and a lot of effort. ------------------------------- [Root @ localhost test] # cat printfi = '% s, % s! 'Printf "$ I" ni hao zhongguoprintf "$ I" ni hao cwteaprintf "$ I" love you [root @ localhost test] #./printfni, hao! Zhongguo ,! Ni, hao! Cwtea ,! Love, you! [Root @ localhost test] # --------------------------------------- Basic I/O redirection concept: the program should have the data source and destination, and report the problem, which is referred to as standard input and standard output, standard Error output and input/output can be any file terminal hardware. in Linux, everything is a file. For example: ----------------------- [root @ localhost test] # cat000034 # manually input 11234 # cat output [root @ localhost test] # ------------------- redirection symbol: <>>> <is used to retrieve the following files and perform operations before <. Example: ----------------------------------- [root @ localhost test] # cat ts #! /Bin/shhello 51CTO [root @ localhost test] # tr-d "hello" <ts #! /Bin/s 51CTO [root @ localhost test] # -----------------------------------------> is used to output the preceding content to the following files. If the destination does not exist, create a file, if the existing data already exists, replace the original data. For example, ----------------------------- [root @ localhost test] # cat ts #! /Bin/shhello 51CTO [root @ localhost test] # cat ts> 123 [root @ localhost test] # ls123 printf ts [root @ localhost test] # cat 123 #! /Bin/shhello 51CTO [root @ localhost test] # -------------------------------------> Add a file to the end, which is very effective for the configuration file. For example: ---------------------------------- [root @ localhost test] # tr-d "hello" <ts> 123 [root @ localhost test] # ls123 printf ts [root @ localhost test] # cat 123 #! /Bin/shhello 51CTO #! /Bin/s 51CTO [root @ localhost test] # ----------------------------------------: This adds an excessive line, first, take the value in ts and execute tr-d "hello" and output it to the end of 123. Because it is a one-way operation, the 123 and ts files will not be cat. Tr command. Check tr-help or Baidu. The role of tr is to change files, especially configuration files. Note that when there are pipelines, we should reflect data efficiency and try to hand over as little data as possible to the later pipelines. Tezhu files:/dev/null and/dev/tty/dev/null are common, especially in task planning. The/dev/null bucket writes data to this file. The system considers that the data has been written and discards the data. /Dev/tty: Open this file, and the system redirects to another terminal to combine with the program. /Dev/null is widely used. For example, if a task is scheduled to execute a program every five minutes, too much information is generated, which is not required. In this case, you can directly write it into the bucket. /Dev/tty this person thinks it is good in terms of security. When A wants to enter A password, he can switch to another terminal, ask another terminal to enter the password, and then confirm it and return it back. Original Shell. --------------------------------- [root @ localhost test] # cat ttyecho "Input Pass1: "stty-echo # disable the echoread pass </dev/tty # connect the response data from the/dev/tty new terminal echo pass2" Input Enter "read pass2 </dev/ttystty echo [root @ localhost test] #. /ttyInput Pass1: pass2 Input Enter [root @ localhost test] # ----------------------------------- the Shell must be read for each login. profile file. The later version is in the root directory. bash_profile -------------------------- [root @ loca Lhost ~] # Cat. bash_profile #. bash_profile # Get the aliases and functionsif [-f ~ /. Bashrc]; then .~ /. Bashrcfi # User specific environment and startup programs PATH = $ PATH: $ HOME/bin # export PATHunset USERNAME [root @ localhost ~] # --------------------------- If you want to define a shell to find your own directory, or you can add # In the first line at the beginning #! /Add your directory --------------------------- PATH =:/bin first find this directory PATH =/bin: finally find this directory ------------------------ so the location example is: parameter. Simple execution tracking: set-x set + x is very useful, and a test is also very powerful, but I like set-x, after each line of the program is executed, a prompt is displayed for Shell operations and the operations that have been completed. -------------------------------- [Root @ localhost test] #. /tty ++ echo 'input Pass1: 'input Pass1: ++ stty-echo ++ read pass ++ echo pass2 'input enter' pass2 Input Enter ++ read pass2 ++ stty echo [root @ localhost test] # -------------------------------- internationalization and Localization: this is so noble that I did not take any notes.

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.