1

I have a bash script that essentially run a computed grep command against a file:

cat $myfile | $string

where myfile, is a list of words and string, is a grep command with parameters:

/usr/bin/grep -v  -e b -e j -e k -e l -e m -e q -e v -e x

The grep parameters are computed earlier in the script from a another process When I run the command, cat $myfile | $string, from the console I get the expected output. When the command runs from a script, I get:

./sp2: line 126: /usr/bin/grep -v  -e b -e j -e k -e l -e m -e q -e v -e x : No such file or directory

This is running through cygwin, and really did work several years ago. Now... not so much.

If anyone has any ideas on what the issue is, and how I can resolve it, I would certainly appreciate it.

Cheers!

4
  • 1
    Is it myfile or string that's se to that command? Storing commands in variables isn't a good idea; see [BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail ](mywiki.wooledge.org/BashFAQ/050) As for the actual problem, have you by any chance changed IFS? Commented Oct 20, 2018 at 0:54
  • That's interesting. I am using IFS. because of embedded spaces in path names I used IFS=$(echo -en "\n\b") Commented Oct 20, 2018 at 1:34
  • That was it!! i removed IFS, ( after renaming my directories etc) The program works fine now. Commented Oct 20, 2018 at 1:38
  • 3
    There are better ways to handle filenames with weird characters than changing IFS. If you need to store a list of files, use an array rather than a plain variable, and expand it with "${filearray[@]}". See BashFAQ #20: How can I find and safely handle file names containing newlines, spaces or both? for more tricks. Commented Oct 20, 2018 at 2:08

3 Answers 3

1

can you try adding this to the line before it:

echo "cat $myfile | $string"

(lets see what it's doing)

can you also change it to an egrep after getting that print out from the previous command and see what it does in the script manually added ?

egrep -v "(b|j|k|l|m|q|v|x)" "${myfile}"

( something like this )

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

7 Comments

this is what is being passed: cat /cygdrive/c/Gunhead/Downloads/ExcelMacros/POP/Allwords | /usr/bin/egrep -v -e b -e j -e k -e l -e m -e q -e v -e x. Changing the grep statement to egrep, works well, but when I use it as a string, it fails the same way the grep statement fails. For some reason my implementation is causing bash to treat the grep/egrep command differently when its in a variable as opposed to it being hard-coded.
build the string as the input to grep like egrep -v “(${string})” etc also to confirm if u placed the command not as a string but actually in code it works as well right?
I was using IFS. I messed up the command string. The script was written way back when, I had lots of pathnames with spaces in them. IFS was my way of dealing with them. No spaces in pathnames now and no reason to use IFS.
@C0ppert0p Hey, you gave us a little too little info. at this point for instance, have you tried different data sets for this command in the script ?
Good point. Thanks. As an aside, kudos to the developers of the Stack Overflow App. Without which, I would never been able to keep track of responses.
|
0

This works fine for me when I try with both sh and bash. I suspect that your problem is that the script file actually contains cat $myfile | "$string" rather than cat $myfile | $string. The former will treat the contents of $string as a single token, rather than being separated by spaces, which would cause exactly the behavior that you're seeing.

3 Comments

That was my first guess, however the line really does say cat $myfile | $string. Also, if I substitute the actual string command /usr/bin/grep -v -e b -e j -e k -e l -e m -e q -e v -e x . It works fine. For some reason bash is not treating $string like an actual command
@C0ppert0p What shell and version is running this? Also, can you post the real, actual commands and files, rather than just things that should look/work like them?
It turned out that IFS was the culprit. Thanks, for the help though
0

If it doesn't appear to be a quoting issue as others have suggested, is there a specific reason you're using Cygwin?

If you use Windows 10, it has a very good native linux subsystem that's way better than Cygwin. Unless that's not a viable approach for you, I'd give that a shot before spending a lot of time on what might be an idiosyncratic to Cygwin.

1 Comment

yeah, I know. But I'm forced to work with what I have (Windows7). The issue turned out to be my use of IFS. I'd written the script years ago, and back then it was on another machine, in another directory structure, and I used IFS because of embedded spaces in folder names. In the current environment I didn't have spaces in folder names and no reason to use IFS. I took it out and now I'm golden.

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.