我生成了一个迭代多个 .csv 文件的脚本,将相关文件转换为 UTF-8:
#!/bin/bash
cd /home/user/prod/
charset="text/plain; charset=iso-8859-1"
for file in *.csv; do
if [[ $(file -i "$file") == "$file: $charset" ]]; then
iconv -f ISO-8859-1 -t UTF-8 "$file" > "$file.new";
mv -f "$file.new" "$file";
fi
done
这可行,但我真正想要的是迭代驻留在不同路径中的文件。我尝试从设置一个路径(而不是定义当前目录)开始,但我无法让它工作:
#!/bin/bash
path="/home/user/prod"
charset="text/plain; charset=iso-8859-1"
for file in "$path/*.csv"; do
if [[ $(file -i "$file") == "$file: $charset" ]]; then
iconv -f ISO-8859-1 -t UTF-8 "$file" > "$file.new";
mv -f "$file.new" "$file";
fi
done
通过设置路径来做到这一点的最佳方法是什么?如何处理驻留在不同路径中的文件(相同扩展名)?
您已经接受了@Charles Duffy 的答案,但是(如果我理解得很好)您的问题是关于将文件放在不同的目录因此,如果您需要处理多个目录中的多个 csv 文件,您可以使用以下代码片段:
# array containing the different directories to work with
pathDir=("/foo/bar/dir1" "/buzz/fizz/dir2")
for dir in "${pathDir[@]}" # For each directory
do
for file in "$dir/"*.csv; do # For each csv file of the directory
if [[ $(file -i "$file") == "$file: $charset" ]]; then
iconv -f ISO-8859-1 -t UTF-8 "$file" > "$file.new";
mv -f "$file.new" "$file";
fi
done
done
The pathDir
变量是一个数组,其中包含不同目录的路径。
首先for
循环遍历该数组以获取要检查的所有路径。
第二for
像前面的答案一样循环遍历当前测试目录的文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)