table of Contents
Shell structure of three selected program
2. case-esac control structure
Shell four loop structure in the program
Use the 5 functions and arrays
Six shell database to achieve bulk inserts
shell programming
1. Create: Create as with the vi editor: vi hello.sh
2, edit the script file content needs attention: the full path to the first line with "#!" At the beginning, the statement used by the shell, such as:! # / Bin / bash
3, given executable permissions: chmod + x hello.sh execution: ./ hello.sh
shell Keyword
Commonly used keywords as follows:
1. echo
Output to print text to the screen
two conventional echo parameters: -e outputting content identifying characters in translation; -n ignores end wrap
[root@node673 test]# echo "hello\tworld"
hello\tworld
[root@node673 test]# echo -e "hello\tworld"
hello world
[root@node673 test]# echo -e -n "hello\tworld"
hello world[root@node673 test]#
2. exec
Another shell script execution
#!/bin/bash
exec ./hello.sh
3. read
Read standard input
read common parameters :-p add message
#!/bin/bash
read -p "please enter a word: " testvar
echo "you entered $testvar"
Run the script:
[root@node673 test]# ./test_read.sh
please enter a word: test
you entered test
4. expr
Integer variables for arithmetic operations, correlation calculation may be performed string
. ① integer variables for arithmetic operations:
Usage: expr operator expression expression 1 2
Multiplication operator "*" in front of you must use '\' for translation. It must be a space between each of the expressions and operators (and let it be different)
#!/bin/bash
i=10
j=20
k=6
res1=`expr $i + $j + $k`
res2=`expr $i - $j - $k`
res3=`expr $i \* $j \* $k`
res4=`expr $i \* $j / $k`
echo $res1
echo $res2
echo $res3
echo $res4
Run the script:
[root@node673 test]# ./practice_expr.sh
i+j+k=36
i-j-k=-16
i*j*k=1200
# 整除结果只能保存为整数
i*j/k=33
. ② the string arithmetic:
#!/bin/bash
str1="abcdefg"
# 输出字符串长度
l1=`expr length $str1`
# 使用echo输出字符串长度
l11=`echo ${#str1}`
# 截取字符串子串
l2=`expr substr $str1 1 3` #expr截取时,下标是从1计算
# 使用echo截取字符串子串
l22=`echo ${str1:0:3}` #echo截取时,下标是从0计算
# 字符串连接
str2="12345"
str3="${str1}$str2"
echo $l1
echo $l11
echo $l2
echo $l22
echo $str3
Run the script:
[root@node673 test]# ./practice_expr_str.sh
7
7
abc
abc
abcdefg12345
5. let
Only integer execution of related operations, operation results can only hold integers
Usage: let variable name = Variable Variable 1 Operator 2
#!/bin/bash
i=10
j=20
k=6
let res1="$i+$j+$k"
let res2="$i-$j-$k"
let res3="$i*$j*$k"
let res4="$i*$j/$k"
echo "i+j+k=$res1"
echo "i-j-k=$res2"
echo "i*j*k=$res3"
echo "i*j/k=$res4"
Run the script:
[root@node673 test]# ./practice_let.sh
i+j+k=36
i-j-k=-16
i*j*k=1200
# 整除结果只能保存到整数
i*j/k=33
6. test
To compare two values, the more successful return 0, otherwise non 0
Common methods of comparison:
- Integer comparison
- String comparison
- Logical comparison (AND, OR, NOT)
- File Compare
Usage: test value1 -option value2
If successful 0 ($? 0) returns, otherwise it returns a non-zero, commonly used in the judgment operation.
Integer comparison of test methods:
more than the | gt |
Less than | lt |
greater or equal to | give |
Less than or equal | the |
equal | eq |
not equal to | born |
For chestnut:
#!/bin/bash
a=1
b=2
c=2
d=4
echo -e "a:$a\t b:$b\t c:$c\t d:$d"
test $b -gt $a
if [ $? -eq 0 ]
then
echo "b>a"
else
echo "b<a"
fi
test $c -lt $a
if [ $? -eq 0 ]
then
echo "c<a"
else
echo "c>a"
fi
test $d -ge $c
if [ $? -eq 0 ]
then
echo "d>=c"
else
echo "d<c"
fi
test $b -le $c
if [ $? -eq 0 ]
then
echo "b<=c"
else
echo "b>c"
fi
test $a -ne $d
if [ $? -eq 0 ]
then
echo "a!=d"
else
echo "a=d"
fi
Run the script:
[root@node673 test]# ./test_int.sh
a:1 b:2 c:2 d:4
b>a
c>a
d>=c
b<=c
a!=d
string comparison test method:
Test string length | -with |
Test string length is non-zero | -n |
A string equal to a certain | = |
Ranging from a certain string | != |
#!/bin/bash
str1="asd"
str2='asd'
str3="123"
str4=""
echo -e "str1:${str1}\t str2:${str2}\t str3:${str3}\t str4:${str4}"
test -n $str1
if [ $? -eq 0 ]
then
echo "str1 is not empty"
else
echo "str1 is empty"
fi
test -z $str4
if [ $? -eq 0 ]
then
echo "str4 is empty"
else
echo "str4 is not empty"
fi
test $str1=$str2
if [ $? -eq 0 ]
then
echo "str1 = str2"
else
echo "str1 != str2"
fi
test $str1!=$str4
if [ $? -eq 0 ]
then
echo "str1 != str4"
else
echo "str1 = str4"
fi
Run the script:
str1:asd str2:asd str3:123 str4:
str1 is not empty
str4 is empty
str1 = str2
str1 != str4
Logical comparison of test methods:
Logic and | -a |
Logical or | -O |
Logical NOT | ! |
Example:
#!/bin/bash
basepath=$(pwd)
filename=$0
# 判断位置参数个数等于1,且第一个位置参数等于100
test $# -eq 1 -a $1 -eq 100
if [ $? -eq 0 ]
then
echo "位置参数数量为1个且值为100"
else
echo "不满足与运算的输入要求..."
fi
# 判断位置参数个数等于2,或第2个位置参数等于200
test $# -eq 2 -o $2 -gt 100
if [ $? -eq 0 ]
then
echo "位置参数为2个或第二个位置参数大于100"
else
echo "不满足或运算的输入要求..."
fi
Run the script:
[root@node673 test]# ./test_logic.sh 200 200
不满足与运算的输入要求...
位置参数为2个或第二个位置参数大于100
test file comparison method:
File exists and is a regular file | -f |
File is not empty | -s |
File is readable | -w |
File writable | -r |
Executable files | -x |
File is a directory name | -d |
File is a symbolic link | -h |
A reference to a character device file name | -c |
A quick reference file filename | -b |
Example:
#!/bin/bash
base_path=$(pwd)
filename=$0
filename2="./nonexists.txt"
dir_name="./20190507"
test -f $filename
if [ $? -eq 0 ]
then
echo "$filename is a regular file"
else
echo "$filename is not exists or not a regular file"
fi
test -s $filename
if [ $? -eq 0 ]
then
echo "$filename is not empty..."
else
echo "$filename is empty..."
fi
test -r $filename
if [ $? -eq 0 ]
then
echo "$filename is readable"
else
echo "$filename is not readable"
fi
test -w $filename
if [ $? -eq 0 ]
then
echo "$filename is writeable"
else
echo "$filename is not writeable"
fi
test -x $filename
if [ $? -eq 0 ]
then
echo "$filename can be excute"
else
echo "$filename can not be excute"
fi
test -d $dir_name
if [ $? -eq 0 ]
then
echo "$dir_name is a directory..."
else
echo "$dir_name is not a directory"
fi
Run the script:
[root@node673 test]# ./test_file.sh
./test_file.sh is a regular file
./test_file.sh is not empty...
./test_file.sh is readable
./test_file.sh is writeable
./test_file.sh can be excute
./20190507 is a directory...
# 查看./test_file.sh文件的信息
[root@node673 test]# ll ./test_file.sh
-rwxr-xr-x 1 root root 1217 5月 7 06:16 ./test_file.sh
7. “<<”
"<<" can be considered a redirection symbol, after being placed on input redirection command, followed in each row << as a command input through the end of the input end of file that can also be given their own definition delimiter, word delimiter after the end of each line as an input delimiter. End delimiter to freeze writing.
#!/bin/bash
# 使用"<<"操作数据库,连接数据库后,先查看状态,再查询库test下user表的信息。以感叹号"!"作为定界符
/usr/local/mysql5.6/bin/mysql <<!
status
select * from test.user
!
Shell structure of three selected program
Selection structure is a logical structure of the program with a determination, only to meet certain conditions, the program will be executed bodies
1. if-then control structure
If a single branch structure, when the condition is satisfied, then the statement following the execution, decision statement exit condition is not satisfied
if [条件]
then
语句
if
Double-branch structure, when the condition is satisfied, then the statement following the execution, does not satisfy the condition statement follows else
if [条件]
then
语句
else
语句
if
2. case-esac control structure
case statement applies to the application of multiple branches, when the condition that any one of a branch condition, this latter branch statement executed
case 变量名 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
*)
默认执行的命令序列
;;
esac
Example:
#!/bin/bash
read -p "请输入一个字符,按回车确定: " key
case $key in
[0-9])
echo "你输入的是数字: $key"
;;
[a-z]|[A-Z])
echo "你输入的是字符: $key"
;;
*)
echo "你输入的是特殊字符: $key"
;;
esac
Run the script:
[root@node673 test]# ./practice_case.sh
请输入一个字符,按回车确定: a
你输入的是字符: a
Shell four loop structure in the program
1. for
Traversed / list loop structure
# 语法如下
for variable in list
do
statement
done
Seq command may be generated with all integers between one number to another, a number, often used together with the for loop
#!/bin/bash
for i in $(seq 5)
do
echo "$i"
done
Run the script:
[root@node673 test]# sh practice_for.sh
1
2
3
4
5
C-style loop structure
# 语法如下:
for((expr1; expr2; expr3)) # 注意:是双括号
do
statement
done
2. while
While when the condition is true, the statements in the loop body is executed
# 语法如下:
while expression
do
statement
done
while loop structure in two ways:
- while [] structure, this time behind while to add a space, otherwise it will error, such as: while [$ -eq 0?]
- while (()) structure, while the rear without adding a space, such as: while ((i <10))
#!/bin/bash
i=1
while [ $i -le 5 ]
do
echo "$i"
let i++
done
Run the script:
[root@node673 test]# sh practice_while.sh
1
2
3
4
5
3. break、continue
break the current cycle exit, continue out of this cycle
break:
#!/bin/bash
# 当输入字符为x或X时,循环终止。
while [ 1 ]
do
read -p "请输入一个字符,按回车确定:" key
test $key = x -o $key = X
if [ $? -eq 0 ]
then
echo "你输入的是$key,终止循环。"
break
else
echo “$key”
fi
done
Run the script:
[root@node673 test]# sh practice_break.sh
请输入一个字符,按回车确定:a
“a”
请输入一个字符,按回车确定:1
“1”
请输入一个字符,按回车确定:x
你输入的是x,终止循环。
[root@node673 test]#
continue:
#!/bin/bash
# 当输入的字符为x或X时,只跳过本次循环
while [ 1 ]
do
read -p "请输入一个字符,按回车确认:" key
test $key = x -o $key = X
if [ $? -eq 0 ]
then
echo "你输入的是x或X,跳过本次循环。"
continue
else
echo "$key"
fi
done
Run the script:
[root@node673 test]# sh practice_continue.sh
请输入一个字符,按回车确认:a
a
请输入一个字符,按回车确认:x
你输入的是x或X,跳过本次循环。
请输入一个字符,按回车确认:X
你输入的是x或X,跳过本次循环。
请输入一个字符,按回车确认:1
1
4. while read line
It can be read into the row until all rows are read only exit the loop. This practice often used for processing configuration data.
# 语法格式:
cat file.txt | while read line # 此处的cat也可以是产生若干行的命令,如find
do
statement
done
Example: a file read cycle of all the lines of the first field in accordance with the output format and a second field
#!/bin/bash
num=0
cat ./test.txt | while read line
do
let num++
echo -n "第$num行..."
one=`echo ${line} |awk '{print$1}'`
two=`echo ${line} |awk '{print$2}'`
echo "第一个字段为:$one-----第二个字段为:$two"
done
Run the script:
[root@node673 test]# sh practice_while_read_line.sh
第1行...第一个字段为:北京市-----第二个字段为:11
第2行...第一个字段为:天津市-----第二个字段为:12
第3行...第一个字段为:上海市-----第二个字段为:31
第4行...第一个字段为:重庆市-----第二个字段为:50
第5行...第一个字段为:河北省-----第二个字段为:13
Use the 5 functions and arrays
1, linux shell function to a set of commands or statements usable form a block, such as a function block statement.
2, a function of composition:
- Function name: function name, note the name of the function in a script to be unique, otherwise called when function disorders.
- Function body: a set of internal function commands, realize the function of a business.
3, the function defined format:
function 函数名() #function 可以省略 注意()内部不能带任何参数
{
命令1
命令2
...
}
Defined and referenced functions: Example 1
#!/bin/bash
# 函数的定义及引用
function now_date()
{
echo "now date is `date`"
}
now_date
Run the script:
[root@node673 test]# sh practice_function.sh
now date is 2019年 05月 09日 星期四 07:25:13 CST
Example Two: passing arguments to functions
#!/bin/bash
function func_sum()
{
if [ $# -eq 2 ]
then
a1=$1
a2=$2
let sum=$a1+$a2
echo "$a1+$a2=$sum"
else
echo "输入的参数数量不正确..."
fi
}
# 在脚本中调用定义好的函数,可以传入多个位置参数
func_sum 10 20
Run the script:
[root@node673 test]# sh practice_func.sh
10+20=30
Example Three: Calling other methods script file function, a function to import: . Spaces in filenames
#!/bin/bash
# 调用函数的文件位置和文件名称
. ./practice_func.sh
while [ 1 ]
do
read -p "please enter a file: " file
func_md5 $file
done
4, array definition: in programming, in order to facilitate processing, the number of variables of the same type organized in an ordered form, the same set of data elements called an array in sequence.
# 定义一个数组:
数组名=(元素1 元素2 元素3) # 一对括号表示一个数组,数组间元素用空格符合隔开
#如:
myarray=(1 2 3 4 5)
5, an array of common operations
- Read one array element: echo $ {myarray [index value]}} {array name must be enclosed, index values are numbered starting from zero.
- Array element assignment: myarray [index value] = value as specific myarray [5] = 6
- Show all array elements: echo $ {myarray [*]}
- Get array length: echo $ {# myarray [*]}
- Remove array elements: unset myarray [index value]
#!/bin/bash
array=(0 1 2 3 4 5 6)
echo "数组: ${array[*]}"
echo "数组长度为: ${#array[*]}"
# 数组新增值,数组下标从0开始
array[7]=7
echo "数组新增后所有元素为: ${array[*]}"
# 数组删除值
unset array[0]
echo "数组删除数组第一个值后剩余元素: ${array[*]}"
# 数组修改值
array[0]=999
echo "修改后数组第一个值是: ${array[0]}"
# 数组查询值
echo "数组第一个值: ${array[0]}"
Run the script:
[root@node673 test]# sh test_array1.sh
数组: 0 1 2 3 4 5 6
数组长度为: 7
数组新增后所有元素为: 0 1 2 3 4 5 6 7
数组删除数组第一个值后剩余元素: 1 2 3 4 5 6 7
修改后数组第一个值是: 999
数组第一个值: 999
Six shell database to achieve bulk inserts
1, batch execution bulk insert into mysql database
#!/bin/bash
# 定义数据库名
db_name="test"
# 定义表名
table_name="user"
# 定义导入数据的文件
import_file=./data.txt
cat $import_file | while read line
do
u=$`echo $line |awk '{print $1}'` # 获取每一行的第一个字段
p=$`echo $line |awk '{print $2}'` # 获取每一行的第二个字段
echo "$u $p"
/usr/local/mysql5.6/bin/mysql -uroot -proot -e "insert into $db_name.$table_name(username,password) values ('$u','$p')";
done
Seven shell debugging
First, check for syntax errors:
[root@node673 test]# sh -n test.sh
No output, indicating no error, the actual start debugging:
[root@node673 test]# sh -x test.sh
Debugging results are as follows:
+ [ 1 -eq 2 ]
+ echo 1
1
+ [ 2 -eq 2 ]
+ continue
+ [ 3 -eq 2 ]
+ echo 3
3