在Ubuntu中,可以从终端打开应用程序。但是有时不清楚执行此操作的适当命令是什么。
因此,打开应用程序后,如何获得用于启动它的命令,而不必在任何地方搜索(仅通过查看即可)?
我刚刚编写了以下脚本,该脚本使用应用程序窗口标题来找到正确的命令,该命令从终端(我将其命名为appcmd
)中打开相应的应用程序:
#!/bin/bash
#appcmd - script which use the application window title to find out the right command which opens the respective application from terminal
#Licensed under the standard MIT license:
#Copyright 2013 Radu Rădeanu (http://askubuntu.com/users/147044/).
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
#check if wmctrl is installed
if [ ! -n "$(dpkg -s wmctrl 2>/dev/null | grep 'Status: install ok installed')" ]; then
echo -e "The package 'wmctrl' must to be installed before to run $(basename $0).\nUse 'sudo apt-get install wmctrl' command to install it."
exit
fi
window_title=$(echo $@ | awk '{print tolower($0)}')
windows=$(mktemp)
pids=$(mktemp)
pid_found=""
wmctrl -l | awk '{$2=$3=""; print $0}' > $windows
cat $windows | while read identity window; do
if [[ $(echo $window | awk '{print tolower($0)}') == *$window_title* ]]; then
wmctrl -lp | grep -e "$identity.*$window" | awk '{$1=$2=$4=""; print $0}'
fi
done > $pids
while read pid window; do
if [ "$pid" != "0" -a "$window" != "Desktop" ]; then
echo -e "Application window title:\t$window"
echo -e "Command to open from terminal:\t\$ $(ps -o command $pid | tail -n 1)\n"
pid_found="$pid"
fi
done < $pids
if [ "$pid_found" = "" ]; then
echo "There is no any opened application containing '$@' in the window title."
fi
将此脚本保存在~/bin
目录中,不要忘记使它可执行:
chmod +x ~/bin/appcmd
用法:
当脚本运行时不带任何参数时,该脚本将返回与所有打开的窗口相对应的所有命令。
如果给出任何参数,脚本将尝试找到一个打开的应用程序窗口,该窗口的标题中包含该参数,并将返回相应的命令。例如,如果打开了Chromium浏览器,则可以仅使用以下命令从终端中找到打开该浏览器的命令:
appcmd chromium
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句