Cygwin、MSYS2 Bash封装函数restart,重启Windows程序进程向导,输入序号一键重启对应进程或软件

发布时间 2023-04-29 16:11:22作者: 晴云孤魂

概述

作用:输入restart,根据菜单提示输入序号重启特定的软件或进程,定义的重启函数太多不便于记忆的情况,特别是手机远程终端(如:JuiceSSH)连接的情况下,减少输入和误操作,其中判定浏览器进程使用了另一篇文章中预定的函数wmicps,ps2为指向wmicps的alias ( 详见:https://www.cnblogs.com/cnhack/p/15937835.html ),

restart函数封装代码

restart() {
	#智能判断当前系统各进程运行状态,交互式提示用户选择要重启的软件或进程!
	#注:传递参数或管道数据可以转化为非交互式操作!
	#1、可通过$*传递序号来直接选择操作;
	#	eg:restart 1 6 7   或   restart restart-chrome  1 6 7  ($1为非数字参数时绕过浏览器进程检测)
	#2、可通过标准输出管道传递序号来选择操作;
	#	eg:restart <<<"1"  或   restart <<<"1 6 7"    (可传递多个序号批量执行)
	#3、【案例】:不管当前运行哪一个浏览器,自动检测并重启浏览器:
	#   eg:restart 1 或  restart <<<1
	#exec 6>&1 1>&-
	local browserAction=""
	declare -a selectAction  #定义数组存储多个要执行的动作
	
	#指定了$1参数,且$1为非数字选项时,认定$1为要执行的浏览器重启动作(此时不再动态检测浏览器运行清空,较为节省时间)
	if [ ! -z "$1" ] && ! expr "$1" + 0 &>/dev/null;then
		browserAction="$1"
		shift
	else
		local browser=$(ps2 360ChromeX.exe --nopath &>/dev/null && echo "360ChromeX.exe 360极速浏览器X版 restart-360chromex" && exit \
			||ps2 chrome.exe --nopath &>/dev/null && echo "chrome.exe 谷歌或Cent浏览器 restart-chrome" && exit \
			||ps2 360chrome.exe --nopath &>/dev/null && echo "360chrome.exe 360极速浏览器(旧版) restart-360chrome" && exit \
			||ps2 SogouExplorer.exe --nopath &>/dev/null && echo "SogouExplorer.exe 搜狗浏览器 restart-sogouexplorer" && exit \
			||echo "None 没有浏览器在运行" >/dev/null)   #<--判断当前系统哪一款浏览器在运行?
		#exec 1>&6 6>&-
		#echo "运行的浏览器:$browser"  ##<---Test Get Dynamic WebBrowser
		[ ! -z "$browser" ] && browserAction=$(echo "$browser"|awk '{print $NF;exit}')
	fi
	local actionList=$(cat <<EOF
${browserAction}
restart-sunlogin
restart-interactive-sshd
restart-cygwin-sshd
restart-explorer
restart-winmedia
restart-winshare
restart-graphics
restart-mysqld
restart-httpd
restart-phpfpm
restart-msedge
restart-excel
restart-zerotier
restart-tailscale
restart-tplink
restart-wcc
restart-winaudio
restart-proxifier
restart-all-frpc
restart-allone
EOF
)
	print_color 40 "交互式重启程序向导:"
	echo "$actionList"|awk '{printf "%2d):%s\n",NR,$0}'
	if [ $# -gt 0 -a -t 0 ];then #有数字序号,同时没有管道数据传入时;
		restart "$browserAction" <<<"$@"
		return
	fi
	while :;
	do
		print_color 40 "请输入序号选择要执行操作,支持多选,多个序号用空格隔开(0 或 q 退出操作):"
		read -p "> " doSelect
		if [[ "${doSelect,,}" == "0" || "${doSelect,,}" == "q" ]];then
			print_color 40 "用户取消,退出操作..."
			break
		elif [ -z "$doSelect" ];then
			print_color 40 "选择为空,退出操作..."
			break
		else
			mapfile -t -d $' ' selectArr <<<$(echo "$doSelect")
			for selectItem in ${selectArr[@]}
			do
				local _action=$(echo "$actionList"|awk 'NR=='"${selectItem}"'{print;exit}' 2>/dev/null)
				if [ -z "$_action" ];then
					print_color 40 "选项 “${selectItem}” 无效,请重新选择!" 
					selectAction=()  #如果有选错的选项,清空已存储的动作列表,让用户重新选择
					break  
				else
					selectAction[${#selectAction[@]}]="$_action"
				fi
			done
			[ ${#selectAction[@]} -gt 0 ] && break
		fi
	done
	[ -z "${selectAction[*]}" ] && return
	for doAction in "${selectAction[@]}"
	do
		echo "执行的操作:=> $doAction ..."
		#eval "$doAction"   #使用此方式重复运行函数时会导致参数堆积,导致重启过程执行两次,比如重复多次运行 `restart2`输入1,多次重复此步骤即可重现Bug!
		( eval "$doAction" )  #使用子进程执行重启命令,防止反复运行函数时导致的参数堆积!
	done
}
alias restart2='restart restart-chrome'  #认定运行的浏览器为谷歌或Cent浏览器,不再动态检测

使用说明

直接终端使用 restart 命令即可,根据菜单提示输入相应序号重启软件进程,缺省参数下,程序会检测当前系统运行哪一款浏览器从而决定第一个选项是啥,$1也可以指定预定义的函数来跳过浏览器进程检测(此时的$1不为纯数字);
$1或$2(存在$1且为约定义函数时,使用$2作为选择序号)可传递纯数字来替代手动输入序号的过程,将操作过程转成非交互式进行(适用于自动化脚本,不需要用户交互的情况)。

使用截图

其他相关参考资料:

https://www.cnblogs.com/cnhack/p/15937835.html
https://www.cnblogs.com/cnhack/p/15970116.html