1

im new to bash scripting, appreciate if you can help.

Im trying to write a script to compare the lines in file with integer argument.

Here is what i've got so far but i make some mistakes and get error.

                 #!/bin/bash
                 a="$1"
                 b="wc -l < /filepath/filename.txt"
                 if (( $a < $b )); then
                 echo "file has more lines than integer"
                 else
                 echo "file has less lines than integer"
                 fi

Appreciate if you can point to where i make mistake.

Sysad85
  • 321
  • 2
  • 11
  • If you don't specify which error, it's hard(er) to help. One thing is wrong though: if you specify 10, and the number of lines is 10, (and all the syntax errors would be solved), the script would say "file has less lines than integer" even though they are the same. – GolezTrol Oct 02 '17 at 21:11
  • `b="foo"` is assigning a string; it doesn't run `foo` as a command. – Charles Duffy Oct 02 '17 at 21:11
  • 1
    Syntax error: invalid arithmetic operator. error token is ".txt < wc -l < filepath/filename.txt " – Sysad85 Oct 02 '17 at 21:13
  • 1
    Then How can I assign the output of a command to a variable? – Sysad85 Oct 02 '17 at 21:16

1 Answers1

1
b="wc -l < /filepath/filename.txt"

should instead be:

b=$(wc -l < /filepath/filename.txt)

...if you want to run that command and store its output in the variable.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441