简体   繁体   English

ash: -c: 未知操作数

[英]ash: -c: unknown operand

I'm trying to run a ash script that constantly checks how many characters are in a file and execute some code if it reaches at least 170 characters or if 5 seconds have passed.我正在尝试运行一个 ash 脚本,该脚本不断检查文件中有多少个字符,并在达到至少 170 个字符或已过去 5 秒时执行一些代码。 To do that I wanted to call wc -c , however it keeps telling me it has an unknown operand.为此,我想调用wc -c ,但是它一直告诉我它有一个未知的操作数。

The code:编码:

#!/bin/ash
while true; do
secs=5
endTime=$(( $(date +%s) + secs ))
while [ /usr/bin/wc -c < "/tmp/regfile_2" -gt 170 ] || [ $(date +%s) -lt $endTime ]; do

#more code

It's output is ash: -c: unknown operand它的输出是ash: -c: unknown operand

You want to check whether the output from wc meets a specific condition.您想检查wc的输出是否满足特定条件。 To do that, you need to actually execute wc , just like you already do with date to examine its output.为此,您需要实际执行wc ,就像您已经使用date来检查其输出一样。

while [ $(wc -c < "/tmp/regfile_2") -gt 170 ] ||
      [ $(date +%s) -lt $endTime ]; do
    # ...stuff

Notice the $(command substitution) around the wc command.注意wc命令周围的$(command substitution)

As you can see from the answer to the proposed duplicate Checking the success of a command in a bash `if [ .. ]` statement your current command basically checks whether the static string /usr/bin/wc is non-empty;正如您在 bash `if [ .. ]` 语句中对建议的重复检查命令是否成功的回答中所见,您当前的命令基本上检查静态字符串/usr/bin/wc是否为非空; the -c after this string is indeed unexpected, and invalid syntax.此字符串后面的-c确实出乎意料,并且语法无效。

(It is unclear why you hardcode the path to wc ; probably just make sure your PATH is correct before running this script. There are situations where you do want to hardcode paths, but I'm guessing this isn't one of them; if it is, you should probably hardcode the path to date , too.) (不清楚为什么要硬编码wc的路径;可能只是在运行此脚本之前确保您的PATH正确。在某些情况下,您确实想对路径进行硬编码,但我猜这不是其中之一;如果是的,您可能也应该对date的路径进行硬编码。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM