shell concise tutorial

**Read only variable * * readonly var

**Delete variable * * unset var

Variable type:

Local variables are defined in scripts or commands and are only valid in the current shell instance

Environment variable a variable that can be accessed by all programs

Shell variable is a variable specially set by the shell program

Single quotation mark: any character will be the original output

Double quotes can have variables or transfer characters

Splicing variable: just add it directly after it

Get string length ${#str}}

Get substring echo ${str:1:4}

Array name = (value 1, value 2,..., value n)

Read array $(array name [subscript])

Get array length ${#array_name [*]}

multiline comment

:<<EOF

Note Content

EOF

Parameter transfer

First argument $1

Number of parameters$#

Parameter content $* or

​ $@

Parameter calculation

var=expr 2 + 2

Relational operator (test)

-Are eq equal

-Is ne not equal

-gt(-ge) is the left greater than the right

-lt(-le) is the right greater than the left

-o or

-a and

&&Logic and

||Logical or

Character operation (test)

=Are the two characters equal

!= Are the two characters unequal

-Is the z length 0

-n is the length not 0

Is the $character empty

File operation

-b is it a block device

-c is it a character device

-d is it a directory

-f is it an ordinary file

-r is the scale

-w is writable

-x is executable

-Is s not empty (file size 0)

-e whether there is

printf

Same as C language

**Process control**

if condition
then
    command1 
    command2
    ...
    commandN 
fi
if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi
for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done


for (( i=0; i<5; i++));do
	echo "$i"
	done
	
for i in `seq 1 5`;do
	echo "$i"
	done
	
for i in {1..5};do
	echo "three:$i"
	done

for i in `ls /home/qyu`;do
	echo "file$i"
	done
while condition
do
    command
done
until condition
do
    command
done
case value in
 Mode 1)
    command1
    command2
    commandN
    ;;
Mode 2)
    command1
    command2
    commandN
    ;;
esac


while read m;do
case $m in
[0-9]) echo "haha"
;;
[a-z]) echo "uu"
;;
*)
;;
esac

done

Jump out of loop: break command, continue command

function

[ function ] funname [()]

{

    action;

    [return int;]

}

Function parameters

funWithParam(){
    echo "The first parameter is $1 !"
    echo "The second parameter is $2 !"
    echo "The tenth parameter is $10 !"
    echo "The tenth parameter is ${10} !"
    echo "The eleventh parameter is ${11} !"
    echo "The total number of parameters is $# One! "
    echo "Output all parameters as a string $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

func(){
	echo "Function called $1"
	echo "The number of parameters is $#"
	
	s=0
	for i in $*;
	do 
	let s=s+i
	echo "sss:"$s
	done
	return  $s
}
func 5 2 3
echo $?
$$
Shell Per se PID(ProcessID,That is, the current process in which the script runs ID No.)
$!
Shell Last running background Process of PID(The process of the last process running in the background ID number)
$?
The end code (return value) of the last command run is the return value of the last instruction executed (Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error)
$-
display shell Current options used, and set The command functions are the same
$*
List of all parameters. as"$*"use「"」Include the situation in"$1 $2 ... $n"All parameters are output in the form of. This option can have more than 9 parameters.
$@
List of all parameters. as"$@"use「"」Include the situation in"$1" "$2" ... "$n" Output all parameters as.
$@ Follow $*Similar, but can be used as an array
$#
Add to Shell Number of parameters
$0
Shell Own file name
$1~$n
 Add to Shell The parameter values of. $1 Is the first parameter $2 Is the second parameter.
command > fileRedirect output to file.
command < fileRedirect input to file.
command >> fileRedirect the output to file as an append.
n > fileRedirect the file with file descriptor n to file.
n >> fileRedirect the file with file descriptor n to file by appending.
n >& mMerge the output files m and n.
n <& mMerge the input files m and n.
<< tagTake the content between the start tag and the end tag tag as input.

File descriptor 0 is usually standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).

If you want to mask stdout and stderr, you can write as follows:

$ command > /dev/null 2>&1

read parameter

brackets

() equivalent to ` order`

**(()) * * commonly used for arithmetic operation, comparison and numerical calculation

[] is equivalent to test

Keywords: C++ Linux Operation & Maintenance server

Added by partypete on Mon, 17 Jan 2022 19:21:45 +0200