我正在编写一个脚本,该脚本在大量AWS账户中搜索IP地址。我根据用户的输入设置jq命令。
我得到的错误是:
./aws_utils.sh: line 1434: jq -r '.Reservations[].Instances[].PrivateIpAddress': command not found
我正在尝试使用以下代码设置用于设置jq命令的变量:
echo "Find IP Address in AWS"
echo "Search for Public or Private IP Addresses?"
echo "Enter public/private:"
read -r search_scope
if [[ "$search_scope" = "public" ]]; then
set_search="jq -r '.Reservations[].Instances[].PublicIpAddress'"
elif [[ "$search_scope" = "private" ]]; then
set_search="jq -r '.Reservations[].Instances[].PrivateIpAddress'"
else
echo "Invalid Input"
fi
脚本运行时,此行代码是发生错误的时间:
search_result=$(aws ec2 describe-instances --profile="$aws_env" | "$set_search" | /bin/grep -v null | /bin/grep \"$ip_address\")
如果我在命令行上运行相同的命令,则不会收到错误消息:
aws ec2 describe-instances --profile=nonprod | jq -r '.Reservations[].Instances[].PrivateIpAddress' | /bin/grep -v null | /bin/grep 10.48.167.228
10.48.167.228
为什么jq命令在脚本中产生命令未找到错误?
答案 0 :(得分:1)
如果要存储代码,请将其存储在函数中。您可以有条件地定义它们:
consumer.subscribe(new ArrayList<String>() {{"test1","test2"}})
...以及更高版本:
if [[ "$search_scope" = "public" ]]; then
search_json() { jq -r '.Reservations[].Instances[].PublicIpAddress' "$@"; }
elif [[ "$search_scope" = "private" ]]; then
search_json() { jq -r '.Reservations[].Instances[].PrivateIpAddress' "$@"; }
else
echo "Invalid Input" >&2; exit 1;
fi