#!/bin/bash
# Author:undoner
# Copyright (c) undoner
# Test Code
# -----------------------------------------------------------------
#标准输入输出
echo "What's your name?"
Read person
echo "Hello,${person}"
printf "%d%s\n" 1 "ABC"
# -----------------------------------------------------------------
#声明变量: readonly (read-only), unset (delete)
Var= "Undoner"
echo "${var}" #输出 Undoner
echo ${#var} #统计字符数量, Output 7
Echo ${var:1:4} #字符串截取, Output Ndon
echo ' expr index ' $var ' d ' #查找字符, output index 3
# -----------------------------------------------------------------
#声明函数:
Hello () {
echo "Declaring function"
}
Hello
#输出: Declaring a function
# -----------------------------------------------------------------
#条件控制:
Echo ' If Else statement '
if_a=10
If_b=20
if [$if _a = = $if _b]
Then
echo "if_a is equal to If_b"
Else
echo "if_a isn't equal to If_b"
Fi
# -----------------------------------------------------------------
#循环控制:
#break命令允许跳出所有循环 (terminates all loops following the execution).
#continue命令与break命令类似, but only jumps out of the current loop.
echo ' For Loop '
For for_str in ' This is a string '
Do
Echo $for _str
Done
#输出: This is a string
For FILE in $HOME/.bash*
Do
Echo $FILE
Done
#输出: Displays files starting with. Bash in the home directory
Echo ' While Loop '
While_sum=0
While [$while _sum-lt 5]
Do
while_sum= ' Expr $while _sum+1 '
Echo $while _sum
Done
#实现计数 (with newline), 0 1 2 3 4 5
Echo ' Until Loop '
Until_sum=0
Until [! $until _SUM-LT 5]
Do
Echo $until _sum
until_sum= ' expr $until _sum + 1 '
Done
#实现计数 (with newline), 0 1 2 3 4 5
# -----------------------------------------------------------------
Echo ' Case ESAC statement '
Echo ' Input a number between 1 to 2 '
Echo ' Your number is:\c '
Read Anum
Case $aNum in
1) echo ' you select 1 '
;;
2) echo ' You select 2 '
;;
*) echo ' Do not select a number between 1 to 4 '
;;
Esac
# -----------------------------------------------------------------
#声明数组
Array_name= (value0 value1 value2 value3)
Echo ${array_name[@]} # gets all the elements of the array
Echo ${array_name[*]} # gets all the elements of the array
Echo ${array_name[2]} # Gets an array of single elements
echo ${#array_name [@]} # Gets the number of array elements
echo ${#array_name [*]} # Gets the number of array elements
Echo ${#array_name [2]} # Gets the length of an array of individual elements
# -----------------------------------------------------------------
#变量替换: Variable substitution can change its value depending on the state of the variable (whether it is empty, whether it is defined, etc.)
# formDescription
# ${var}The variable's original value
# ${var:-word}If the variable var is empty or has been deleted (unset), then return to word, but do not change the value of var.
# ${var:=word}If the variable var is empty or has been deleted (unset), return to Word and set the value of Var to word.
# ${var:?message} If the variable var is empty or has been deleted (unset), then send message messages to the standard error output, which can be used to detect whether Var can be properly assigned. If this substitution appears in the shell script, the script will stop running.
# ${var:+word}If the variable var is defined, it returns word but does not change the value of var.
# -----------------------------------------------------------------
#算术运算符列表
#运算符DescriptionExample
# +AdditionThe ' expr $a + $b ' result is 30.
# -SubtractionThe ' expr $a-$b ' result is 10.
# *MultiplicationThe ' expr $a \* $b ' result is 200.
# /DivisionThe ' expr $b/$a ' result is 2.
# %Take surplusThe ' expr $b% $a ' result is 0.
# =Assign valueA= $b assigns the value of variable B to a.
# ==Equality is used to compare two numbers, and the same returns true.[$a = = $b] returns FALSE.
# !=Unequal is used to compare two numbers, and returns true if they are different.[$a! = $b] Returns TRUE.
# -----------------------------------------------------------------
#关系运算符列表
# operatorDescriptionExample
#-eqDetects whether two numbers are equal and returns true for equality.[$a-eq $b] returns TRUE.
#-neDetects whether two numbers are equal and returns true if they are not equal.[$a-ne $b] returns TRUE.
#-GTDetects if the number on the left is greater than the right and, if so, returns True.[$a-gt $b] returns false.
#-LTDetects if the number on the left is less than the right and, if so, returns True.[$a-lt $b] returns TRUE.
#-geDetects if the number on the left is large equal to the right, and returns true if it is.[$a-ge $b] returns false.
#-leDetects if the left number is less than or equal to the right, and returns true if it is.[$a-le $b] returns TRUE.
# -----------------------------------------------------------------
#布尔运算符列表
# operatorDescriptionExample
# !Non-operation, the expression is true returns False, otherwise true.[! false] returns TRUE.
#-OOr operation, there is an expression of true to return true.[$a-lt 20-o $b-GT 100] returns TRUE.
#-AWith an operation, two expressions are true to return true.[$a-lt 20-a $b-GT 100] returns FALSE.
# -----------------------------------------------------------------
#字符串运算符列表
# operatorDescriptionExample
# =Detects whether two strings are equal and returns true for equality.[$a = $b] returns FALSE.
# !=Detects whether two strings are equal and returns true if they are not equal.[$a! = $b] Returns TRUE.
#-ZDetects whether the string length is 0 and returns true for 0.[-Z $a] returns false.
#-NDetects whether the string length is 0 and does not return true for 0.[-Z $a] returns true.
# strDetects whether the string is empty and does not return true for null.[$a] returns TRUE.
# -----------------------------------------------------------------
#文件测试运算符列表
# operatorDescriptionExample
#-B FileDetects if the file is a block device file, and returns True if it is.[-B $file] returns FALSE.
#-C FileDetects if the file is a character device file, and returns True if it is.[-B $file] returns FALSE.
#-D FileDetects if the file is a directory, and returns True if it is.[-D $file] returns false.
#-F FileDetects if the file is a normal file (not a directory and device file), and returns True if it is.[-F $file] returns TRUE.
#-G fileDetects if the file has a SGID bit set, and returns True if it is.[-G $file] returns false.
#-K FileDetects if the file has a sticky bit set (Sticky bit), and returns True if it is.[-K $file] returns false.
#-P FileDetects if the file is a named pipe, and returns True if it is.[-P $file] returns false.
#-U fileDetects if the file has a SUID bit set, and returns True if it is.[-U $file] returns false.
#-R FileDetects if the file is readable and returns true if it is.[-R $file] returns TRUE.
#-W FileDetects if the file is writable and returns true if it is.[-W $file] returns TRUE.
#-X fileDetects if the file can be executed and, if so, returns True.[-X $file] returns TRUE.
#-S fileDetects whether the file is empty (the file size is greater than 0) and does not return true for null.[-S $file] returns TRUE.
#-E FileDetects whether the file (including the directory) exists and, if so, returns True.[-e $file] returns TRUE.
# -----------------------------------------------------------------
#输入输出重定向, List of all available REDIRECT commands
# commandDescription
# command > FileRedirects the output to file.
# Command < fileRedirects the input to file.
# command >> fileRedirects the output to file in an append manner.
# n > Fileredirect files with file descriptor N to file.
# N >> FileFiles with file descriptor n are redirected to file in an append manner.
# N >& mMerges the output file m and N.
# N <& mMerges the input file m and N.
# << TagEnter the contents of the tag between tag and end tag tags as input.
Other
#/dev/nullOutput results are not displayed
#/dev/null 2>&1Shielding the output of stdout and stderr
# -----------------------------------------------------------------
# WC Command
# command Description
# wc-c FileThe number of bytes of statistics.
# wc-l FileCount the number of rows.
# wc-m FileThe number of statistics characters. This flag cannot be used with the-C flag.
# wc-w FileCount the number of words. A word is defined as a string separated by a blank, a jump, or a newline character.
# wc-l FilePrints the length of the longest line.
# wc-help FileDisplay Help information
# WC--version FileDisplay version Information
# -----------------------------------------------------------------
#特殊变量列表
#变量Meaning
# $File name of the current script
# $nArguments passed to the script or function. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is $ A.
# $#The number of arguments passed to the script or function.
# $*All parameters passed to the script or function.
# [Email protected]All parameters passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below.
# $?The exit state of the last command, or the return value of the function.
# $$The current shell process ID. For Shell scripts, this is the process ID where the scripts are located.
#转义字符 meaning
# \\Back slash
# \aAlarm, Bell
# \bBACKSPACE (delete key)
# \fPage Break (FF), moving the current position to the beginning of the next page
# \ nLine break
# \ REnter
# \ tHorizontal tab (Tab key)
# \vVertical tab
Shell Learning Notes