我创建了一个 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/