如何从脚本中启动默认 Web 浏览器?在下面的脚本中,当脚本作为可执行 shell 脚本成功运行时,浏览器就会打开:
#!/bin/bash
cd $1
php -S 127.0.0.1:5000
for f in *.html; do cp -- "$f" "${f%.html}.php"; done
x-www-browser http://127.0.0.1:5000/index.php
但是,在这个带有 Kdialog UI 的较长版本中,除了启动浏览器之外,一切都正确执行(文件已创建并且服务器在所选目录中启动)
#!/bin/bash
`kdialog --yesno "HTML Files created, make PHP?"`
if [ $? = 1 ]; then
`kdialog --sorry "No PHP files created"`
exit 1
fi;
if [ $? = 0 ]; then
`kdialog --warningcontinuecancel "Select HTML directory"`
if [ $? = 0 ]; then
cd `kdialog --getexistingdirectory`
#exit 1
else
`kdialog --warningyesno "You didn't select a directory. \
<br>Yes to choose, No to cancel."`
if [ $? = 0 ]; then
cd `kdialog --getexistingdirectory`
exit 1
fi;
fi;
fi;
if [ $? = 0 ]; then
PORTNO=`kdialog --title "Port Number" --inputbox "Port: (Eg 7000)"`
fi;
if [ $? = 1 ]; then
`kdialog --warningyesno "You didn't enter a port. <br>Yes to coose, No to cancel."`
if [ $? = 0 ]; then
PORTNO=`kdialog --title "Port Number" --inputbox "Port: (Eg 7000)"`
fi;
fi;
if [ $? = 0 ]; then
COPYORNEW=`kdialog --radiolist "Copy HTML or make new files?:" 1 "Copy \
HTML files" off 2 "Rename HTML files" off`
else
exit 1
fi;
if [ "$COPYORNEW" = 1 ]; then
php -S 127.0.0.1:$PORTNO
for f in *.html; do cp -- "$f" "${f%.html}.php"; done
#x-www-browser http://127.0.0.1:$PORTNO
exit 1
elif [ "$COPYORNEW" = 2 ]; then
php -S 127.0.0.1:$PORTNO
for f in *.html; do mv -- "$f" "${f%.html}.php"; done
#x-www-browser http://127.0.0.1:$PORTNO
exit 1
fi;
URL="http://127.0.0.1:$PORTNO"; xdg-open $URL || sensible-browser $URL || x-www-browser $URL || gnome-open $URL
无论是短版本还是长版本,打开浏览器的命令都是最后一个。
答案1
我在 shell 中尝试了最后一行,它成功了。意思是。给 PORTNO 赋一个值,然后执行最后一行。
#!/bin/bash
PORTNO=5000
URL="http://127.0.0.1:$PORTNO"; xdg-open $URL || sensible-browser $URL || x-www-browser $URL || gnome-open $URL
它在我的系统 (桌面 20.04.2) 上运行良好,
逻辑正确吗?看来 @steeldriver 说的有道理。
问候。