如何在shell脚本传递SQL语句

运维Linux系统shell脚本系列

1、如何在如何在shell脚本传递SQL语句


有时候运维需要写Linux的shell脚本,最近有个需求,需要对几个固定的SQL语句进行处理,一开始的思路是使用字符串转数组,但是搞了老半天始终传递不过去一句完整的SQL语句;
最后发现在Linux的shell中字符串转数组使用shell默认的空格符进行分割的,此时需要我们改变默认的分隔符,使用自定义的分隔符就能解决问题了;
千言万语还是上脚本代码看看:

[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 ~]$   

猜你喜欢

转载自blog.csdn.net/qq_31555951/article/details/106725953
今日推荐