我正在尝试使用 shell 脚本中的函数调用来执行命令。当我将命令作为该函数的参数传递给该函数时,它不起作用。
函数定义:
function ExecuteCommand() (
# $1: User@Host
# $2: Password
# $3: Command to execute
# Collect current IFS value
OLD_IFS=$IFS
# Set IFS value to new line feed
IFS=$'\n'
#Execute the command and capture the output
EXPECT_OUTPUT=($(expect ssh_exec.expect $1 $2 $3))
#Print the output
OUTPUT_LINE_COUNT=${#EXPECT_OUTPUT[@]}
for ((OUTPUT_LINE_INDEX=0; OUTPUT_LINE_INDEX<OUTPUT_LINE_COUNT; OUTPUT_LINE_INDEX++)) ;
do
echo ${EXPECT_OUTPUT[$OUTPUT_LINE_INDEX]}
done
# Get back to the original IFS
IFS=$OLD_IFS
)
函数调用:
ExecuteCommand oracle@192.168.***.*** password123 "srvctl status database -d mydb"
我得到的输出是:
spawn ssh oracle@192.168.***.*** {srvctl status database -d mydb}
oracle@192.168.***.***'s password:
bash: {srvctl: command not found
但是当我不将命令作为函数的参数传递时,它可以完美地工作:
这种情况下的函数定义:
function ExecuteCommand() (
# $1: User@Host
# $2: Password
# Collect current IFS value
OLD_IFS=$IFS
# Set IFS value to new line feed
IFS=$'\n'
#Execute the command and capture the output
EXPECT_OUTPUT=($(expect ssh_exec.expect $1 $2 srvctl status database -d mydb))
#Print the output
OUTPUT_LINE_COUNT=${#EXPECT_OUTPUT[@]}
for ((OUTPUT_LINE_INDEX=0; OUTPUT_LINE_INDEX<OUTPUT_LINE_COUNT; OUTPUT_LINE_INDEX++)) ;
do
echo ${EXPECT_OUTPUT[$OUTPUT_LINE_INDEX]}
done
# Get back to the original IFS
IFS=$OLD_IFS
)
函数调用:
ExecuteCommand oracle@192.168.***.*** password123
我得到了我预期的输出:
spawn ssh oracle@192.168.***.*** srvctl status database -d mydb
oracle@192.168.***.***'s password:
Instance mydb1 is running on node mydb1
Instance mydb2 is running on node mydb2
Instance mydb3 is running on node mydb3
请帮助我了解在第一种情况下将命令作为函数参数传递时出了什么问题。