我正在使用while循环读取。txt文件:
baseFileDir=./Base_Database_Files/
if [ -f "./SqlFiles.txt" ]; then
while IFS= read -r line || [[ -n "$line" ]]
do
sqlFilePath=${baseFileDir}$line
echo "Executing: $sqlFilePath"
sqlcmd -S $server,$port -U $user -P $password -d $database -I -i "$sqlFilePath" < /dev/null
done <./SqlFiles.txt
fi
sqlcmd
与$lin
有问题。下面是输出:
Executing: ./Base_Database_Files/MyScript.sql
': Invalid filename.base_Files/MyScript.sql
但是如果我硬编码./Base_Database_Files/MyScript.sql
行,即
baseFileDir=./Base_Database_Files/
if [ -f "./SqlFiles.txt" ]; then
while IFS= read -r line || [[ -n "$line" ]]
do
sqlFilePath=./Base_Database_Files/MyScript.sql
echo "Executing: $sqlFilePath"
sqlcmd -S $server,$port -U $user -P $password -d $database -I -i "$sqlFilePath" < /dev/null
done <./SqlFiles.txt
fi
似乎正在正确地执行脚本。例如
Executing: ./Base_Database_Files/MyScript.sql
Warning! The maximum key length for a nonclustered index is 1700 bytes. The index 'UIX_ALARM_NOTIFICATION_1' has maximum length of 2004 bytes. For some combination of large values, the insert/update operation will fail.
注意:上面的
Warning!
来自SQL server,描述了脚本被正确执行。
看起来sqlcmd
不喜欢变量$line
格式。
问题:如何使while IFS= read -r line
语句使用的$line
变量可被sqlcmd
工作/可读?
参考@Gordon Davisson和@Barmar发布的评论,我能够通过添加将Dos/Windows CR/LR
转换为Unix LF
格式的sed -i 's/r//g' $SqlFilesList
来解决这个问题:以下是工作片段:
SqlFilesList="./SqlFiles.txt"
# converting the CR/LF to Unix LF Or sqlcmd won't recognize the paths properly due to Windows CR/LR chars like rn.
sed -i 's/r//g' $SqlFilesList
#// Read the file list and concatenate the related Path and execute the .sql files.
if [ -f $SqlFilesList ]; then
while IFS=$'r' read -r line || [[ -n "$line" ]]
do
printf "nExecuting: %sn" "$line"
sqlcmd -S $server,$port -U $user -P $password -d $database -I -i "$line" < /dev/null
done < $SqlFilesList
else
echo "The file $database/$SqlFilesList is missing, which should contain the sequence of *.sql to be executed."
exit 1
fi