[英]Bash file creation from variable
我正在尝试从名为columnHeaders []的数组创建文件,在数组中,我具有当前的测试值:id,source,EN,EN-GB,French-FR,French-DE
当我运行代码时,我得到
EN
EN.xml
EN-GB
EN-GB.xml
法国-FR
法国FR.xml
法国-DE
.xmlch-DE
注意,FRENCH-DE文件名被转换为.xmlch-DE为什么会这样? 我无法一生搞清楚乳清,只能使文件最终看起来像这样。 这让我疯狂! 谢谢你的帮助。
以下是导致我出现问题的代码片段:
# take all the languages and either find the files or create new files for them. The language options
# should be stored in the columnHeader array in positions 3 - n
# cycle through all the output languages (so exclude "id, source' inputs)
# in my example, numWordsInLine is 6
c=2
while [ $c -lt $numWordsInLine ]; do
OUTPUT_LANG="${columnHeaders[$c]}"
echo "$OUTPUT_LANG"
OUTPUT_FILE="$OUTPUT_LANG".xml
# HERE'S WHERE YOU CAN SEE OUTPUT_FILE IS WRONG FOR FRENCH_DE
echo "$OUTPUT_FILE"
OUTPUT_BAK="$OUTPUT_LANG".bak
TMP_FILE="~tmp.xml"
if [ -f "$OUTPUT_BAK" ]; then
rm "$OUTPUT_BAK"
fi
# make a backup of the original language.xml file in case of program error or interruption
if [ -f "$OUTPUT_FILE" ]; then
mv "$OUTPUT_FILE" "$OUTPUT_BAK"
fi
if [ -f "$TMP_FILE" ]; then
rm "$TMP_FILE"
fi
c=$(expr $c + 1)
done
我敢打赌,您正在使用DOS换行符从文件中读取该行数据。
我还敢打赌,变量的内容是“精细”的,但包括尾随回车符。
尝试printf %q\\\\n "$OUTPUT_FILE"
或echo "$OUTPUT_FILE" | cat -v
echo "$OUTPUT_FILE" | cat -v
看。
然后在文件上使用dos2unix
之类的东西进行转换。
额外的(无关)评论:
也没有理由使用expr
。 ((c++))
会做你想做的事。
您甚至可以将循环本身变成for ((c=2;c < $numWordsInLine; c++)); do
如果您for ((c=2;c < $numWordsInLine; c++)); do
话。
如果$columnHeaders
已被分割成正确的“单词”,则$numWordsInLine
也不必要,因为您可以使用${#columnHeaders}
来获取长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.