加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 查看并选择要从列表中删除的文件

发布时间:2020-12-13 22:48:19 所属栏目:Linux 来源:网络整理
导读:我有一堆图表保存为png文件.其中大多数都不是很有用,但有些非常有用. 我想编写一个脚本,一次显示每个脚本并等待我按下y或n.如果我点击n,删除它.如果没有,请转到下一个. 我遇到了两个问题. 首先,feh打开一个新窗口,所以我必须使用alt-tab返回我的shell才能按y
我有一堆图表保存为png文件.其中大多数都不是很有用,但有些非常有用.

我想编写一个脚本,一次显示每个脚本并等待我按下y或n.如果我点击n,删除它.如果没有,请转到下一个.

我遇到了两个问题.

首先,feh打开一个新窗口,所以我必须使用alt-tab返回我的shell才能按y或n.是否可以让bash监听任何按键,包括不同窗口中的按键?

其次,我试图使用read来监听一个字符,但它说-n不是一个有效的选项.尽管如此,同样的线在终端中工作正常.

知道怎么做吗?
感谢帮助.

#! /bin/sh                                                                                                                                                               

FILES=./*.png
echo $FILES
for FILE in $FILES
do
    echo $FILE
    feh "$FILE" &
    CHOICE="none"
    read -p "d to delete,any other key to keep: " CHOICE -n 1 -s
    killall feh
    if [$CHOICE -eq "d"]
    then
        rm $FILE
    fi
done

解决方法

这可能只与您的问题相关,但您的脚本设置为使用/ bin / sh(POSIX shell)执行,但您可能使用/ bin / bash作为交互式终端shell.读取句柄取决于您使用的shell:

这是你的一个读命令输出三个不同的shell; dash shell用于我的系统上的/ bin / sh,但为了确保它在调用sh和dash时处理相同的内容,我运行了两次,每个名称一次:

$bash
$read -p "d to delete,any other key to keep: " CHOICE -n 1 -s
d to delete,any other key to keep: d
bash: read: `-n': not a valid identifier
$exit
$dash
$read -p "d to delete,any other key to keep: d
read: 1: -n: bad variable name
$
$pdksh
$read -p "d to delete,any other key to keep: " CHOICE -n 1 -s
pdksh: read: -p: no coprocess
$$sh
$read -p "d to delete,any other key to keep: d
read: 1: -n: bad variable name
$
$

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读