简体   繁体   English

如何使用循环变量响应 shell 脚本中的提示

[英]How to respond to prompt in shell script with loop variable

I have a cli command to delete an entity like admin delete <<entity>> .我有一个 cli 命令来删除像admin delete <<entity>>这样的实体。

Now, my usecase is to delete a bunch of entities with entity names in a file.现在,我的用例是删除文件中具有实体名称的一堆实体。 I can just iterate over the file and delete these entities like:我可以遍历文件并删除这些实体,例如:

cat file.txt | while read entity 
do
   admin delete $entity
done 

The catch is that the admin delete <<entity>> command asks for a confirmation of the name of the entity before deleting the entity like To proceed, type the full name of the entity again here .问题是admin delete <<entity>>命令在删除实体之前要求确认实体名称,如To proceed, type the full name of the entity again here

How do I feed in the loop variable as the response to this prompt during the shell script execution for each entity?在为每个实体执行 shell 脚本期间,如何输入循环变量作为对此提示的响应?

A simple go-around is using a here document .一个简单的解决方法是使用here document

admin delete $entity <<< $entity

For comparision:比较:

fn() { local x && read x; [ "$x" = "$1" ] && echo yep || echo nope; }

run as运行为

var=smthg
foo=smthg
fn $var <<< $foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM