【问题标题】:Repeat a specific code line in bash在 bash 中重复特定的代码行
【发布时间】:2017-05-18 21:52:16
【问题描述】:

我编写了一个简单的带有对话框的 bash 脚本,用于在 deepin 发行版上进行后期安装。该脚本有一个使用对话框创建的简单菜单,现在我的问题是我不知道如何在每个命令完成后重复菜单。

这是脚本的一部分:

sudo apt-get install dialog
 cmd=(dialog --separate-output --checklist "Seleziona i programmi che vuoi installare:" 22 76 16)
 options=(1 "Impostazione Mirror GARR" off # qualsiasi impostazione può essere impostata su "on"
 2 "Aggiornamento di sistema" off
 3 "Installazione font Microsoft" off
 4 "Installazione Gdebi" off
 5 "Synaptic" off
 6 "BleachBit" off
 7 "Open JDK 8" off
 8 "Supporto lettura DVD" off
 9 "LibreOffice" off
 10 "VLC Media Player" off
 11 "Flash Player" off
 12 "Google Chrome" off
 13 "Teamiewer" off
 14 "Skype" off
 15 "Brasero" off
 16 "iFuse per supporto device Apple" off
 17 "Kodi" off
 18 "Gimp" off
 19 "Telegram" off 
 20 "Enpass Password manager" off
 21 "Opera Browser" off
 22 "GUFW" off
 23 "Vivaldi Browser" off
 24 "Risparmio energetico TLP" off
 25 "Pulizia del sistema" off)
 choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
 clear
 for choice in $choices
 do
 case $choice in

 1)
 #Setting mirror on italian server GARR
  echo "Impostazione Mirror GARR"
 cd $HOME &
cp /etc/apt/sources.list backup.sources.list
bash -c 'cat << EOF > /etc/apt/sources.list
# Generated by deepin-installer
# deb [by-hash=force] http://packages.deepin.com/deepin unstable main contrib non-free
# deb-src http://packages.deepin.com/deepin unstable main contrib non-free
##########################################################################################
deb [by-hash=force] http://ba.mirror.garr.it/mirrors/deepin/ panda main contrib non-free
EOF'
      sudo apt-get update
 ;;

2)
 #Update of the repo and upgrade the system
clear
  echo "Aggiornamento del sistema"
apt-get update && sudo apt-get upgrade -y
 ;;

【问题讨论】:

  • 添加一个选项26 Exit 并使用覆盖整个脚本的while循环。
  • 是的,但是现在执行命令后,bash 脚本会退出。脚本的每个命令都可以使用 while 命令。

标签: linux bash shell debian-based


【解决方案1】:

这个简单的测试似乎运行良好。试试看:

$ cmd=(dialog --separate-output --checklist "Seleziona i programmi che vuoi installare:" 22 76 16)
$ options=(1 "Impostazione Mirror GARR" off 2 "Aggiornamento di sistema" off 3 "Installazione font Microsoft" off 4 "Exit" off)
$ while [ "$choices" -ne "4" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done

【讨论】:

  • 我认为需要while [[ "$choices" -ne "4" ]] 才能避免-bash: [: : integer expression expected,但很好的答案+
猜你喜欢
  • 1970-01-01
  • 2016-04-12
  • 2022-12-12
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 2011-03-24
相关资源
最近更新 更多