我有一个变量,我希望将其传递给命令。然而,$产生了不寻常的结果。它看起来像一个*并搜索所有目录。我的代码是:
"Please enter the test type: "
read test
result="pass"
search_result=`find ./logs/$test_2017_01_logs*/* -type f -name func_log -exec egrep $result {} \;`
echo "$search_result"
我只想搜索用户读入的目录后缀,而不是我的代码正在执行的所有目录。
问题是由于传入值($ test)和文件夹名称的其余部分(_2017_01_logs *)串联而导致的吗?
答案 0 :(得分:2)
_
是一个“单词字符”([a-zA-Z0-9_]
),被理解为您要处理的变量的一部分,因此是未定义的$test_2017_01_logs
。
为避免这种情况,您可以将变量名括在大括号中:
find ./logs/"${test}_2017_01_logs"*/* -type f -name func_log -exec egrep $result {} \;
或者,如果我们遵循Charles Duffy的明智建议:
find ./logs/"$test"_2017_01_logs*/* -type f -name func_log -exec egrep -h -e "$result" /dev/null {} +