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
PATH
or
use absolute path
while you can still find that you don’t have the privilege to run the script, you can use
chmod
commandchmod u+x your_script
display message
- use
echo
, but need to pay attention to the quotation in the sentence echo
has many parameters- use
-n
=> do not output the trailing newline - …..
- use
- use
man echo
to 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
data
and pip the output totesting
pay attention, subshell cannot use the varables created by the caller, for example
date
can 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 < inputfile
wc
(word count) => count the data in plain text input内联输入重定向
<<
Pip
command1 | command2
, the output ofcommand1
will be piped tocommand2
as its inputactually these two commands run exactly the same time, not in order
Math Calculation
expr
nearly useless, very complicated
$[ operation ]
bash shell only support integer calculation, while z shell provides complete floating point operation
use
bc
in 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 num
to quit your script with a status numbernum
ranging from 0 to 255
Use Structured command
use
if-then
1
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 thecommands
afterthen
will be excuteduse
if-then-else
1
2
3
4
5
6if command
then
commands
else
commands
fiuse nested
if-else
orelif
use
test
combine
test
andif-else
allow you implementif else
like other language1
2
3
4if test condition
then
commands
fitest
support- 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 n2
equal? -ge
bigger or equal? -gt
bigger? -le
less or equal? -lt
less? -ne
not 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 = str2
equal? !=
not equal? <
less? >
bigger -n str1
len(str1) != 0? -z str1
len(str1) == 0? -n
and-z
can 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 file
file exist and is a dictionary? -e file
exist? -f file
exist and is a file? -r file
exist and file readable? -s file
exist and is no empty? -w file
exist and writable -x file
exist and excutable -O file
own by current user? -G file
file是否存在并且默认组与当前用户相同 file1 -nt file2
file1 is newer than file2? file1 -ot file2
file1 is older than file2?
compound condition test
[ condition1 ] && [ condition2 ]
[ condition2 ] || [ condision2 ]
Advance features of
if-else
use double brackets
[[]]
pattern matching
use double brace
(())
more operators
++
--
!
~
**
and more
case
substitution for long
if-else
1
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