1

with the below code I keep getting the following errors:

#!/bin/bash
    sourceFile="file1.log"
    targetFile="/etc/network/interfaces"
    numLines=$(wc -l ${sourceFile})
    if (( counter >= "$numLines" || ! -f "${sourceFile}" )); then
            echo "invaild file"
            exit 0
    fi
    while [ "$counter" -le "$numLines" ]; do
            sed -i "${2} s/.*/wireless-key s: $(sed -n "${counter}"p <<< "${sourceFile}")/" "${targetFile}"
            counter=$((counter + 1))
    done

with the above code I keep getting the following errors:

  > ./2test.sh: line 5: ((: counter >= 12 file1.log || ! -f file1.log : syntax error: invalid arithmetic operator (error token is ".log || !
  > -f file1.log ") ./2test.sh: line 9: [: : integer expression expected
12
  • 2
    wc -l ${sourceFile} prints 12 file1.log, so that's the value of $numLines. numLines="$(wc -l < "${sourceFile}")" should give you just the number (the quotes are free this week, enjoy). Commented Feb 19, 2017 at 13:33
  • 1
    Seems like echo 'invalid file' >&2; exit 1 would be more appropriate. Errors should be printed to stderr, and the script ought to return non-zero if it fails. Commented Feb 19, 2017 at 13:37
  • Also ! -f "${sourceFile}" is not valid code in (()), maybe try it in square brackets i.e (( counter >= "$numLines" )) || [[ ! -f "${sourceFile}" ]] Commented Feb 19, 2017 at 13:38
  • Thank you Biffen, William Pursell & 123 Commented Feb 19, 2017 at 13:49
  • I've just got the last error still, do you know anything on that one? Commented Feb 19, 2017 at 13:50

1 Answer 1

3

Turning my comment into an answer.

wc with an explicit filename includes that filename in the output, so:

wc -l ${sourceFile}

Prints:

12 file1.log

That's then the value of $numLines.

The clue is in the error message: It includes the expanded expression:

> ./2test.sh: line 5: ((: counter >= 12 file1.log || ! -f file1.log : syntax error[…]
                                     ^^^^^^^^^^^^

You can avoid getting that filename by redirecting the file's contents to wc instead:

numLines="$(wc -l < "${sourceFile}")"

(The added quotes are thrown in as it's a good habit. (No, they're not strictly necessary in this case.) (And yes, the qoutes are correct, even if SO's syntax highlighter doesn't quite seem to understand them.)

There are other issues in the script (mentioned in the comments), but one question at a time. I will take the opportunity to recommend ShellCheck, though.

Sign up to request clarification or add additional context in comments.

2 Comments

nit: "piping" the file's contents can be done with cat $sourceFile | wc -l (UUOC), but there is no pipe involved with wc -l < $sourceFile. A redirection is not a pipe.
@WilliamPursell Heh, lazy me. Fixed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.