Basic
- indicate the shell you use in the first line of your script, for example
#!/bin/bash
to run your script without occurring the error
command ××× not found, you canadd the container folder of your script to the
PATHor
use absolute path
while you can still find that you don’t have the privilege to run the script, you can use
chmodcommandchmod u+x your_script
display message
- use
echo, but need to pay attention to the quotation in the sentence echohas many parameters- use
-n=> do not output the trailing newline - …..
- use
- use
man echoto learn more
- use
use variables
environment variables
$VAR=> show the value of VAR\$=> display character$in a string
user variables
var1=10=> no blank space allow$var1=> to use var1
command substitution
use back quote, testing=`data`
use
$(),testing=$(data)shell will run
dataand pip the output totestingpay attention, subshell cannot use the varables created by the caller, for example
datecan not use variableVAR
Redirection
output redirection
redirection output to a file,
command > outputfile(over write if exist already),command >> outputfile(append if exist already)input redirection
command < inputfilewc(word count) => count the data in plain text input内联输入重定向
<<
Pip
command1 | command2, the output ofcommand1will be piped tocommand2as its inputactually these two commands run exactly the same time, not in order
Math Calculation
exprnearly useless, very complicated
$[ operation ]bash shell only support integer calculation, while z shell provides complete floating point operation
use
bcin you scriptbc is built-in shell calculator, allowing floating point operation
combine
bc,|and$(), probably also with<<variable=$(echo "options; expression" | bc)for example
1
2var1=$(echo "scale=4; 3.44 / 5" | bc)
echo The answer is $var1OR with
<<
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
!/bin/bash
var1=10.46
var2=43.67
var3=33.2
var4=71
var5=$(bc << EOF
scale = 4
a1 = ( $var1 * $var2 )
b1 = ( $var3 * $var4 )
a1 + b1
EOF
)
echo The final answer for this mess is $var5
exit script
use
exit numto quit your script with a status numbernumranging from 0 to 255
Use Structured command
use
if-then1
2
3
4if command
then
commands
fidifferent from other languages, if 语句之后的对象是一个等式,这个等式的求值结果为TRUE 或者FALSE,bash shell的if 语句不是这么做的
bash shell will run the command followed the
if, and if the exit status code is 0, which indicates the command runs successfully, then thecommandsafterthenwill be excuteduse
if-then-else1
2
3
4
5
6if command
then
commands
else
commands
fiuse nested
if-elseorelifuse
testcombine
testandif-elseallow you implementif elselike other language1
2
3
4if test condition
then
commands
fitestsupport- numeric comparison
- string comparison
- file comparison
there is also a alternative way to use condition in
if-else[ condition ]=> must keep the blank space1
2
3
4if [ condition ]
then
commands
finumeric comparison
comparison Description n1 -eq n2equal? -gebigger or equal? -gtbigger? -leless or equal? -ltless? -nenot equal? 1
2
3
4
5
6
7
8
9
10
11if [ $value1 -gt 5 ]
then
echo "The test value $value1 is greater than 5"
fi
if [ $value1 -eq $value2 ]
then
echo "The values are equal"
else
echo "The values are different"
fican’t use floating point number
string comparison
comparison Description str2 = str2equal? !=not equal? <less? >bigger -n str1len(str1) != 0? -z str1len(str1) == 0? -nand-zcan be used to check whether a variable contains datawhen using
>and<need to use escape character\>and\<or they will be regarded as redirectionfile comparison
comparison Description -d filefile exist and is a dictionary? -e fileexist? -f fileexist and is a file? -r fileexist and file readable? -s fileexist and is no empty? -w fileexist and writable -x fileexist and excutable -O fileown by current user? -G filefile是否存在并且默认组与当前用户相同 file1 -nt file2file1 is newer than file2? file1 -ot file2file1 is older than file2?
compound condition test
[ condition1 ] && [ condition2 ][ condition2 ] || [ condision2 ]Advance features of
if-elseuse double brackets
[[]]pattern matching
use double brace
(())more operators
++--!~**and more
casesubstitution for long
if-else1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac
for example
case $USER in
rich | barbara)
echo "Welcome, $USER"
echo "Please enjoy your visit";;
testing)
echo "Special testing account";;
jessica)
echo "Do not forget to log off when you're done";;
*)
echo "Sorry, you are not allowed here";;
esac