我正在使用 bash 脚本处理文件$ processh.sh file
。输入file
包含有关要处理的文件的信息
# id path type
id1 filename1 csv
id2 filename2
id3 filename3 json
...
process.sh
是某样东西吗
#!/bin/bash
parse(x) {
# get idX filenameX typeX
# check if filenameX exists on disk
# if typeX is missing, set to csv
}
a=`grep -vE '^(\s*$|#)' $1` # remove comment and empty lines
echo $a | while IFS= read -r l; do
[id, filename, type]=parse(l)
process filenameX ...
done
如何按照上面代码中的注释进行健全性检查?
答案1
我向您建议下一个解决方案:
#!/bin/bash
parse() {
# get idX filenameX typeX
id=$1
filename=$2
typo=$3
# if typeX is missing, set to csv
[ -z "$typo" ] && typo="csv"
# check if filenameX exists on disk
[ -f "$filename.$typo" ] && echo "Filename $filename exists"
}
while IFS= read -r line
do
parse_line=`echo $line | grep -vE '^(\s*$|#)'`
[ ! -z "$parse_line" ] && parse $parse_line
done < "$1"