0

I am trying to extract the number of lines from a file, and then use it in a variable. However, it keeps passing the file name, not the number of lines. I read through this question, but the examples are not working.

for i in $BASE/$TEMPLATE_DIR-$i$PRUNING_METHOD/*.txt
    do
        NUM_LINES=$(wc -l < $i)
        echo $NUM_LINES
        UPLOAD_CMD="sh sshpass_setup_verification.sh $EXP_ID-$i$PRUNING_METHOD__$NUM_LINES__features";
        echo "$UPLOAD_CMD"
        break 1;
    done

Prints:

15 #the correct number of lines
sh sshpass_setup_verification.sh di-60sec-max/TemplateUser1.txt #where TemplateUser1.txt is the name of the file

Should print:

15
sh sshpass_setup_verification.sh di-60sec-max__15__features
Community
  • 1
  • 1
Adam_G
  • 7,337
  • 20
  • 86
  • 148
  • 2
    Use `${NUM_LINES}` (with the curlies) in your `echo` statement. Otherwise, Bash thinks you mean the variable named `NUM_LINES__features` which isn't set and expands to nothing. – gniourf_gniourf Mar 26 '15 at 17:55
  • 3
    `....${EXP_ID}-${i}${PRUNING_METHOD}__${NUM_LINES}__features`. Since `_` is a valid character in a variable name, and you don't actually have a `NUM_LINES__features` variable defined... Just get in the habit of always using braces (and always quoting, and ...)... – twalberg Mar 26 '15 at 17:55
  • It's now putting a space in, like this: `sh sshpass_setup_verification.sh di-60sec-best-test2-1-max__ 822__features`. Any idea why? – Adam_G Mar 26 '15 at 18:07

1 Answers1

1

A summary of what people are telling you in the comments:

for i in "${BASE}/${TEMPLATE_DIR}-${i}${PRUNING_METHOD}"/*.txt
do
    num_lines=$(wc -l < "$i")
    echo "$num_lines"
    upload_cmd="sh sshpass_setup_verification.sh ${EXP_ID}-${i}${PRUNING_METHOD}__${num_lines}__features"
    echo "$upload_cmd"
    break
done

The key thing here is using double quotes around your parameter expansions and curly braces to disambiguate in situations where characters such as _ could be interpreted as part of a variable name but shouldn't. I've added them in several places where they aren't strictly needed but they do no harm.

I've also changed your variable names to lowercase. This will help you one day when you decide to have a variable called PATH and suddenly all your commands stop working.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • This is still adding spaces before the `num_lines` value – Adam_G Mar 26 '15 at 18:26
  • @Adam_G I forgot to change `NUM_LINES` to `num_lines` in the command (but not sure that was the problem). Is it definitely that variable with a space in front of it? What does `declare -p num_lines` tell you?? – Tom Fenech Mar 26 '15 at 18:35
  • Weird. It has a huge space in it: `declare -- num_lines=" 822"`. Any idea why this got added, and how to trim it out? – Adam_G Mar 26 '15 at 18:38
  • 1
    @Adam_G I'm not sure where it came from, but there are many ways to trim it out: http://stackoverflow.com/q/369758/2088135 - in this case, as you want to remove all space characters, you could just use `${num_lines// /}`. – Tom Fenech Mar 26 '15 at 18:44
  • Thanks! That did it. Could you explain what the double and single backslash do? – Adam_G Mar 26 '15 at 18:46
  • The syntax `${var//a/b}` means "replace all occurrences of a with b". Here, we're replacing all spaces with nothing. – Tom Fenech Mar 26 '15 at 18:48
  • Ahh, right. I just didn't recognize it with the empty string. Thank you again. – Adam_G Mar 26 '15 at 18:49