How do I launch the default Web browser from within a script? In the below script, the browser opens when the script is successfully run as an executable shell script:
#!/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
However, in this longer version with Kdialog UI, everything executes correctly (the files are created and the server starts in the chosen directory) except for the launching of the browser
#!/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
In both the short- and long-form versions, the command to open the browser is the last.