我想执行一个awk来自 bash 脚本的命令。我将文件路径作为参数传递给 bash 脚本,但该参数在awk命令。
我的 bash 文件mybash.sh代码:
#!/bin/bash
file="$1"
echo $file
awk 'BEGIN{FS=OFS=","} NF!=5{print "not enough fields"; exit} NR == 1 && ($1 != "nasme" || $2 != "designation" || $3 != "email" || $4 != "phone" || $5 != "group") {print "Wrong file headers"; exit}' $file
如果我运行 bash 文件:
bash mybash.sh /home/file_path.csv
我的脚本打印$文件正如我所给出的路径,但是我的awk命令未读取此内容$文件,因为它打印核因子始终为 0。但是,上面的 awk 命令可直接从终端正常运行。
awk 'BEGIN{FS=OFS=","} NF!=5{print "not enough fields"; exit} NR == 1 && ($1 != "name" || $2 != "designation" || $3 != "email" || $4 != "phone" || $5 != "grsoup") {print "Wrong file headers"; exit}' /home/file_path.csv
答案1
我会做如下的事情:
file="$1"
cat $file | awk 'BEGIN{FS=OFS=","} NF!=5{print "not enough fields"; exit} NR == 1 && ($1 != "nasme" || $2 != "designation" || $3 != "email" || $4 != "phone" || $5 != "group") {print "Wrong file headers"; exit}' > someoutputfile