0

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. To do that I wanted to call wc -c, however it keeps telling me it has an unknown operand.

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

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • You have `ash` (sic), not `bash`. –  Jul 15 '21 at 12:17
  • Does this answer your question? [Checking the success of a command in a bash \`if \[ .. \]\` statement](https://stackoverflow.com/questions/36371221/checking-the-success-of-a-command-in-a-bash-if-statement) – tripleee Jul 15 '21 at 12:27
  • @Roadowl `ash` is another shell. – chepner Jul 15 '21 at 12:31
  • (Unintentional near-pun there; `ash` is the [Almquist Shell](https://en.wikipedia.org/wiki/Almquist_shell); it doesn't literally stand for **A**nother **Sh**ell.) – chepner Jul 15 '21 at 12:32
  • The question was originally incorrectly tagged [tag:bash] [tag:ash] – tripleee Jul 15 '21 at 12:36

1 Answers1

2

You want to check whether the output from wc meets a specific condition. To do that, you need to actually execute wc, just like you already do with date to examine its output.

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

Notice the $(command substitution) around the wc command.

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; the -c after this string is indeed unexpected, and invalid syntax.

(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.)

tripleee
  • 175,061
  • 34
  • 275
  • 318