微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一堆图表保存为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: " CHOICE -n 1 -s
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: " CHOICE -n 1 -s
d to delete, any other key to keep: d
read: 1: -n: bad variable name
$
$

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