Shell Scripting Learning Guide

Source: Internet
Author: User
Tags echo command posix time and date shebang

Entry
    • Awk and Bourne shell are the basis of POSIX shell
    • In addition to the POSIX standard, there are European x/open standards
    • Two types of regular expressions
      • BRE basic Regular Expressions basic grep use
      • ERE Extended Regular Expressions Extensibility Egrep Use
    • Small shell script development, first, directly in the command line test, and then put a series of commands into the script, and set the executable permissions, and finally use the script
$ cat > users       #建立文件,使用cat复制终端的输入    who | wc -l     #程序的内容    ^D              #Ctrl + D表示end-of-line$ chmod +x nusers   #让文件拥有可执行权限$ ./nusers          #执行    6$ cat nusers        #文件内容    who | wc -l
    • "#!" in the first line of the shell script
#! /bin/sh     #告诉系统用哪种Shell解释#! /bin/sh -f  #-f表示执行脚本的参数,后面不能有空格,空格会跟着选项一起传给程序   /bin/sh -f /bin/ls -la#! /bin/awk -f #声明一个awk脚本,参数为-f#! /bin/sh -   #表示没有选项。这是基于安全的考虑,可避免欺诈攻击
    • Shell command-line arguments
      • Options that do not require parameters, you can merge
      • Separate multiple commands in the same line with semicolons
      • Shell basic Commands
        • Built-in commands
        • Shell functions
        • External Command--shell initiates a new process to execute the external command, and after execution, the process of returning to the shell continues execution
      • $ plus number, indicating the parameters of the script
      • If the argument exceeds 9, enclose it in curly braces ${10}
lsls -la
    • Variable
      • Start with a letter or underscore
      • The variable can be null, and the null value is NULL
      • When assigning a variable, there must be no spaces around the equals sign
      • Gets the value of the variable, preceded by $; If the value contains a space, enclose it in quotation marks
#! /bin/sh -var1=1          #=>1var2=aaa        #=>aaavar3="bbb"      #=>bbbvar4="aaa bbb"  #=>aaa bbbvar5 =1         #=>错误2         #=>错误
    • Output
      • Echo
        • Simple output, end with line break
        • -N option, omitting the last line break
        • echo command standard is not uniform, so portability is problematic, only use this command to do the simplest output, do not use complex parameters
      • Printf
        • You must specify a newline character to be displayed.
#! /bin/sh -echo"Hello World!"                 #=>Hello World!printf"Hello World! i = [%d]\n"3  #=>Hello World! i = [3]
    • io redirection
      • > redirect standard output
      • < REDIRECT standard input
      • >> append to file
      • | pipeline
      • /dev/null reads/dev/null and immediately returns EOF
    • Basic Command Lookup
      • Shell finds the command to execute in the environment variable $path
      • $PATH, Empty project with:: Represented, representing the current path
      • starts with: Indicates the current path is first
      • to: End, indicates the last find current path
      • use. Instead of empty items, enhanced readability
      • should not put the current path in the lookup path because of security issues
    • Execute trace
      • Print Statements executed by the Shell
      • SET-X Start Tracking
      • St +x de-tracking
#!/bin/sh- set -xecho  " 111 " set  +xecho   "222"   #输出:  + echo  111  111  + set  +x 222   
    • internationalization and localization
      • internationalization i18n
      • localization l10n
      • locale environment variable
        • LANG does not set the default value when any lc_xxx
        • Lc_all is used to override the value of all other lc_xxx variables
        • lc_collate collation
        • lc_ctype Character Set
        • lc_messages message and response (POSIX only)
        • lc_monetary currency
        • lc_numeric number
        • lc_time time and date
      • The
      • should avoid assigning a value to any of the lc_xxx variables
      • locale-a
        • lists all locale names
      • lc_all=da locale-ck lc_time
        • query Danish time and date format
    • commands involved
      • who commands
        • display who are logged in
      • TR
        • tr is used to convert character conversions from standard input through substitution or deletion
      • stty
      • locale
        • display locale settings
# Replace the "ABC" that appears in the file with "XYZ"Cat file| tr "Abc" "Xyz"> New_file# Use the TR command to "unify" the case of lettersCat file| tr [A-z] [a-z] > New_fileCat file| tr [A-z] [a-z] > New_file# Replace the number 0-9 in the file with A-jCat file| tr [0-9] [a-j] > New_file# Delete the "Snail" characters that appear in file filesCat file| tr-d "Snail"> New_file"Note" Here, the ' S ', ' n ', ' a ', ' I ', and ' l ' characters that appear in the file files will be deleted! Instead of tightly deleting the resulting"Snail" string# Remove the newline ' \ n ' and the tab ' \ T ' characters that appear in file files, and the invisible characters must be represented by the escape characterCat file| tr-d "\n\t"> New_file# Delete "consecutive" repeating letters, leaving only the first oneCat file| tr-s [a-za-z] > New_file# Delete empty linesCat file| tr-s "\ n"> New_file# Delete windows file "Cause" the ' ^m ' characterCat file| tr-d "\ r"> New_fileCat file| tr-s "\ r"  "\ n"> New_file"Note" Here-S is followed by two parameters"\ r"And"\ n", replace the former with the latter# Replace tabs with space \040 \011Cat file| tr-s "\011" "\040"> New_file# Replace the colon ":" in the path variable with the line break "\ n"Echo $PATH| tr-s ":" "\ n"

Shell Scripting Learning Guide

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.