-1

So I have 1 bash scripts,

findFungible.sh

#!/bin/bash
for file in $*;
    for word in $(cat $file);
        if [ $word == Fungible ];
            echo Fungible found
        fi
    done
done

Which should be checking files if they contain the word fungible. It's pretty much verbatim out of my lecture example.

So if I run it with bash findFungible.sh I get:

findFungible.sh: line 2: syntax error near unexpected token "$\r''
'indFungible.sh: line 2: 'for file in $*;

So I think it has something to do with windows putting in extra line \r characters or something. As there is a \r after $.

Then if I run it with sh findFungible.sh I get:

findFungible.sh: 2: findFungible.sh Syntax error: word unexpected (expecting "do")

Any help would be great thanks.

Argon
  • 55
  • 1
  • 7
  • 1
    `help for` and try https://shellcheck.net also that error is windows line endings which is a carriage return. – Jetchisel Jun 19 '20 at 03:26
  • 1
    See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) Also, for such a short script this is extremely dense in bad scripting practices (mostly having to do with unquoted variable references). And it's missing `do` and `then` keywords. I don't think I trust that lecture you're following... – Gordon Davisson Jun 19 '20 at 04:27
  • Trying to use `for var in $(cat file)` to read the lines of a file is wrong too; see http://mywiki.wooledge.org/BashFAQ/001 and http://mywiki.wooledge.org/DontReadLinesWithFor – Shawn Jun 19 '20 at 04:49
  • @Argon : If you look at your script with `xxd findFungible.sh`, you will see that there are carriage returns in your script. – user1934428 Jun 19 '20 at 06:57

1 Answers1

0

As someone mentioned in a reply, you have syntax errors in this, meaning, you are missing "do's" and "then's".

#!/bin/bash
for file in $*; do
    for word in $(cat $file); do
        if [ $word == Fungible ]; then
            echo Fungible found
        fi
    done
done

And yes, like mentioned in the reply, bash is very sensitive to white spaces, new lines and quotes, I'm not getting into too much detail there.

Marcio Lucca
  • 370
  • 2
  • 10