0

I am trying to write a script then search for all the directories with the given substring but I am getting the following error.

I have no idea why this is happening.. can someone please guide me ?

#!/bin/bash echo $1 for recordings in $1/*/ ; do echo $recordings | grep -q "XavierA" echo $? if ["$?" -eq 0];then echo "found." fi done 

The output is this

0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 0 ./readallfiles.sh: line 7: [0: command not found 1 ./readallfiles.sh: line 7: [0: command not found 
1
  • Note that running your code through shellcheck.net will identify this and other bugs automatically. $1/*/ should be "$1"/*/ to work right with directory names with spaces and glob characters; and the remaining lines would be better written as if [[ $recordings = *XavierA* ]]; then echo "found"; fi -- though you also just have your whole glob search for "$1"/*XavierA*/ and avoid the need for a loop altogether. See BashFAQ #4 re: best practices for counting how many directory entries match a pattern. Commented Mar 24, 2020 at 17:17

1 Answer 1

1

"[" isn't a punctuation mark, it's a program. Put a space after it, and before the "]".

The error message shows the shell has expanded the "$?" to 0, but it's adjacent to the [ so the shell is looking for an executable "[0". Naturally, it doesn't find one.

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

1 Comment

In How to Answer, see the section "Answer Well-Asked Questions", and therein the bullet point regarding questions that have "been asked and answered many times before".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.