在 Bash 脚本中使用 Tcl Expect 脚本打印数组
Prints out array using Tcl Expect script within Bash script
希望了解 Tcl、Expect 和 Bash 的人能够指导我进行以下操作:
我正在尝试使用 for 循环和数组 打印出每一行的用户输入,直到使用 CTRL-D 键盘快捷键直到 EOF(文件末尾)。
然而,结果并不像预期的那样,我不确定第 22 行的语法应该是什么,因为数组索引似乎没有注册变量 i
输入“用户:${inputs_array[$i]}”
print_inputs.sh
#!/bin/bash
# While Loop to get user inputs
echo '======For User Inputs======'
while read line
do
if [ "${line}" == '' ]
then
continue
else
inputs_array=("${inputs_array[@]}" "$line")
fi
done
# Prints out using Tcl script
echo '======Prints out======'
{
/usr/bin/expect << EOF
for {set i 0} {$i < ${#inputs_array[@]}} {incr i} {
puts "User: ${inputs_array[$i]}"
}
EOF
}
结果:
======For User Inputs======
Sad
Happy
======Prints out======
User: Sad
User: Sad
下面是我目前使用的bash和Tcl版本:
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
% puts $tcl_version
8.5
提前致谢!
考虑 Donal 的建议:这不需要成为一个混合语言程序。
然而:
首先,使用 bash 的 readarray
命令将 行输入 读入数组:替换 bash while
使用这个命令循环:
readarray -t inputs_array
注意:这不会像您在数组中那样过滤掉空行。
接下来,expect 无法访问 shell 变量。您需要通过环境共享变量。但是,环境变量不能是数组。我建议这样做:
export array_contents
array_contents=$(printf '%s\n' "${inputs_array[@]}")
# Prints out using Tcl script
echo '======Prints out======'
# Note the heredoc word is quoted.
/usr/bin/expect << 'EOF'
set inputs [split $env(array_contents) \n]
foreach input $inputs {
puts "User: $input"
}
EOF
经过这些更改,输出看起来像
$ bash print_inputs.sh
======For User Inputs======
foo
bar
baz
======Prints out======
User: foo
User: bar
User: baz
或者,使用编程输入
$ { echo foo; echo bar; echo baz; } | bash print_inputs.sh
======For User Inputs======
======Prints out======
User: foo
User: bar
User: baz
希望了解 Tcl、Expect 和 Bash 的人能够指导我进行以下操作:
我正在尝试使用 for 循环和数组 打印出每一行的用户输入,直到使用 CTRL-D 键盘快捷键直到 EOF(文件末尾)。 然而,结果并不像预期的那样,我不确定第 22 行的语法应该是什么,因为数组索引似乎没有注册变量 i
输入“用户:${inputs_array[$i]}”
print_inputs.sh
#!/bin/bash
# While Loop to get user inputs
echo '======For User Inputs======'
while read line
do
if [ "${line}" == '' ]
then
continue
else
inputs_array=("${inputs_array[@]}" "$line")
fi
done
# Prints out using Tcl script
echo '======Prints out======'
{
/usr/bin/expect << EOF
for {set i 0} {$i < ${#inputs_array[@]}} {incr i} {
puts "User: ${inputs_array[$i]}"
}
EOF
}
结果:
======For User Inputs======
Sad
Happy
======Prints out======
User: Sad
User: Sad
下面是我目前使用的bash和Tcl版本:
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
% puts $tcl_version
8.5
提前致谢!
考虑 Donal 的建议:这不需要成为一个混合语言程序。 然而:
首先,使用 bash 的 readarray
命令将 行输入 读入数组:替换 bash while
使用这个命令循环:
readarray -t inputs_array
注意:这不会像您在数组中那样过滤掉空行。
接下来,expect 无法访问 shell 变量。您需要通过环境共享变量。但是,环境变量不能是数组。我建议这样做:
export array_contents
array_contents=$(printf '%s\n' "${inputs_array[@]}")
# Prints out using Tcl script
echo '======Prints out======'
# Note the heredoc word is quoted.
/usr/bin/expect << 'EOF'
set inputs [split $env(array_contents) \n]
foreach input $inputs {
puts "User: $input"
}
EOF
经过这些更改,输出看起来像
$ bash print_inputs.sh
======For User Inputs======
foo
bar
baz
======Prints out======
User: foo
User: bar
User: baz
或者,使用编程输入
$ { echo foo; echo bar; echo baz; } | bash print_inputs.sh
======For User Inputs======
======Prints out======
User: foo
User: bar
User: baz