Operation and maintenance of Linux system shell script series
1. How to pass SQL statements in shell scripts
Sometimes it is necessary to write Linux shell scripts for operation and maintenance. Recently, there is a need to process several fixed SQL statements. The initial idea was to use string to array, but after a long time, I can’t pass a complete sentence. SQL statements;
finally found a string of switch array using the default shell in Linux space character of the shell is divided, in which case we need to change the default delimiters, use custom delimiters will be able to solve the problem;
a thousand words or Take a look at the script code:
[devops@VM_3_101_centos ~]$ more a.sh
#!/bin/sh
function aa()
{
echo "aa"=="$1"
}
function main()
{
str="select * from aa;select * from bb"
#保存旧的分隔符
OLD_IFS="$IFS"
IFS=";"
array=($str)
# 将IFS恢复成原来的
IFS="$OLD_IFS"
for i in "${!array[@]}"
do
echo "${array[i]}"
aa "${array[i]}"
done
}
main
[devops@VM_3_101_centos ~]$ sh a.sh
select * from aa
aa==select * from aa
select * from bb
aa==select * from bb
[devops@VM_3_101_centos ~]$