0

I am very new to bash programming and wanted to create a script that would store each result of find individually into an array. Now I want the command variable to expand on the statement MYRA=($(${Command} $1))

Command = 'find . -iname "*.cpp" -o -iname "*.h"'
declare -a MYRA
    MYRA=($(${Command} $1))
echo ${#MYRA[@]} 

However when I try this script I get the result

$ sh script.sh
script.sh: line 1: Command: command not found
0

Any suggestions on how I can fix this ?

4
  • 1
    First, you may want to include the "shell" you expect this to run as/under, such as #!/bin/bash. I don't know if I'd use "Command" as an identifier, but I do question if you are using a single quote ' versus a single open quote `, as the open quote says to "run this as a command" while a single quote will get you the command you wanted to run as a "string" to put in your array. Commented Jun 3, 2015 at 17:47
  • See mywiki.wooledge.org/BashFAQ/050 to discuss safely storing shell commands in variables. Commented Jun 3, 2015 at 17:59
  • Also, array=( $(find ...) ) fails miserably with filenames that contain whitespace, glob characters, or are in any other way interesting. Commented Jun 3, 2015 at 18:05
  • BTW, feel free to ask follow-up questions; some of the other tricks I used in my answer (such as reading from a NUL-delimited stream -- since NUL is the only character that can't exist in a UNIX pathname) are documented somewhere other than FAQ 50; that particular one, for instance, is touched in BashFAQ #1 (mywiki.wooledge.org/BashFAQ/001). Commented Jun 3, 2015 at 18:24

2 Answers 2

3

Shell assignment statements may not have whitespace around the =. This is valid:

Command='some command'

This is not:

Command = 'some command'

In the second form, bash will interpret Command as a command name.

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

1 Comment

Thanks for clearing that up however now I am getting the array size to be zero however if I replace ${Command} with the actual command it works then
1

All of the below requires a #!/bin/bash shebang (which should come as no surprise since you're using arrays, which are a bash-only feature).

Also, see http://mywiki.wooledge.org/BashFAQ/050 for comprehensive discussion.


A best-practices implementation would look something like this:

# commands should be encapsulated in functions where possible
find_sources() { find . '(' -iname '*.cpp' -o -iname '*.h' ')' -print0; }

declare -a source_files
while IFS= read -r -d '' filename; do
  source_files+=( "filename" )
done < <(find_sources)

Now, if you really need to store the command in an array (maybe you're building it up dynamically), doing that would look like this:

# store literal argv for find command in array
# ...if you wanted to build this up dynamically, you could do so.
find_command=( find . '(' -iname '*.cpp' -o -iname '*.h' ')' -print0 )

declare -a source_files
while IFS= read -r -d '' filename; do
  source_files+=( "filename" )
done < <("${find_command[@]}")

Comments

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.