BASH script returning command not found -
i new bash programming , wanted create script store each result of find individually array. want command variable expand on statement myra=($(${command} $1))
command = 'find . -iname "*.cpp" -o -iname "*.h"' declare -a myra myra=($(${command} $1)) echo ${#myra[@]} however when try script result
$ sh script.sh script.sh: line 1: command: command not found 0 any suggestions on how can fix ?
all of below requires #!/bin/bash shebang (which should come no surprise since you're using arrays, bash-only feature).
also, see http://mywiki.wooledge.org/bashfaq/050 comprehensive discussion.
a best-practices implementation this:
# commands should encapsulated in functions possible find_sources() { find . '(' -iname '*.cpp' -o -iname '*.h' ')' -print0; } declare -a source_files while ifs= read -r -d '' filename; source_files+=( "filename" ) done < <(find_sources) now, if really need store command in array (maybe you're building dynamically), doing this:
# store literal argv find command in array # ...if wanted build dynamically, so. find_command=( find . '(' -iname '*.cpp' -o -iname '*.h' ')' -print0 ) declare -a source_files while ifs= read -r -d '' filename; source_files+=( "filename" ) done < <("${find_command[@]}")
Comments
Post a Comment