#!/bin/sh file="/var/www/tutorialspoint/unix/test.sh" if [ -r $file ] then echo"File has read access" else echo"File does not have read access" fi if [ -w $file ] then echo"File has write permission" else echo"File does not have write permission" fi if [ -x $file ] then echo"File has execute permission" else echo"File does not have execute permission" fi if [ -f $file ] then echo"File is an ordinary file" else echo"This is sepcial file" fi if [ -d $file ] then echo"File is a directory" else echo"This is not a directory" fi if [ -s $file ] then echo"File size is zero" else echo"File size is not zero" fi if [ -e $file ] then echo"File exists" else echo"File does not exist" fi
String
1 2 3 4 5 6 7 8 9 10 11
# 1. get string length string="abcd" echo${#string}#输出 4
# 2. get substring string="alibaba is a great company" echo${string:1:4}#输出liba
# 3. search substring string="alibaba is a great company" echo `expr index "$string" is`
Array
Define array
1 2 3 4 5
array_name=(value0 value1 value2 value3) # or array_name[0]=value0 array_name[1]=value1 array_name[2]=value2
Read array
1 2 3 4 5
${arrray_name[index]}
# get all elements in the array ${array_name[*]} ${array_name[@]}
Get array length
1 2 3 4 5 6
# get the # of elements length=${#array_name[@]} # or length=${#array_name[*]} # get the length of single element length_n=${#array_name[n]}
Condition
If else
1 2 3 4
if [ `expression` ] then # Statement(s) to be executed if expression is true fi
If .. elseif … fi
1 2 3 4 5 6
if [ expression ] then Statement(s) to be executed if expression is true else Statement(s) to be executed if expression is not true fi
If … elseif … fi
1 2 3 4 5 6 7 8 9 10 11 12
if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi
In one line
1 2
# test is used too check the condition is true or not, silimar to ([]) iftest $[2*3] -eq $[1+5]; thenecho'The two numbers are equal!'; fi;
#!/bin/bash option="${1}" case${option}in -f) FILE="${2}" echo"File name is $FILE" ;; -d) DIR="${2}" echo"Dir name is $DIR" ;; *) echo"`basename ${0}`:usage: [-f file] | [-d directory]" exit 1 # Command to come out of the program with status 1 ;; esac
Result:
1 2 3 4 5 6 7 8 9
$./test.sh test.sh: usage: [ -f filename ] | [ -d directory ] $ ./test.sh -f index.htm $ vi test.sh $ ./test.sh -f index.htm File name is index.htm $ ./test.sh -d unix Dir name is unix $
For loop
1 2 3 4 5 6 7
for 变量 in 列表 do command1 command2 ... commandN done
While loop
1 2 3 4
whilecommand do Statement(s) to be executed ifcommand is true done
Until loop
1 2 3 4
until command do Statement(s) to be executed until command is true done
Function
1 2 3 4 5 6
function_name () { list of commands $1# the first param $2# the second param [ return value ] }
unzip <file>.zip -d <exdir-path> # extract files into exdir, (default: current dir if not specified -d.) -l # list files -P # "extract passwd" -t # test compressed archive data -v # list verbosely / show version info -o # overwrite WITHOUT prompting -n # never overwrite existing files
Set envrionment
Set command is used to set and unset certain flags or settings within the sell environment.
1
set [option]
-a: It is used to mark variables that are modified or created for export.
-b: It is used to notify of job termination immediately.
-e: It is used to exit immediately if a command exits with a non-zero status.
-f: It is used to disable the file name generation (globbing).
-h: It is used to save the location of commands where they looked up.
-k: It is used to place all assignment arguments in the environment variable of a command, except those that precede the command name.
-m: It is used to enable Job control.
-n: It is used to read commands.
-o: It is used for option-name.
-p: It is used to disable the processing of the ‘$ENV’ file and import shell functions. It is turned on whenever the real and effective user ids do not match. Turning off this option may cause the working uid and gid to be set as the authorized uid and gid.
-t: It is used to exit from the command after executing one command.
-u: It is used to treat unset variables as an error when substituting.
-v: It is used to print shell input lines.
-x: It is used to print commands and their arguments in a sequential way (as they are executed).
-B: It is used to perform brace expansion by the Shell.
-C: It is used to disallow existing regular files to be overwritten by redirection of output.
-E: It is used if the ERR trap is inherited by the shell functions.
-H: It is used to enable style history substitution. By default, it is on when the shell is interactive.
-P: It is used if we do not want to follow symbolic links when executing commands.
-T: If this flag is set, the DEBUG trap is inherited by the shell functions.
Compress
File info
Check the size of current directories:
1 2 3 4 5 6 7 8 9 10 11
# output all file and directories in the current dir, and sort du -h -d 1 | sort -h
# show the size of current subfiles du -h -d 1
# show the total size of current dir du -sh
# show the size of all subdirectoies (recursive to the max depth) du -h