How to open a terminal and run a script by clicking on the icon

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Nemesis
Level 1
Level 1
Posts: 41
Joined: Wed Jun 12, 2019 1:06 am

How to open a terminal and run a script by clicking on the icon

Post by Nemesis »

I've been trying to execute a bash script by clicking on the icon.

this script are in turn calling other scripts, and echoing status,and might request input if something happens.

but now when I'm clicking the link to the script, it asks if I want to run in terminal, display, cancel or run.
if I'm pressing run in a terminal, a terminal window pops up and closes immediately.

So my question is, how to keep the terminal window open until an "exit" command tells it to close, and if it's possible to make linux mint just run the script (when I'm clicking the link) without asking what to do?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
1000
Level 5
Level 5
Posts: 999
Joined: Wed Jul 29, 2020 2:14 am

Re: How to open a terminal and run a script by clicking on the icon

Post by 1000 »

Open terminal.
There run script.
For example:

Code: Select all

bash ./script_name
How use terminal, watch tutorials on youtube.

May I ask what kind it is your script? Can I see?
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Welcome »

How to run a script by clicking on an icon.

Here's a very easy way. Right click on your Desktop, and look for "Create a new launcher here...". If you have that option when you right click, you can easily select it and enter the required information to create a launcher.

Alternatively, here's another way that should work for nearly everyone:

1. Open a text editor with a new document.

