Bash脚本读取文件:参数列表太长

时间:2016-08-09 16:45:56

标签: linux bash shell

我的代码

#!/bin/bash

file =$( < 262_V01_C00_R000_TEx_BL_2048H.dat)

awk '$0+0 == $0 { printf "%.3f\n", $0 / .03 }' file > output

终端

milenko@milenko-HP-Compaq-6830s:~/procmt$ bash a5.sh
a5.sh: line 3: /usr/bin/file: Argument list too long

为什么?

1 个答案:

答案 0 :(得分:2)

空白很重要。

这不是一个赋值 - 而是运行名为file的命令,其参数列表以=开头,并包含文件262_V01_C00_R000_TEx_BL_2048H.dat字符串的内容 - split和glob-expanded(碰巧太长而无法放在参数列表中):

# runs the file command with an argument list based on the contents of your .dat file
# will thus fail if the .dat file's contents cannot fit on a UNIX command line
file =$( < 262_V01_C00_R000_TEx_BL_2048H.dat)

是一项作业:

# reads the contents of your .dat file into the variable named file
# note that this doesn't correctly handle content with NUL literals
file=$(<262_V01_C00_R000_TEx_BL_2048H.dat)

...您可以将阅读的内容反馈到awk的输入中:

# feed contents of the file variable on stdin to awk
awk '$0+0 == $0 { printf "%.3f\n", $0 / .03 }' <<<"$file" > output

那就是说,你不清楚为什么要尝试将文件的内容分配给一个变量。为什么不直接将awk连接到文件?

# store filename in a variable
file=262_V01_C00_R000_TEx_BL_2048H.dat

# ...and pass that filename to awk
awk '$0+0 == $0 { printf "%.3f\n", $0 / .03 }' "$file" > output