一、读取csv文件中的数据,生成sql语句
1、表定义
create table student
(number int primary key,name char(20) not null,age int null
)
2、输入数据
$ cat < student.csv
20240001,lin,20
20240002,zhangsan,21
20240003,li4,23
20240004,wang5,20
3、shell脚本
#!/usr/bin/bash
csv_file=""
sql_file=""
while getopts f:o: opt
docase $opt inf) csv_file=$OPTARG;;o) sql_file=$OPTARG;;*) echo param error: $opt ;;esac
doneexec 0<$csv_file
exec 1>$sql_file
IFS=,
while read number name age
doecho insert into student\(number,name,age\) values\($number,$name,$age\)
doneexit
4、结果
$ ./test.sh -f student.csv -o student.sql
$ cat < student.sql
insert into student(number,name,age) values(20240001,lin,20)
insert into student(number,name,age) values(20240002,zhangsan,21)
insert into student(number,name,age) values(20240003,li4,23)
insert into student(number,name,age) values(20240004,wang5,20)
二、捕获脚本信号,并将脚本置于后台运行
1、预期效果
格式:
./test.sh -S signals command
在后台执行 command,并忽略信号signals
2、shell脚本
#!/usr/bin/bash
signalList=""
while getopts S: opt
docase $opt inS)for signal in $OPTARGdocase $signal in1)signalList=$signalList" SIGHUP";;2)signalList=$signalList" SIGINT";;20)signalList=$signalList" SIGTSTP";;*) echo signal error : $signalexit;;esacdone;;*) echo signals error $optexit;;esac
doneecho signalList=$signalList;shift $[ $OPTIND - 1 ]if [ -z $@ ]
thenecho script name not provided!!exit
fi
scriptToRun=$@
scriptOutput=$@.outecho script name:$scriptToRun
echo output file:$scriptOutputtrap "" $signalListsource $scriptToRun > $scriptOutput &trap -- $signalListexit
3、结果
$ ./test.sh -S "1 2 20" ./loop.sh
signalList= SIGHUP SIGINT SIGTSTP
script name:./loop.sh
output file:./loop.sh.out$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
...
1 S 1000 1123077 1 0 80 0 - 1723 do_wai pts/4 00:00:00 test.sh
...$ kill -2 1123077$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
...
1 S 1000 1123077 1 0 80 0 - 1723 do_wai pts/4 00:00:00 test.sh
...