linux - 用于多重选择和命令输入的 Bash 脚本

标签 linux bash select rar

我创建了一个 bash 脚本,用于选择要插入到 RAR 命令中的文件/文件夹:

#!/bin/bash

prompt="Please select a file:"
options=( $(find -maxdepth 1 -print0 | xargs -0) )

PS3="$prompt "
select opt in "${options[@]}" "Quit" ; do 
if (( REPLY == 1 + ${#options[@]} )) ; then
    exit

elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
   echo  "You picked $opt which is folder $REPLY"
    echo -e "What do you want to name your archive?"
    read archivename
    rar a $archivename.rar $opt
    break

else
    echo "Invalid option. Try another one."
fi
done    

因此,这会创建一个包含我运行脚本的所有文件/文件夹的列表,然后将我的选择放入“RAR”命令中以 rar 选定的文件或文件夹。

我希望能够运行该脚本并选择多个文件和文件夹以输入到 rar 命令中。这些文件可以用于连续命令或相同命令...IE:

rar a $archivename.rar $opt1 $opt2 $opt3 $opt4

或者

rar a $archivename.rar $opt
rar a $archivename1.rar $opt1
rar a $archivename2.rar $opt2

第二个示例显示将按顺序运行的命令。

感谢任何帮助!谢谢!

最佳答案

I would like to be able to run the script and select multiple files and folders for input in to the rar command.

您可以为此使用对话框 list 。示例(假定 ${options[@]} 包含可供选择的所有文件/文件夹的列表):

prompt="Please select files:"
set -- "${options[@]/#/\"}"; set -- "${@/%/\" \"\" \"\"}"
read -a opt < <(eval dialog --checklist '"$prompt"' 0 0 0 $@ 2>&1 >/dev/tty)
[ "$opt" ] || exit
eval opt=(${opt[@]})    # parse the quotes

set 命令在文件名两边加上引号,以便它也适用于包含空格的名称。选定的名称放入${opt[@]}中。然后你就可以

rar a $archivename.rar "${opt[@]}"

rar a $archivename.rar "${opt[0]}"
rar a $archivename.rar "${opt[1]}"
rar a $archivename.rar "${opt[2]}"

关于linux - 用于多重选择和命令输入的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32429202/

相关文章:

c - 在 Solaris 中用字符串中的前导零填充有效,但在 RHEL 中无效

linux - 无法找到包色调

excel - 尝试在 Linux 中将 Microsoft.Office.Interop.Excel 与 VB.NET 结合使用

arrays - 从 bash 中的输入文件中读取数组

mysql - 从连接表中选择最后一个值和总数

php - &lt;textarea&gt;和MYSQL在php中显示数据

c - 在汇编函数中获取字符指针

linux - 如何将标准错误流输出分配给bash中的变量?

linux - 在 Shell 中读取多行用户输入

mysql - 如何从 MySQL 中的 SELECT 查询中选择一条记录