linux – 压缩Bash脚本来解析文件
发布时间:2020-12-13 22:48:26 所属栏目:Linux 来源:网络整理
导读:我有这个bash脚本来填充一个文本文件,我想让它更简单,更简单,使它更有效率,有没有人有任何想法,我怎么能这样做? $vifunction displayHelp{ echo "Use '-f' to set the file to be used " echo "Use '-s' to sort the data bya column" echo "Use '-m' to ou
我有这个bash脚本来填充一个文本文件,我想让它更简单,更简单,使它更有效率,有没有人有任何想法,我怎么能这样做?
$vi function displayHelp { echo "Use '-f' to set the file to be used " echo "Use '-s' to sort the data bya column" echo "Use '-m' to output the rows which match this expression" } function displayColumn { columnnumber="$2" awk '{print $'$columnnumber'}' $1 } function displayParameter { parameter="$3" columnnumber="$2" awk -v s=$3 -v c=$2 '$c ~ s { print $0 }' $1 } while getopts f:s:m:h opt do case "$opt" in h) displayHelp;; f) filepath="$OPTARG";; s) column="$OPTARG" displayColumn $filepath $column ;; m) searchParam="$OPTARG" displayParameter $filepath $column $searchParam ;; esac done 解决方法
在displayColumn()中,你没有使用-v变量传递,但是在displayParameter()中你是.您应该始终使用-v而不是嵌入shell变量.
同样在displayParameter()中,您可以分配两个变量,而不会使用它们. 在扩展时,应始终引用shell变量.当它们单独出现时,没有必要在作业的右侧引用它们. 如果这是你想要的,那么就没有强制执行-f是必需的.如果没有提供它(以及它的必需参数),你的脚本将尝试从stdin读取(这可能是你想要的). 没有执行-m和-s是必需的.如果两者都不存在,AWK命令将输出错误消息. 通常,必须将参数作为位置参数处理,并且选项标志应该用于选项(及其参数). 你的问题可能更适合Code Review. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |