[英]BASH script returning command not found
我对bash编程非常陌生,想创建一个脚本,将每个find结果分别存储到数组中。 现在,我希望command
变量在语句MYRA=($(${Command} $1))
上扩展
Command = 'find . -iname "*.cpp" -o -iname "*.h"'
declare -a MYRA
MYRA=($(${Command} $1))
echo ${#MYRA[@]}
但是,当我尝试此脚本时,我得到了结果
$ sh script.sh
script.sh: line 1: Command: command not found
0
关于如何解决此问题的任何建议?
Shell赋值语句在=
周围不能有空格。 这是有效的:
Command='some command'
这不是:
Command = 'some command'
在第二种形式中,bash将Command
解释为命令名称。
以下所有内容都需要一个#!/bin/bash
shebang(这并不奇怪,因为您正在使用数组,这是仅bash的功能)。
另外,请参阅http://mywiki.wooledge.org/BashFAQ/050进行全面讨论。
最佳实践的实现如下所示:
# 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)
现在,如果您确实需要将命令存储在一个数组中(也许您正在动态地构建它),则应如下所示 :
# 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[@]}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.