我想将一个带有空格的字符串作为变量传递给来自Bash脚本的awk,但不管我如何逃避它,awk都会抱怨。请考虑以下示例:
list1的:
one
two
three
four
输出:
[user@actual ~]$ ./dator.sh list1
1470054866 two (...)
工作脚本:
CMD='awk'
DATE=$(date +%s)
VARIABLES="-v time=$DATE"
SCRIPT='NR>=2 {printf "%s %s\n", time, $1;}'
$CMD $VARIABLES "$SCRIPT" $1
只有更改日期格式才能打破它:
CMD='awk'
DATE=$(date -u)
VARIABLES="-v time=$DATE"
SCRIPT='NR>=2 {printf "%s %s\n", time, $1;}'
$CMD $VARIABLES "$SCRIPT" $1
我应该如何逃脱它?
答案 0 :(得分:1)
数组用于存储任意参数。
current_date=$(date +%u)
variables=( -v "time=$current_date")
script='NR >= 2 {printf "%s %s\n", time, $1;}'
awk "${variables[@]}" "$script" "$1"