2. Copy and paste the below into that new document. Change the "Name" and "Exec" settings to what is appropriate for the script that you want to execute. You can also change the "Icon" if desired. (If you want to call a script that outputs to the terminal, you'll want to add another line "Terminal=true" to the .desktop file below and maybe "read x" at the end of the script so you can stop the script before it closes.)

Code: Select all

[Desktop Entry]
Version=1.0
Type=Application
Icon=mate-panel-launcher
Name=Example
Exec=xterm -hold -e "echo Hello My World"
3. Save the file as example.desktop or other appropriate name in a good location, perhaps your desktop?

4. Change the permissions on this file to executable. Here's one way to do that:

In the directory where the .desktop file is located, open a terminal window. Then copy and paste this command into that window (change the name as appropriate and execute it, of course):

Code: Select all

chmod +x example.desktop
Now it should work when you double-click the "Example" icon (or whatever name you've selected.)

*** Credit goes to Linux Mint Forum user 1000 for the 2 methods above. ***
Nemesis wrote: Sun Feb 14, 2021 8:17 am So my question is, how to keep the terminal window open until an "exit" command tells it to close, ...
I often add the following line to the end of my scripts:

Code: Select all

echo "Press Enter when complete."
read x
This will allow you to see the output before the window closes. Press 'Enter' to close the window.
Last edited by Welcome on Wed Feb 17, 2021 11:56 am, edited 7 times in total.
Nemesis
Level 1
Level 1
Posts: 41
Joined: Wed Jun 12, 2019 1:06 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Nemesis »

1000 wrote: Sun Feb 14, 2021 9:11 am Open terminal.
There run script.
well, that's what I'm trying to avoid, I want to just click an icon and it should be done automatically.
Everybody knows how to do it via the terminal..

well, the script is parted into 4, where it scans the directory, determinate what it is, and acts accordingly..
Nemesis
Level 1
Level 1
Posts: 41
Joined: Wed Jun 12, 2019 1:06 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Nemesis »

@wel
Welcome wrote: Sun Feb 14, 2021 10:18 am
This will allow you to see the output before the window closes. Press 'Enter' to close the window.
yes, it would work, but not here since there are several scripts being run, it looks like it starts one script and then closes the window, instead of showing the next scripts output
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Welcome »

Depends on how the other scripts are called. You can call the other scripts either synchronously or asynchronously.

If it was possible to call the other scripts synchronously, they'd run one-by-one in sequence.

You probably need them to run asynchronously. So, you'll need some method to 'signal' your main script when the other scripts are complete. Probably the easiest is to use a file to indicate that a script is complete. Or maybe even easier, an externally defined variable. There are some other methods of Inter Process Communication (IPC) such as named pipes, but their use in this case may be overkill.
Last edited by Welcome on Wed Feb 17, 2021 12:15 pm, edited 1 time in total.
Nemesis
Level 1
Level 1
Posts: 41
Joined: Wed Jun 12, 2019 1:06 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Nemesis »

Welcome wrote: Sun Feb 14, 2021 10:28 am Depends on how the other scripts are called. You can call the other scripts either synchronously or asynchronously.
God idea, I bet the first script starts the other script so it runs in the background, I need to make it run after each other, so how do I start a script from a script so the first script waits for the second one..
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Welcome »

Nemesis wrote: Sun Feb 14, 2021 10:22 am ...since there are several scripts being run, it looks like it starts one script and then closes the window, instead of showing the next scripts output
Are you expecting output from the other scripts to be displayed in the current window, AND you're calling them asychonously. There's a way to do that...
I bet the first script starts the other script so it runs in the background, I need to make it run after each other, so how do I start a script from a script so the first script waits for the second one..
I've used the following style (runs in the current shell):

Code: Select all

. ./otherscript
But, if you don't need it to run in the current shell, just calling the script should work, too.

Code: Select all

./otherscript
I'll post a sample in a minute....

Edit: The dot-space is the same as the 'source' command. See 'help source' for more info.
Last edited by Welcome on Sun Feb 14, 2021 11:41 am, edited 3 times in total.
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Welcome »

For example, create two small script files test1 and test2.

In test1:

Code: Select all

variable1="Got me?"
echo In test1
. ./test2
echo Back in test1

in test2:

Code: Select all

echo In test2....
echo $variable1

Now, run test1 and you'll get this output:
In test1
In test2....
Got me?
Back in test1
Now, try removing the dot-space and observe the output. See the difference?

Question: If you were running your scripts asychronously, did you use coproc? Or maybe as a background task? Or, could it be that a program called by your scripts is running asychronously?
Last edited by Welcome on Wed Feb 17, 2021 11:18 am, edited 1 time in total.
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Welcome »

Nemesis wrote: Sun Feb 14, 2021 8:17 am ...
Could you post your scripts so that we can better help you? If not the full set of scripts, can you explain exactly what the scripts are attempting to accomplish? We'll be better able to help you if you provide that info. Thanks! :D
Last edited by Welcome on Wed Feb 17, 2021 11:27 am, edited 4 times in total.
1000
Level 5
Level 5
Posts: 999
Joined: Wed Jul 29, 2020 2:14 am

Re: How to open a terminal and run a script by clicking on the icon

Post by 1000 »

Code: Select all

. ./test2
Maybe the Linux distribution is still far from being safe.
But remember that relative path is not recommended. ( in finished products )
Try use absolute path ( . /path/to/test2 ) to file or more safe form relative path.

Edit

Code: Select all

xterm -hold -e "echo Hello My World"
Example shortcut.desktop

Code: Select all

[Desktop Entry]
Version=1.0
Type=Application
Icon=mate-panel-launcher
Name=Example
Exec=xterm -hold -e "echo Hello My World"
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Welcome »

1000 wrote: Sun Feb 14, 2021 12:41 pm Edit

Code: Select all

xterm -hold -e "echo Hello My World"
...
Thanks! This works great! :D
Last edited by Welcome on Wed Feb 17, 2021 11:24 am, edited 1 time in total.
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Welcome »

Nemesis wrote: Sun Feb 14, 2021 10:37 am
Welcome wrote: Sun Feb 14, 2021 10:28 am Depends on how the other scripts are called. You can call the other scripts either synchronously or asynchronously.
God idea, I bet the first script starts the other script so it runs in the background, I need to make it run after each other, so how do I start a script from a script so the first script waits for the second one..
Even if your scripts are called sychronously from your main script, it also might be that the programs called in these many scripts run asychronously (that is, a program starts running and immediately returns control back to the script while it's still running). Some programs allow the user to select either "run asych" or "run sych", but most don't. If this is the case in your situation, you'll need to find some method of determining when the program completes, and test for it. Maybe the program will output a file when it's done. You'll need to test for the existance of this file, and you'll want to make sure the program has completed writting the output. You might be able to check the processes to see if the program is still running (or not). Of course, this would only work if it's the type of program that exits when it has completed it's task.

And, you might need to do this for every script/program that you've called.

Hard to help on this without knowing the details....
tonicr33d

Re: How to open a terminal and run a script by clicking on the icon

Post by tonicr33d »

Welcome wrote: Mon Feb 15, 2021 12:26 pm
1000 wrote: Sun Feb 14, 2021 12:41 pm Edit

Code: Select all

xterm -hold -e "echo Hello My World"
Example shortcut.desktop

Code: Select all

[Desktop Entry]
Version=1.0
Type=Application
Icon=mate-panel-launcher
Name=Example
Exec=xterm -hold -e "echo Hello My World"
Thanks! This works great! :D

This is a good way to make a "Launcher" in any desktop (Cinnamon, Mate, XFCE).


hello
i'm new and i dont get along with all the words and i need just one step by step tutorial on how (for exemple) make "xset dpms force off" run from desktop icon! i keep reading on the forum wich is great but some people dont use word .. like this you put there and other you put there ..... if you now what i mean and i get lost.... please if you can help me you would save me

tank you for your time even if you just read it
all the best
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Welcome »

tonicr33d wrote: Tue Feb 16, 2021 2:34 pm hello
i'm new and i dont get along with all the words and i need just one step by step tutorial on how (for exemple) make "xset dpms force off" run from desktop icon! i keep reading on the forum wich is great but some people dont use word .. like this you put there and other you put there ..... if you now what i mean and i get lost.... please if you can help me you would save me

tank you for your time even if you just read it
all the best
I'm not an expert at this, but I'll try...

1. Open a text editor with a new document.

2. Copy and paste the below into that new document.

Code: Select all

[Desktop Entry]
Version=1.0
Type=Application
Icon=mate-panel-launcher
Name=Energy Star Off
Exec=xset dpms force off
3. Save the file as energystaroff.desktop in a good location, perhaps your desktop?

4. Change the permissions on this file to executable. Here's one way to do that:

In the directory where the energystaroff.desktop file is located, open a terminal window. Then copy and paste this command into that window (and execute it, of course):

Code: Select all

chmod +x energystaroff.desktop
Now it should work when you double-click the "Energy Star Off" icon (or single-click if you've set up your system that way.)

Did it work?

By the way, I see this was your first post to the forums! Welcome tonicr33d to the Linux Mint Forums!
Last edited by Welcome on Tue Feb 16, 2021 4:31 pm, edited 1 time in total.
1000
Level 5
Level 5
Posts: 999
Joined: Wed Jul 29, 2020 2:14 am

Re: How to open a terminal and run a script by clicking on the icon

Post by 1000 »

Because it is simple. Just click right mouse button on the desktop and then select the appropriate option and create the activator.
That's all.

If you have problem find option, you can create just file. (like above)

Also from terminal you can do.
For example, copy and paste to terminal ( then click Enter key ):

Code: Select all

 echo -e '[Desktop Entry]\nType=Application\nIcon=computer\nExec=xset dpms force off\nName=Turn off' > turnoff.desktop
Allow the file to be opened as a program

Code: Select all

chmod +x turnoff.desktop
And check how it works.

On YouTube you can find many tutorials, how to use the terminal.
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: How to open a terminal and run a script by clicking on the icon

Post by Welcome »

1000 wrote: Tue Feb 16, 2021 4:29 pm Because it is simple. Just click right mouse button on the desktop and then select the appropriate option and create the activator.
That's all.
On the desktop is very important (in Cinnamon). When attempting to create a launcher in any directory window (including a Desktop window), a right click doesn't give the option to create the launcher. Only on the actual desktop. :D

Then, after the launcher is created, it can be moved to nearly any other useful directory. :D
Last edited by Welcome on Wed Feb 17, 2021 11:29 am, edited 1 time in total.
tonicr33d

Re: How to open a terminal and run a script by clicking on the icon

Post by tonicr33d »

well thank you for kindness, patience and worm welcome!

it works!!!!! :D

on cinnamon version i found like app down on panel where the volume is (for the same stuff) but on mate
( my machine is not that powerfull so i chose mate because of visuals in cinnamon. does that make sense??) .... can not find it

now i'll explore what else can i put in this way because copy and paste in terminal in not a option for me (not well organize)

Thank You and keep on helping!!!
Locked

Return to “Scripts & Bash”