好的,所以我有一个脚本,我正在使用它来解析具有不同列和值的各种日志文件。我一直试图使用getopts来允许我的脚本对一个目录中的文件运行解析,并将输出保存到另一个目录。本质上,命令是(应该是):使用getopts的Bash脚本 - 使用目录作为参数
./script.sh -i /absolute/input/dir/ -o /absolute/output/dir/
目前,如果脚本是在目录中的日志文件,它会
检查第4和每个文件的最后4, 基于这些结果以某种方式解析文件, 输出修改到分配的输出目录, 移到下一个文件,完成。
现在,如果我将脚本移动到日志目录之外,我似乎无法让它对这些文件执行任何操作。
这里是我的代码示例:
#!/bin/bash
while getopts ":i:o:" opt; do
case $opt in
i)
indir="$OPTARG"
;;
o)
outdir="$OPTARG"
;;
\?)
echo "invalid option"
exit 0
esac
done
shift $((OPTIND-1))
for f in *.log
do
shopt -s nocasematch
f4l4="${f:0:4}${f:${#f}-4}"
if [[ "${f4l4}" = "this.log" ]]; then
tr -cd "[:print:]\n" < $f | awk -F, 'BEGIN{OFS=FS}for(i=6,i<8;i++) $i=sprintf(%02X,$i)}1' > $outdir$f.csv
sed -i '1icolumn1,column2,column3,column4,5,6,7,8,etc' $outdir$f.csv
elif [[ "${f4l4}" = "that.log" ]]; then
parse log file another way < $f | sed this and that > $outdir$f.csv1
fi
done
所以我使用中的$indir
变量声明($indir$f instead of $f
)都试过,但没有奏效。如果我echo $f
那么我可以看到目录中的所有文件,但脚本不会做任何事情。
总之,我想使用getopts指定一个输入目录,其中包含要编辑的文件以及要保存的编辑文件的输出目录。
想法?
'下载= “$ OPTARG”; cd“$ indir”; ....'?或者更好的代码在目录和文件之间添加'/',即'tr -cd'[:print:] \ n“<$ indir/$ f'? '$ outdir/$ f'也一样。祝你好运。 – shellter