-2

I'm trying to use an If/else statement in bash. I'm confused which is the correct syntax.

  if [ "$NUMBER_dcn" -gt $NO ]
  then
  dir_dcn='/mnt/md0/capture'
  fi

or

   if [ "{$NUMBER_dcn}" -gt $NO ]
  then
  dir_dcn='/mnt/md0/capture'
  fi

NUMBER_dcn is populated using

NUMBER_dcn=$(find /mnt/md0/capture/ -maxdepth 1 -name "*.pcap" |wc -l)

Are both of them correct because one of them is giving me an "integer expression expected" error.

closed as unclear what you're asking by Mikel, slm, devnull, X Tian, jasonwryan Mar 21 '14 at 19:17

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    The second one should not be ${NUMBER_dcn} instead? – Laurent C. Mar 21 '14 at 9:15
  • That was a typo. :) – Jishnu U Nair Mar 21 '14 at 9:17
  • Ok :p Anyways why are you putting your variables into double quotes? As you are using -gt, it should be a number. Can you try without the quotes? – Laurent C. Mar 21 '14 at 9:18
  • I've tested it, iwth and without quotes and with boths writings with no issue. Are you sure $NO contains a number? How is it populated? Are you using bash, or else? – Laurent C. Mar 21 '14 at 9:24
  • Yes NO=0 and I'm using bash – Jishnu U Nair Mar 21 '14 at 9:28
1

The correct way of referencing a variable is $VAR. Since your VAR is populated by wc, I'm assuming that it is always non-empty, so you don't actually need the quotes "" - those are only for guarding against the case that a variable might be totally empty.

However, that is not your problem here. The -gt operator not only requires two arguments, but they must be integers. What you're passing to -gt here is, e.g. 50 in the one case and {50} in the other. The latter is not an integer expression, it's a String starting with {, so you should leave the braces off.

Braces are a permissible alternative syntax for using variables: $VAR is the same as ${VAR}. This is sometimes useful when you interpolate a variable in a way that it is unclear where the variable name ends. For instance, if you want to print your variable value and an index, sometimes it is necessary to write something like

echo ${VAR}00

to get output like Hugo00. Without the braces, bash would try to dereference the variable VAR00 and fail, since there is no such variable. (Note that in this case there is a dollar sign in front of the braces.) But since you're not interpolating anything, but using the variable exactly as it is, you don't need to bother with braces.

  • So what you say is when the variable contains only numbers, $VAR and ${VAR} do not behave the same ? If you look at the question, the second form of the OP's script is ${NUMBER_dcn}, the $ is placed before the curly brace. See the edit of the question by the OP. – Benoit Mar 21 '14 at 9:39
  • No, the content of the variable is irrelevant. Braces are useful to influence the behaviour of bash during the parsing state before dereferencing. They make a difference only when the dereferencing expression is written immediately before another character that could be part of the variable name, but isn't. For instance, a fake Russian generator might use something like for word in $SENTENCE; do echo -n "${word}ski "; done. – Kilian Foth Mar 21 '14 at 9:44
  • I just wanted to attract your attention on the fact that the question as been edited to contain "${NUMBER_dcn}" and no more "{$NUMBER_dcn}" which would indeed produce the effect you described in your answer. – Benoit Mar 21 '14 at 10:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.