Linux Shell Script Programming

Source: Internet
Author: User

The shell script shell is a non-type interpreted language, which is defined when assigning a value to a variable.
#!/bin/bash  //环境说明- chmod o+x 文件名  //命令行运行
  • /bin/bash file name//Ibid. function
  • $ variable name//reference variable
  • $//The name of the current program (previously defined by the system)
  • $n//Nth parameter
  • $*//All parameters
  • $PWD//directory in which you are currently located
  • -F//file is present
  • -d//directory is present
  • -S//Check whether the file is empty (indicates that it contains data when it is successful; usually used when deleting files)
  • -nt;-ot//Compare a file and another file for the old and new
  • -x;-w;//execute Permissions and Write permissions
  • -eq (equals)-ne (not equal to)-lt (less than)-GT (greater than)-le (less than equals)-O (or, unilaterally established)-a (with, manifold)-Z (empty string)
  • (()) [[]]//double parenthesis denotes a mathematical expression, both brackets denote advanced string processing functions//commonly used for arithmetic judgments or calculations
  • If Start fi End if (); then (if statement only executes a command that returns a return value of (0) and skips the error command) (you can use Boolean logic to execute if [] | | | [];if [] && [])
  • Elif
  • Case command: Eg:
    Case Var in pattern1 | PATTERN2) Command1;;
    PATTERN3) Command2;;
    *) default command3;;
    Esac

  • Spaces at the beginning and end of parentheses
  • while ();
    Done
  • for Var in list
    Do
    Commands
    Done
  • The until statement is the same while but the opposite is known as 0 stop
  • C-type for loop: eg:
    for ((I=1; i<10; i++)
    Note: The assignment of a variable can contain spaces; the variable is not prefixed with the dollar sign; expr is not included in the iteration process
  • If and while after space
  • The contents of the inverted quotation mark are executed as commands.
  • Date +%y%m%d//date format output by month date
  • The WC command counts the text in the data, generating three values by default: Number of lines of text, number of words, number of bytes
  • Exiting the script returns a 0 to 255 integer value that can be used with $? To capture this value to determine if the script is successful (returning 0 generally indicates a successful execution) (you can set the return code of the script exit from 0 to 255 a number; Eg:exit 5; 1 is an unknown error when not set. 2 for misuse of the shell command, 126 for the user without permission to execute the command, 127 for no command found)
  • You can make the script omit the field delimiter (Space, tab, delimited) by temporarily changing the IFS variable in the environment variable eg:ifs = $ '/n '

    Redirect:
  • Input redirection: Eg:command < Inputfile (arrow direction for data flow direction)
  • Built-in input redirection: eg:
    $WC << EOF
    Test.txt
    Eof
    (EOF is data marker, must be the same at beginning and end)
  • The expr command can handle the equation: eg
    $expr 1+5
    You can also use $[1 + 5]
  • The BC command can call Bash's calculator to enter quit when you need to leave (scale sets the size of the decimal place and you can use the BC command in the script eg:

variable`echo "options;express | bc"`
The variables created in the BC are valid only in the calculator and are not valid in the shell script

Linux Shell Script Programming

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.