A
A
Alexey Yarkov2017-02-28 16:00:06
linux
Alexey Yarkov, 2017-02-28 16:00:06

Why does one code give different results?

I'm trying to count the number of lines of code in *.js files:

#!/bin/bash
# файл wc.sh
set -e
find ./src -type f -name "*.js" | xargs cat | sed '/^\s*$/d' | grep -i -v -e '^\s*(\/\/|\/\*|\s*\*)\s*' | wc -l

Result:
$ ./wc.sh
4585

And if you run it directly in the terminal:
$ find ./src -type f -name "*.js" | xargs cat | sed '/^\s*$/d' | grep -i -v -e '^\s*(\/\/|\/\*|\s*\*)\s*' | wc -l
2906

Why is the correct amount in the terminal? That is, the command in the script does not take into account grep processing?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Yarkov, 2017-02-28
@yarkov

In the end it worked the same way:

$ find ./src -type f -name "*.js" | xargs cat | sed '/^\s*$/d' | grep -i -v -E '^\s*(\/\/|\/\*|\s*\*)\s*' -c
2469

Y
Yuri Chudnovsky, 2017-02-28
@Frankenstine

Escaping in scripts works... a bit different than in the command line :) Additional slash escaping is required. Or replace double quotes with single quotes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question