File or directory not found from script, but there from CLI

Hi guys!

I feel absolutely terrible writing this, seeing as I don’t have a Stackoverflow account so cannot ask there, nor do I want to, to b e honest. I’d rather struggle on my own, which would probably lead to me giving up.

Anyway, I’m trying my hand at bash scripting for the first time ever. I’ve only done CSS, PHP, and Javascript/ES 6 up until last week.

Now, my question is this:

When I run this grep command in the terminal, it works as expected:

grep '^w[a-z][a-z][a-z][a-z]$' /usr/share/dict/words

I can even pipe, |, the results to refine it:

grep '^w[a-z][a-z][a-z][a-z]$' /usr/share/dict/words | grep -v '[omens]' | fgrep 'a' | fgrep 'c' | fgrep 'k'

And everything works like a charm.

However, when I put it in a bash script, it gives me the following error:

grep '^w[a-z][a-z][a-z][a-z]$' /usr/share/dict/words | grep -v '[omens]' | fgrep 'a' | fgrep 'c' | fgrep 'k'
line 155: grep '^w[a-z][a-z][a-z][a-z]$' /usr/share/dict/words | grep: No such file or director

I’ve noticed that is seems Manjaro aliases the grep command, because I coouldn’t find an alias in one of my own config files, yet:

$ which grep
grep: aliased to grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}

Which led me to trying /usr/bin/grep instead of grep in the commands above:

/usr/bin/grep '^w[a-z][a-z][a-z][a-z]$' /usr/share/dict/words | /usr/bin/grep -v '[omens]' | /usr/bin/fgrep 'a' | /usr/bin/fgrep 'c' | fgrep 'k'

But the output remains the same:

line 155: /usr/bin/grep '^w[a-z][a-z][a-z][a-z]$' /usr/share/dict/words | /usr/bin/grep: No such file or directory

Which leads me to ask, if there is a special way to call grep from a script, vs. calling grep from the CLI. And I’m presuming the same would be the case for fgrep.

If my question shouldn’t be here, just tell me and feel welcome to remove it then.

Your way to call a command in a shell script is correct. It also doesn’t matter if grep is aliased to grep ADDITIONAL_ARG ....

It’s likely that your script has some issue because the grep isn’t recognized as an executable, but as regular file or directory instead.

Check the previous line if it’s properly ended with newline without \.

Just did a double-check. And the line line is on it’s own, so I’m guessing there is a newline before and after it.

Also just did a test, it doesn’t matter if I do it in a subshell, like this:


or directly, like this:


The error stays the same. It seems.


And I’ve confirmed, numerous times, my shebang is correct:

#!/usr/bin/env bash

Well, correct AFAIK.

I tested on my side in simple script and it works. You use it in a long script so I think it’s in relation between previous lines potentially but it’s not the command itself.

Is it possible that you’ve somehow tacked some non-printable character to the end of /usr/share/dict/words?

Hey! That’s quite possible!

That file is 1.6 million lines long, and from what I see in micro the last line is a blank line.

So, I don’t really think so.

There’s a chance grep ran out of memory ─ see the man page.

It should be. A text file ─ script or otherwise ─ should always end on a newline in UNIX.

By the way…

[10:40:12][aragorn] >  ls -l /usr/share/dict/
total 4
lrwxrwxrwx 1 root root 34 Nov 13  2019 cracklib-small -> /usr/share/cracklib/cracklib-small

I don’t seem to have a words file in there. :thinking:

That’s why I checked on CLI, and it runs without a problem or hiccup.

Then it’s awesome!

It’s not standard. IIRC I installed words specifically for it:

$ pamac search words
words                                                                                                                                                                                                     [Installed] 2.1-6                       community
A collection of International 'words' files for /usr/share/dict.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.