Shell Tutorials < Introductory articles >

Source: Internet
Author: User
Tags echo command

Because my normal working environment is Linux, so inevitably use command line mode and shell script, and some command line every day to lose many times, such as SSH login, so simply put the usual commands are written script files, so deliberately opened a shell script classification, Used to record the process of learning the shell for review.

    • First we start with a simple script that opens the door to shell learning, assuming you want to know how many people are logged in to the current system, and you can use the WHO command to query:
1 W.H.O. 2 George        pts/2        :3 Betsy         pts/3         :
    • But if the amount of information on a multiuser system is very large and cumbersome in statistics, we can use the channel and WC program to count the number of lines (line), Word and character (character), in this case count the number of logins so query the number of rows.
1 Who | WC-L  2         2
    • Note: | (piping) symbols can be used to establish a pipeline between two programs : The WHO output becomes the input of the WC
    • You can then turn it into a small script file, write the above command into a generic text file, and then give execution permission to execute, which also shows the development cycle of the script file, you can first test development on the command line, write the file without error.
1 VI wc.sh 2 #进入文本编辑模式, who will be ordered | WC-L Write, save exit 3chmod +x wc.sh #赋予该脚本文件执行权限 4./  wc.sh #执行脚本 5 $     2  #获得输出结果
    • In the first line of many scripts there are two characters, the meaning of which is to tell the Linux kernel which shell should be used to execute the current script, most of the current scripts are using #!/bin/sh , and #! #!/bin/ CSH (is the C shell interpreter)
1 Cat wc.sh 2 3 #!/bin/sh4 who | Wc-l
    • A semicolon, which can be used to split multiple commands in the same line, the shell executes sequentially, and if the & symbol is used, the current command is executed in the background, that is, you do not have to wait until the current command is complete to continue with the next command
    • Variable: The name of the shell variable begins with a letter or underscore symbol, followed by any length of letters, numbers, and underscores, and the variable length is not limited; the variable is assigned in the following way: Write the variable name, followed by the = character, and finally the given value, with no spaces in the middle , you need to use the $ character before the name of the variable when you want to take the value of the variable.
1 value=this_is_a_long_value #当赋予的值中存在空格时, enclose the quotation marks.  2 Echo $value 3 $ this_is_a_long_value
    • Output commands: Most people may be accustomed to using the echo command for simple output, but to know that Echo is different from version, porting is a potentially risky issue, and it is relatively recommended to use the printf command, The printf command almost completely printfcopy C, so the formatting characters in C also apply.
1  "hello world.\n"  # printf does not implicitly add a line break like echo, so you need to add a newline character manually \ n 2 printf "The first program always prints%s%s.\n" Hello World
    • redirection: To <   Change standard input to  >   Change standard output
1 Program <  File can modify the standard input of program to file 2 eg. tr-d ' \ R ' <  test.file #这条命令会将test A carriage return in a. file file Delete 34program>   file the standard output of program can be modified to file5eg. tr-d ' \ R ' < test.file >   result.file #这条命令会先将test. Enter in file is deleted and the processed data is output to Result.file, but the data in Test.file does not change 67 #注意: > The redirect will automatically create a new one if the destination file does not exist, overwriting the existing data if the file already exists, and if you want to append the data, use the >> redirect
    • Create a channel with a | symbol
1 program1 | program2 #program2可以将program1的标准输出作为自己的输入

Special files /dev/null and /dev/tty

1 #linux系统提供了两个对Shell编程特别有用的特殊文件2#/dev/Null file3 #该文件一般被称为位桶. The data transferred to this file is discarded by the system, which means that when the program writes the data to a secondary file, the data has been successfully written in the results, but nothing has actually been done .4 #例如测试一个文件是否包含某个模式5  if grep pattern myfile >/dev/null 6  Then7 printf "find."8 Else9 printf "don ' t find"Ten fi One  A#/dev/TTY - #当程序打开此文件时, Linux automatically redirects it to a terminal and then integrates with the program. - eg.printf "Enter new  passed: " #提示输入新密码 the    Stty- echo #关闭自动打印输入字符的功能 -    Read Pass </dev/ TTY #从当前终端读取密码 -  printf "Enter again:" #再次输入密码 -    Read Pass2 </dev/TTY  +    Stty Echo#打开自动打印输入字符的功能
    • The Stty command welcomes the various settings of the control terminal, the-echo option is to turn off the ability to automatically print each input character, and the echo option turns on the function
    • In a relatively complex shell script, you can use set -X to turn on code tracing, use set +x to turn off the function, and then open the code for that function. Any executed will add a + sign at the beginning of the line
1 #!/bin/sh 2  Set - x #开启代码追踪功能3 value=1 4  if ["$value" x = "0  "x];then #此处在值后面加一个x是规避值为空的情况5     printf "value  is 0. \ n" 6 Else7     printf "value isn ' t 0. \ n" 8 fi9  set +x#关闭代码追踪功能
    The
    • outputs the result:
1 + value=1  2  if ["$value" x = "0" x];then  3 is  0. \ n " #由于value的值不为0, so did not execute this sentence, so the header does not have the + symbol 4Else 5  0. \ n "  6 + fi  7  set +x

Shell Tutorial < introductory article;

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.