-1
DSKUSDPERCENT=$(($DISKUSD * 100 / $limit))

throws an error like

syntax error: operand expected (error token is "/ ")

where limit=$OPTARG defined in Process command line options

Here I am not able to get the value of $limit

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Rahul
  • 21
  • 5
  • 2
    Run the code with `set -x` turned on. Most likely `limit` is blank. – user1934428 Apr 20 '23 at 10:43
  • 2
    `if test 0 -lt "$#"; then`. Neither the command substitution nor the subshell are necessary. (In fact, they are actively harmful; `test` produces no output, and a shell with no command trivially succeeds; you have a complicated way of writing `if true; then ...`.) Run your code through shellcheck.net before asking questions here. – chepner Aug 03 '23 at 17:46
  • `doopts $@` is broken. **Always** quote your arguments: `doopts "$@"` -- and edit to fix the other bugs https://shellcheck.net/ shows you too. A `set -x` log would be extremely useful, since it would show us which arguments your script (and thus `doopts`) was actually passed. If you don't pass a limit argument in practice, we would expect this error. – Charles Duffy Aug 06 '23 at 13:02

1 Answers1

1

Check $limit value:

$ DISKUSD="1000"
$ limit="100"
$ echo $(($DISKUSD * 100 / $limit))
1000
$ limit=''
$ echo $(($DISKUSD * 100 / $limit))
bash: 1000 * 100 / : syntax error: operand expected (error token is "/ ")

So, it is likely that $limit is empty, so that the expression becomes something like $((666 * 100 / )), which is, of course, invalid.

where limit=$OPTARG

Well, either there's no command line option for limit is provided, or there's some mistake in processing arguments.

Nikolaj Š.
  • 1,457
  • 1
  • 10
  • 17
  • thanks for the prompt response, I couldn't follow your answer. could you please provide more description – Rahul Apr 20 '23 at 09:55
  • @Rahul, I've expanded it a bit – Nikolaj Š. Apr 20 '23 at 12:33
  • how can I correct this [syntax error: operand expected (error token is "/ ")], I am bit confused – Rahul Aug 03 '23 at 07:34
  • 1
    @Rahul, the code you're posted is correct. The "syntax error" is because `$limit` is empty (or not defined), but we can't help you fix it, because you haven't posted the code that's supposed to have `$limit` defined/set. – Nikolaj Š. Aug 03 '23 at 12:42
  • @ Nikolaj Š., updated more details – Rahul Aug 03 '23 at 17:37
  • @Rahul, post the whole script and how you run it. Your code snippet can't be run, and even after a bit of fiddling with it I can't reproduce the problem. – Nikolaj Š. Aug 03 '23 at 19:55
  • Also, _chepner_'s suggestion to run the code through shellcheck is an absolute must. – Nikolaj Š. Aug 04 '23 at 09:59
  • @Rahul, it's helpful that you're now posting the whole script, but you left out the "and how you run it". If we don't know that you're passing `-l` with a valid limit value, then we still don't know that limit should be gegtting set. – Charles Duffy Aug 06 '23 at 13:04
  • @Charles Duffy, I am able to run by passing "-p filesystem" and able to get/see the value of available space but % disk used could not able to get due to limit is null.. Also validated using shellcheck – Rahul Aug 06 '23 at 14:32
  • 1
    @Rahul, you can't _only_ pass `-p filesystem`; the way your script is written you **must** also pass `-l limit` with a valid number in place of `limit`. If you don't want that, update your code to have smarter handling for what to do when no limit is set. – Charles Duffy Aug 06 '23 at 14:56