Score:0

How to launch default browser from bash script

cn flag

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.

hr flag
Are you sure you want to `exit 1` from the script after the copy commands? I haven't followed the logic through completely, but it looks like the script will never reach the last line
jpbrain avatar
ca flag
can you try xdg-open "$URL" ?
Mark Lee avatar
cn flag
That's the first option in the list @jpbrain. I tried with the quotation marks as you have it but it didn't make a difference.
jpbrain avatar
ca flag
Hi @MarkLee. I tried the last line in ashell and it worked.
Score:0
ca flag

I tried the last line in shell and it worked. Meaning. gave a value to PORTNO and the executed the last line.

#!/bin/bash
PORTNO=5000
URL="http://127.0.0.1:$PORTNO"; xdg-open $URL || sensible-browser $URL || x-www-browser $URL || gnome-open $URL

it is working on my system (desktop 20.04.2)
is the logic ok? seems that @steeldriver has a point there.

regards.

Mark Lee avatar
cn flag
I had already tried with removing the exits, with no success.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.