0

I'm using bash shell on Ubuntu Linux. I have this in my script

output_file=create_test_results_file "$TFILE1"

Through echo statements, I have verified that the value of $TFILE1 is a file path, e.g.

/tmp/run_tests.sh1.7381.tmp

But when I run my script, somehow the contents of the file are being passed to my function, whose contents are

#!/bin/bash

create_test_results_file () {
        RESULTS_INPUT_FILE=$1
        OUTPUT_FILE="/tmp/output`date +%m`_`date +%d`_`date +%y`.txt"
        touch $OUTPUT_FILE
        marker=""
        num_passed=0
        num_failed=0
        while read p; do
                if [[ $p == *"√"* ]]; then
                        if [[ $p == *"PASSED"* ]]; then
                                num_passed=$((num_passed+1))
                        elif [[ $p == *"WARNING"* ]]; then
                                num_failed=$((num_failed+1))
                        fi
                elif [ $num_passed -gt 0 -o $num_failed -gt 0 ]
                then
                        echo "second branch"
                        echo "$marker PASSED: $num_passed, WARNING: $num_failed" >> $OUTPUT_FILE
                        marker=$p
                        num_passed=0
                        num_failed=0
                else
                        marker=$p
                fi
        done <"$RESULTS_INPUT_FILE"

        # Add un-added lines
        if [ $num_passed -gt 0 -o $num_failed -gt 0 ]
        #if [ \( "$num_passed" -gt 0 -o "$num_failed" -gt 0 \) -a \( -z "$marker" \) ]
        then
                echo "$marker PASSED: $num_passed, FAILED: $num_failed" >> $OUTPUT_FILE
        fi
        echo $OUTPUT_FILE
}

because I get errors like

/tmp/run_tests.sh1.7381.tmp: line 1: Validation: command not found
/tmp/run_tests.sh1.7381.tmp: line 2: 2017-04-20: command not found
/tmp/run_tests.sh1.7381.tmp: line 3: Login: command not found
/tmp/run_tests.sh1.7381.tmp: line 4: $'\E[1': command not found

The words "Validation", "2017-04-20", and so on, are all contents of the file. What's the correct way to pass in the file path as an argument and not have it be interpreted literally?

Dave
  • 2,548

1 Answers1

3

The command line for calling your function:

output_file=create_test_results_file "$TFILE1"

This will assign the value create_test_results_file to the variable output_file before running the command "$TFILE1".

I believe you might have wanted to do

output_file=$( create_test_results_file "$TFILE1" )

This assigns the output of create_test_results_file "$TFILE1" to the variable output_file.


There are several things one could comment upon in this script, but I'll pick this line:

OUTPUT_FILE="/tmp/output`date +%m`_`date +%d`_`date +%y`.txt"

This is better written as

OUTPUT_FILE=$( date +"/tmp/output%m_%d_%y.txt" )

Also related:

Kusalananda
  • 333,661