我有一堆图表保存为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 $ $