0

I have a problem with reading a pattern from a script file to another file. Basically the output from one file should be used as an input to the other script file. The output from the maze_gen is an array of # and space symbols. the following example 5 7 represents the height and width of the output.

if [`./maze_gen 5 7 | grep -E "[^# ]"` ]; then
        echo no
else 
        echo yes
fi

So here maze_gen is a file that produces a series of # symbols with spaces.  This will receive as an input file that I am currently working on. There is something wrong with my code and I just can't check if the input has only # characters.

Praveen
  • 11
  • Could you add some example output from maze_gen to your question? – Natolio Apr 12 '21 at 21:19
  • If you just want a has any bad lines / doesn't have any bad lines try adding -m 1 [can use the return status] or add -c gives a count output. But as @Natolio says - attach a sample please. – Mr R Apr 12 '21 at 21:31
  • 1
    The code as written should produce a "command not found" error. Please make sure it matches what you think it should be (minding spaces in particular), and if it does, then paste it into https://shellcheck.net to see what's wrong – Chris Davies Apr 12 '21 at 21:51
  • I'm sorry if English isn't your first language, but please try harder to explain what you are trying to do and what is happening.  "There is something wrong with my code" is not clear. … … … … Please do not respond in comments; [edit] your question to make it clearer and more complete. – G-Man Says 'Reinstate Monica' Apr 12 '21 at 21:53
  • get rid of the backticks (`). and you probably want to use grep's -q option to suppress normal output. i.e. if [ ./maze_gen 5 7 | grep -q -E "[^# ]" ]; then – cas Apr 13 '21 at 05:05
  • Note to reviewers: This is not a duplicate of the bash: looping through characters not working question, IMHO. The data is stored in a variable in that question whereas here it's piped in. These two issues therefore needs two different answers, even though the originating homework assignment (or whatever) may be identical. – Kusalananda Apr 13 '21 at 06:58
  • @cas, you mean if ./maze_gen | grep -q; then. (roaima linked shellcheck.net earlier, an it also mentions that) – ilkkachu Apr 13 '21 at 09:33
  • @ilkkachu yep, that's what i meant. i didn't edit the copy-paste properly, forgot to delete the [ and ]. – cas Apr 15 '21 at 03:14

1 Answers1

0

It looks as if you want to test whether the output of ./maze_gen 5 7 contains characters other than spaces and hashes.

You can do that more efficiently using

if ./maze_gen 5 7 | grep -q '[^ #]'
then
    echo no
else
    echo yes
fi

This does not actually extract the lines that matches the expression. Instead grep -q exits as soon as the pattern matches and the exit-status of grep is used in the if statement to determine what branch to take (echo no or echo yes).

This is more efficient than what you seem to want to do because the grep utility stops processing its input as soon as the pattern matches, and the shell does not have to store the output from grep to use in a string comparison test.

Your code does not work since it contains syntax errors. The [ needs spaces around it.

Note also that -E enables extended regular expressions with some implementations of grep. In your case here, this is not needed as the expression is a basic regular expression.

Kusalananda
  • 333,661