原始帖子已被删除,因为尽管 bash/dash 问题已解决,但主要备份目标尚未实现/解决:
目标:按编号指定分区,以便在提示时按顺序执行由逗号分隔的输入指定的多项选择备份(解析多项选择的输入),仅输入一次身份验证密码。
示例:将 ubuntu 指定为“1”,将 android 指定为“2”,将 windows 指定为“3”。在输入提示符下选择 ubuntu 和 windows 进行备份,如下所示:1,3。脚本应解析多个输入并执行 ubuntu 和 windows 的备份。
在输入提示中选择 ubuntu 和 android 和 windows,如下所示:1,2,3。解析输入并执行 ubuntu、android 和 windows 的备份。
命令行(此处省略)将指定备份指令。
在提示符下运行脚本:
'/backup.sh'
- Ubuntu
- 安卓
- 视窗
- 退出选择备份:
(选择要解析的逗号分隔的数字并按顺序执行备份)
剩下的问题是,如何修改才能达到先前提出的目标?
#!/bin/bash
PS3='Choose backup:'
backups=( "ubuntu" "android" "windows" "Quit" )
select pick in "${backups[@]}"; do
case $pick in
"ubuntu")
echo "backing up $pick"
sudo fsck -y -f /dev/sda2 ;
sleep 1 ;
#sudo (commandline ommitted) ;
;;
"android")
echo "backing up $pick"
sudo fsck -y -f /dev/sda3 ;
sleep 1 ;
#sudo (commandline ommitted) ;
;;
"windows")
echo "backing up $pick"
sudo fsck -y -f /dev/sda4 ;
sleep 1 ;
#sudo (commandline ommitted) ;
;;
"Quit")
echo "User requested exit"
exit
;;
*) echo "invalid option $REPLY";;
esac
done
或者,可以将输入存储在数组中并按如下方式执行备份:
#!/bin/bash
IFS=$'\t' read -p "Execute Backup (COMMA Separated): " -a WHAT
for (how to reference array element 0?) "${WHAT[@]}"
do
sudo (backup command line here)
for (how to reference array element 2?) "${WHAT[@]}"
do
sudo (backup command line here)
for (how to reference array element 3?) "${WHAT[@]}"
do
sudo (backup command line here)
done
如何调用数组中的元素以及分隔各个 for---do 备份例程的语法是什么?
答案1
这不是在做你的家庭作业,而是让你学习更多的东西:)...请记住,以下示例远非完美,仅用于让你开始学习,所以请做好你的家庭作业。
示例 1
这是一个指向文本通配符和非短路case
运算;;&
符bash
:
echo -e "1) Ubuntu\n2) Android\n3) Windows\n4) quit"
read -r -p "Execute Backup (COMMA Separated): " pick
case "${pick}" in
*1*)
echo "backing up Ubuntu ..."
;;&
*2*)
echo "backing up Android ..."
;;&
*3*)
echo "backing up Windows ..."
;;
"4")
echo "User requested exit"
exit
;;
*[^1-3]) echo "invalid option ${pick}";;
esac
示范:
1) Ubuntu
2) Android
3) Windows
4) quit
Execute Backup (COMMA Separated): 1,2,3
backing up Ubuntu ...
backing up Android ...
backing up Windows ...
示例 2
你可以读用户输入数组然后在 shell 中处理数组元素for
环形并将类似的任务/工作归类为函数在某个时间点轻松调用某些任务,例如:
#!/bin/bash
# Create a function to show the menue and read the user comma delimited input into an array
function menu {
echo -e "1) Ubuntu\n2) Android\n3) Windows\n4) quit"
IFS=',' read -r -p "Execute Backup (COMMA Separated): " -a pick
}
# Create a function to process the user input
function process {
for element in "${pick[@]}"
do
# Check for invalid input using a RegEx pattern
if [[ "${pick[*]}" =~ [^(123 )] ]]
then
# If array has only one element "4"
if [ "${#pick[@]}" = 1 ] && [ "$element" = "4" ]
then
echo "User requested exit"
exit
else
echo "Invalid input: ${pick[*]}"
main
break
fi
elif [ "$element" = "1" ]
then
echo "backing up Ubuntu ..."
elif [ "$element" = "2" ]
then
echo "backing up Android ..."
elif [ "$element" = "3" ]
then
echo "backing up Windows ..."
fi
done
}
# Create the main function to call the above two
function main { menu; process; }
# call the function "main"
main
示范:
1) Ubuntu
2) Android
3) Windows
4) quit
Execute Backup (COMMA Separated): 1,2,3
backing up Ubuntu ...
backing up Android ...
backing up Windows ...