How do I make an executable bash script?

Maximilian

Lifer
Feb 8, 2004
12,603
9
81
I dont know if im using the correct terminology but I want to run "lscpu" and then "sensors". I want to do this by clicking on a desktop icon.

Im on debian 8.1 with KDE. Ive made a text file like so:

Code:
 #!/bin/bash
lscpu
sensors

KDE seems to pick this up as a bash script. However double clicking it just opens the text editor. Right clicking it and selecting "run in konsole" does this:

XoPx857.png


What am I missing?
 

Essence_of_War

Platinum Member
Feb 21, 2013
2,650
4
81
You can configure default behaviors for scripts, whether that's execute, or open in editor.

Do you have execute privileges for this script? If you try:

Code:
ls -lh
In the directory where the script lives, do you see something like:
Code:
rwxrwxr-x
 
Last edited:

Essence_of_War

Platinum Member
Feb 21, 2013
2,650
4
81
Hmm ok, it looks like the permissions are OK.

At the konsole, from the same folder where the script is located, could you try executing it manually ?

Code:
./cputemps
 

Essence_of_War

Platinum Member
Feb 21, 2013
2,650
4
81
It looks to me like you have a working bash script.

I'm not sure why the "run in Konsole" didn't work the way it seemed like it should, I've only played with KDE a little bit.
 

h4rm0ny

Member
Apr 23, 2015
32
0
0
Whilst I can't know that your script is running without seeing it in action, my strong suspicion is that it's running fine. You could test by adding a line that wrote to a file or similar.

Now ask yourself what you would expect to see if your script were running... (no seriously, ask yourself this and answer it properly)

And that is the problem. If you run it from the command line, then it has a standard input and a standard output and you see the results on your terminal. When you run it from the desktop GUI, what does it have? Your script returns a value that is text but that is all. It doesn't return a window object that Gnome will display (or whatever you're using). It just returns a value that is text. Unless you're capturing that text and doing something with it, it just gets discarded.

You need to amend your script so that instead of just running and exiting, it sends the information somewhere. I would recommend using Zenity which is a very simple to use dialogue display program available on nearly all GNU/Linux distributions.

It works something like this:

Code:
zenity --info --text="Hello"

Try that. You should get an information dialogue containing the text "Hello".

So your actual bash script should be something like the below:

Code:
#!/bin/bash
INFO=$(lscpu)
echo $INFO
zenity --info --text=$INFO
Note, that wont completely work, because I think the text parameter only takes one line. You'll need to pull up the documentation for zenity and check. You might even need to write your cpu info out to a file and read it back into zenity using the text-info parameter. I don't really use Zenity so I'm not an expert. But this should get you started. Hope it helps.


EDIT: If you are simply trying to get this to run in a terminal on screen (there is possibly an option for this when you click on it on the desktop if it asks you something like "Run in Terminal - Yes / No" then there is), are you certain it's not just running and not staying on screen? Your terminal will close once the script completes and given all you're doing is querying some CPU temperatures, that's going to last about 0.2 seconds. Maybe you simply blinked and missed it. The reason it closes automatically is because that's normally what you want to happen - the script executes and finishes. If you want a terminal to open to run it in, that's normally done by, well... opening a terminal and running it. Hence why the normal approach for something like this would be to actually interact with the desktop environment by using something like Zenity.
 
Last edited:

zambien1

Member
Jun 7, 2013
33
0
0
If you want to see if your script is doing anything you can get more debug by adding:

set +x

If you put this at the top of the script you will see what steps are being executed and if there are any errors.