4

I created a basic ls.sh script with the following body:

#!/bin/bash -x
ls

and I was sure to allow execute permissions chmod +x ls.sh. I tested ls.sh in my terminal and it works fine. I went ahead and created a ls.desktop entry in ~/.local/share/applications/ with the following body:

[Desktop Entry]
Encoding=UTF-8
Name=ls.sh
Exec=ls.sh
Terminal=true
Type=Application
Categories=Programming;

I then press my Super key, start typing ls.sh, select it from my Ubuntu GNOME default launcher, and nothing happens. There's no output from ls printed to my terminal. How do I get the output to print to my terminal? Or a newly launched terminal?

And let me add that I went ahead and created another script and desktop entry with whiptail, and whiptail does indeed launch when I press my Super key, start typing ls-whiptail.sh and select it from my Ubuntu GNOME default launcher. So it appears I'm doing everything "right". I just need to determine how to see the terminal output from my scripts when initialized through the GNOME .desktop application launcher.

ls-whiptail.sh

#!/bin/bash -x
if whiptail --yesno "Are you sure you want to run this 'ls' command?" 10 40; then
    ls
else
    whiptail --msgbox "You did not run the 'ls' command." 10 40
fi

ls-whiptail.sh desktop entry

[Desktop Entry]
Encoding=UTF-8
Name=ls-whiptail.sh
Exec=ls-whiptail.sh
Terminal=true
Type=Application
Categories=Programming;
12
  • 3
    I suspect you need a long-running command after the ls, otherwise the default gnome-terminal behavior is to exit immediately after the command completes - see for example How to keep terminal open after launching desktop file Commented Oct 19, 2023 at 23:48
  • ... Also, where did you place the ls.sh file? is its parent directory present in your search path $PATH? ... whiptail is an executable that is already in your search path and that's part of why it works.
    – Raffa
    Commented Oct 20, 2023 at 8:21
  • @steeldriver that's the answer, you might as well post it. I just tested and the OP's script works if we add a sleep.
    – terdon
    Commented Oct 20, 2023 at 11:32
  • 1
    @terdon Ah ... I guess because your ls.sh script is run in a sub-shell that is actually still will close returning back to the other parent shell of it i.e. the one forked for the command string ... Why not try adding either read or exec bash to the end of ls.sh scriptfile itself to hold that shell with the output ... I hope it'll work ... I mean It should work that way :-)
    – Raffa
    Commented Oct 20, 2023 at 12:22
  • 1
    Ah yes, that works! Post an answer, @Raffa!
    – terdon
    Commented Oct 20, 2023 at 12:26

1 Answer 1

4

Assuming that you know that ls as you put it is a useless example as it will always list files under your home directory unless you specify an argument to it or a working directory to the terminal ... and that you know Exec=ls.sh is not optimal and you better use something like Exec=/full/path/to/ls.sh ... also not discussing your ls.desktop file below this point but rather your script file ls.sh.

with ls, what happens is a terminal opens and your script runs in a shell then your script ends, the shell is terminated and the terminal is closed ... hence any terminal output is gone ... whiptail on the other hand keeps waiting for user input and therefore your script hasn't ended yet and thus holding with it the running shell that in turn is holding with it the terminal it's running in.

If you want to see the output, hold the terminal1 for a certain period of time by e.g. adding a sleep call to the end of your script like so:

#!/bin/bash
ls
sleep 10

... or hold it until you press Enter by making it wait for user input like so:

#!/bin/bash
ls
read

... or hold it by replacing the running shell with another interactive shell that you can actually use afterwords like so:

#!/bin/bash
ls
exec /bin/bash

... or hold it with any other means like e.g. with launching a GUI window containing the output of your command like so:

#!/bin/bash
ls | /bin/zenity --text-info

... etc.


1 You're not really holding the terminal but the last running shell in it is ... and unless you explicitly run a terminal in your script with e.g. gnome-terminal or similar, your script's output "should" (generally) be kept even if from multiple shells and/or sub-shells

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .