bash - 如何在 while 读取循环中以交互方式使用 rm?
问题描述
我很少陷入用于查找和删除某些文件的脚本中。我想让用户有机会遍历列表并仅在人工批准后删除每个文件。
但是,我发现脚本跳过用户交互并且不删除。
cat $fileToBeDeleted | while read in; do
rm -i "$in"
echo "deleted: $in"
done;
解决方案
rm -i
在删除文件之前,您可以使用 if 语句来请求确认,而不是使用。在此示例中,我使用了一个文本文件 (to_delete_list.txt),其中包含我将要删除的文件列表。
read -u 1 answer
将让您在循环中要求用户输入。
#!/bin/bash
while IFS= read -r file; do
echo "do you want to delete $file (y/n)?"
read -u 1 answer
if [[ $answer = y ]]
then
rm $file
echo "deleted: $file"
else
continue
fi
done < "to_delete_list.txt"
推荐阅读
- angularfire2 - Angular 6 和 AngularFire2 RxJS 错误
- sql - 原子替换 PostgreSQL 表中所有内容的“正确”方法?
- ios - Xamarin.ios:详细命令为空,因为它显示在函数之前
- javascript - 我可以总是假设不返回 Promise 或 Observable 的函数是 JavaScript 中的同步函数吗?
- google-chrome - 谷歌在谷歌浏览器中阻止了网站/域
- python - Python,将 jpg 文件添加到 zipfile,忽略现有(不要覆盖它)
- node.js - Google Maps Directions API:计算给定点是否在路线上
- java - 在单独的 IDE 中调试 flex 应用程序的 java 部分
- ruby-on-rails - Rails 5 - Shrine 问题:“nil:NilClass 的未定义方法‘cached_image_data’”
- ios - SceneKit move:to action在缩放动作后的一系列动作中不起作用