-1

I'm recieving this in the error -

syntax error near unexpected token `total_lines=$(wc -l < $extracted_log_path_value)'

$extracted_log_path_value stores the path of the file which is correctly defined. I'm using simple shell script. No plugin is installed. I have also tried following commands - `

syntax error near unexpected token `total_lines="$(wc -l "$extracted_log_path_value" | cut -d' ' -f1)"'. 

This is the code that is written in shell script. I'm pasting the actual line as well -

total_lines="$(wc -l "$extracted_log_path_value" | cut -d' ' -f1)"

`

bosari
  • 1,922
  • 1
  • 19
  • 38
  • back ticks and single quote? – SMA Jan 22 '18 at 11:25
  • 1
    @SMA: no, that's not the problem, this is just how bash outputs error messages. Try it out with "$ for foo iiiin bar", you will get something like "bash: syntax error near unexpected token `iiiin' " with the problematic token enclosed in a backtick and a single quote. – Andrey Tyukin Jan 22 '18 at 11:34
  • 1
    If you quote the variable name, your example is fine. Without the quotes the example is buggy. The problem is somewhere else in your script. The error message says "syntax error near ...", which means the syntax error is before the line you have written in the question. – ceving Jan 22 '18 at 11:54
  • 1
    What are those two backticks in your plain text, right after "commands -" and in the very end of your question? Do you have any backticks randomly flying around in your code? The problem seems to be somewhere outside of the line that you've given us. – Andrey Tyukin Jan 22 '18 at 12:07

1 Answers1

1

wc does not expect the file to be fed into STDIN, it wants the file name as a simple argument.

total_lines="$(wc -l "$extracted_log_path_value" | cut -d' ' -f1)"

should do the trick. The cut part drops the filename and retains only the number of lines.

---

Correction: if you don't specify any files as arguments, wc does read from STDIN, and prints out the number of lines. If you do it like this, you don't need to invoke an additional cut:

total_lines="$(wc -l < "$extracted_log_path_value")"
Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
  • you may remove the wrapper double quotes for the assignment. – sjsam Jan 22 '18 at 11:32
  • 1
    That's correct. However, I strongly prefer to use "$( ... )" instead of backticks, and to wrap everything into double quotes. As long as it is enclosed in double quotes, I don't have to stop and look at it and ask myself "Can I really leave the double quotes out in this case?". I just prefer this more defensive style. – Andrey Tyukin Jan 22 '18 at 11:37
  • I agree to above as a best practice. `:-)` . Was just making a point regarding the assignment. Being defensive with this is indeed better and safe ! – sjsam Jan 22 '18 at 11:40
  • Still facing issue - syntax error near unexpected token `total_lines="$(wc -l < "$extracted_log_path_value")"' – bosari Jan 22 '18 at 11:41
  • tried your way as well - syntax error near unexpected token `total_lines="$(wc -l "$extracted_log_path_value" | cut -d' ' -f1)"' – bosari Jan 22 '18 at 11:42
  • 1
    `wc` reads from stdin if no file name is given in the command line arguments. – ceving Jan 22 '18 at 11:47
  • @basari66 open a terminal, type in this command, substituting "$extracted_..._value" by a name of some existing file. My version of bash saves the number of lines to the `total_lines` variable. What does your bash version do, if you type only this command, and nothing else? If it breaks, maybe it's because of the context around the command, not because of the command itself. – Andrey Tyukin Jan 22 '18 at 11:57
  • @ceving oh, indeed, you're right. If you feed the file to stdin, you don't even need the `cut` in the end. Then the problem definitely isn't in the command itself. – Andrey Tyukin Jan 22 '18 at 12:01
  • I got it mates. My bad, i forgot to write 'do' after for loop before the mentioned command and thought there's a problem in wc -l command – bosari Jan 22 '18 at 12:19