linux - 如何使用循环变量响应 shell 脚本中的提示
问题描述
我有一个 cli 命令来删除像 admin delete <<entity>>
.
现在,我的用例是删除文件中具有实体名称的一堆实体。我可以遍历文件并删除这些实体,例如:
cat file.txt | while read entity
do
admin delete $entity
done
问题是该admin delete <<entity>>
命令在删除实体之前要求确认实体名称,例如To proceed, type the full name of the entity again here
.
如何在每个实体的 shell 脚本执行期间输入循环变量作为对此提示的响应?
解决方案
一个简单的解决方法是使用here document
.
admin delete $entity <<< $entity
比较:
fn() { local x && read x; [ "$x" = "$1" ] && echo yep || echo nope; }
运行为
var=smthg
foo=smthg
fn $var <<< $foo
推荐阅读
- c++ - 模板参数的显式指定参数无效,即 constexpr
- android - 自定义视图在使用时产生数据绑定错误
- python - 如何从 API 调用访问数字数据?
- c# - C#:如何遍历自己的 Dictionary 以在我的 XDocument 中编写 XElements?
- java - 使用 dataBinding 在 recyclerView 中获取单击项目的位置
- java - 带有 Angular 的 Spring Boot。从源“http://localhost:4200”访问“http://localhost:8080/”的 XMLHttpRequest 已被 CORS 策略阻止
- python - xlsxwriter 格式特定单元格
- python - 如何绘制具有不同 colspans 的四个子图?
- r - 如何在ggplot函数中使用以数字开头的列名
- ruby-on-rails - RoR 回调 after_create