gpt4 book ai didi

linux - 将自动输入发送到 bash 中另一个脚本调用的脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:40:57 24 4
gpt4 key购买 nike

我正在编写一个 bash 脚本 (my_script),我在其中调用了很多脚本,它们共同实现了工作流程的自动化。

但是当我调用一个特定的 (ksh/bash) 脚本 (master_script) 时,其中有很多输入和检查(不是参数)。

它正在减慢整个自动化,因为每次我都必须 super 智能并手动输入值。

我没有修改或制作新脚本的选项(工作限制)

每次的问题都是一样的。我试图在执行 master_script 之前获取除一个答案(其值取决于执行)之外的所有答案,然后在正确的时间将其提供给 master_script。

有没有办法在 my_script 执行期间将值传递给 master_script。? ./master_script<< EOF .. EOF 无济于事,因为我必须自己输入一个答案。

下面只是一个示例和我的创作,但描述的正是我的要求。

示例代码

我的脚本

#! /bin/bash
echo "Proceeding...."
#calling master_script
/master_script $arg1 $arg2
echo "Completed.."
echo "Executing other scripts"
/other_scripts"

执行

$ sh ./my_script
Proceeding....

Started master_script..

Press Enter to Proceed MY_INPUT

Enter username to add (eg.user123) MY_UNAME

Enter preferred uid (eg.1234) MY_UID

Do you want to bla bla..(Y/n) MY_INPUT

Please select among the following
1.option1
2.Option2

Selection: MY_SELECTION

Please choose which extension to use
1.ext1
2.ext2
3.ext3
4.ext4

Do you want to bla bla 2..(Y/n) MY_INPUT

Ended master script
Completed..

Executing other scripts

要求

#! /bin/bash
echo "Proceeding...."
# get values for master script
read -p "Proceed(Y/n):" proceed1
read -p "Uname:" uname
read -p "Uid:" uid
read -p "bla bla (Y/n):" bla1
read -p "Selection(1/2):" selection1
read -p "bla bla 2(Y/n):" bla2
#calling master_script
./master_script $arg1 $arg2 {all_inputs}
#Silent Execution of master_script until choosing execution...
Please choose which extension to use
1. ext1
2. ext2
3. ext3
4. ext4
#Silent Execution of master_script after choosing ext and continue with other scripts
./other_scripts
echo "Completed.."

I've read about expect/send combination, but I'm unable to comprehend how to use it. Any inputs will be greatly helpful

编辑

I am also not sure about ./master_script<< EOF ... EOF as I have to enter one answer in the middle of execution myself.

最佳答案

有一个使用here documents 并重定向输入的解决方案:

./master_script "$arg1" "$arg2" << ENDINPUT
$proceed1
$uname
$uid
$bla1
$selection1
ENDINPUT

备注1:最后的ENDINPUT必须起行,不要缩进!参见 Man bash

备注 2:某些脚本或程序会检查输入是否来自实际终端(调用 isatty()),例如在输入密码时。仍然可以自动输入条目,但要棘手得多。

关于linux - 将自动输入发送到 bash 中另一个脚本调用的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42845223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com