我在linux bash中有一个SQLCMD,它可以得到一个SELECT的结果,如何遍历每一行,并能够挑选出特定的列,以便使用它们在
我现在doa 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
上面的方法在导出到外部csav文件时有效,但我需要将sql export放入变量中并在上面循环——所以没有什么外部的想法?
最佳答案
类似于:
sqlcmd ... | while IFS="," read -r d1 d2 d3 d4 d5 d6
do
:# stuff
done
或者:
while IFS="," read -r d1 d2 d3 d4 d5 d6
do
:# stuff
done < <(sqlcmd ...)