如何迭代 bash sqlcmd 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56728643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate over bash sqlcmd output
提问by James_Inger
I have a SQLCMD in linux bash that im getting the results of a SELECT, how can i iterate over each line, and be able to pick out specific columns so that i can use them to run a bash command on
我在 linux bash 中有一个 SQLCMD,我正在获取 SELECT 的结果,我如何遍历每一行,并能够挑选出特定的列,以便我可以使用它们来运行 bash 命令
I currently doa SELECT and output to csv file, which works OK, but i would rather find a way that does not export, as permissions are in play, and i need to run all fully withing the one script
我目前做 SELECT 并输出到 csv 文件,它工作正常,但我宁愿找到一种不导出的方法,因为权限正在发挥作用,我需要使用一个脚本完全运行所有内容
#!/bin/bash
sqlcmd -S DB_string -d DB -U USERNAME -P PASSWORD -Q \
"SET NOCOUNT ON SELECT * FROM dbo.microiopenvpn WHERE mode='create' ORDER BY id" -o "sqloutputCheckCreate.csv" -W -w 1024 -s"," -h-1
line_no=0
file=sqloutputCheckCreate.csv
line_no=$(awk '{x++} END {print x}' $file)
echo $line_no
#rowCount= sqloutputCheckCreate.csv | wc -l
if [ $line_no > 0 ]; then
echo "SQL Has Entries"
while IFS="," read -r d1 d2 d3 d4 d5 d6; do
if [ -e "/etc/openvpn/clients/$d6.ovpn" ]; then
echo "$d6 File exists"
sqlcmd -S DB_string -d DB -U USERNAME -P PASSWORD -Q \ -Q "SET NOCOUNT ON UPDATE dbo.microiopenvpn SET mode='valid' WHERE id='$d1'" < /dev$ echo "Database Updated - Successfully"
else
echo "$d6 does not exist - Action Creation Script of $d6"
./executecreate.sh
fi
done < sqloutputCheckCreate.csv
else
echo "Nothing there"
fi
cp /dev/null sqloutputCheckCreate.csv
the above works when exporting to external csav file, but I need to put sql export into variable and loop over that - so nothing external - ideas ?
以上在导出到外部 csav 文件时有效,但我需要将 sql 导出放入变量并循环 - 所以没有外部 - 想法?
采纳答案by Roadowl
Something like:
就像是:
sqlcmd ... | while IFS="," read -r d1 d2 d3 d4 d5 d6
do
:# stuff
done
or, alternatively:
或者,或者:
while IFS="," read -r d1 d2 d3 d4 d5 d6
do
:# stuff
done < <(sqlcmd ...)