一、读取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
do
case $opt in
f) csv_file=$OPTARG;;
o) sql_file=$OPTARG;;
*) echo param error: $opt
;;
esac
done
exec 0<$csv_file
exec 1>$sql_file
IFS=,
while read number name age
do
echo insert into student\(number,name,age\) values\($number,$name,$age\)
done
exit
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
do
case $opt in
S)
for signal in $OPTARG
do
case $signal in
1)signalList=$signalList" SIGHUP";;
2)signalList=$signalList" SIGINT";;
20)signalList=$signalList" SIGTSTP";;
*) echo signal error : $signal
exit;;
esac
done
;;
*) echo signals error $opt
exit;;
esac
done
echo signalList=$signalList;
shift $[ $OPTIND - 1 ]
if [ -z $@ ]
then
echo script name not provided!!
exit
fi
scriptToRun=$@
scriptOutput=$@.out
echo script name:$scriptToRun
echo output file:$scriptOutput
trap "" $signalList
source $scriptToRun > $scriptOutput &
trap -- $signalList
exit
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
...