Help with bash script

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
Floatingshed

Help with bash script

Post by Floatingshed »

I have created a script and put it in a folder in my path.
If I open terminal and type the name of the script it runs fine.

How do I make it so that I just double-click on the script and it opens Terminal and runs?
And leaves Terminal visible.
Basically I'm after something akin to .bat in windows

Thanks.
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.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Help with bash script

Post by xenopeek »

This isn't about getting help with a bash script; this is about configuring your specific terminal for how you want it to behave. By default, the terminal closes after you choose to "Run in Terminal" a script. You can probably configure your terminal to not close after running a script that way. Depends on the terminal you use, and as you have given us zero information about your installation we can't help with any specifics. On Cinnamon you can configure the default Terminal through its Edit > Profile Preferences menu, the Title and Command tab, there set "When command exits" to what you want.

If you have a terminal without a menu, right-click inside the terminal and select to show the menubar.

Alternatively, if you want to change it only for this one bash script, put the following at the top of your bash script:

Code: Select all

# if the script was not launched from a terminal, restart it from a terminal
if [[ ! -t 0 && -x /usr/bin/x-terminal-emulator ]]; then
	/usr/bin/x-terminal-emulator -e "bash -c \"$0 $*; read -s -p 'Press enter to continue...'\""
	exit
fi
Then start it with "Run" instead of "Run in Terminal", and it will ask for you to press the enter key before closing the terminal after the script completes. So this doesn't need above change to the terminal's settings.
Image
Floatingshed

Re: Help with bash script

Post by Floatingshed »

Actually this is about helping to run a script, the "keep the terminal visible" comment was an aside, but useful as this is performing a download and an idea of progress is always nice.

I am running Mint 16 MATE.

My question remains. How do I make my script double-clickable?

Thanks.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Help with bash script

Post by xenopeek »

Floatingshed wrote:How do I make it so that I just double-click on the script and it opens Terminal and runs?
And leaves Terminal visible.
Floatingshed wrote:My question remains. How do I make my script double-clickable?
I don't follow, can you explain a bit more what you expect to happen when you double-click the file and what doesn't? Assuming you marked the file as executable, you'll be presented with a choice to either run the script, run it in a terminal, edit it, or cancel. When you "run" a terminal isn't opened unless you added my above code. When you "run in terminal" a terminal is opened but unless you made above configuration change the terminal closes immediately after the script is done.
Image
Floatingshed

Re: Help with bash script

Post by Floatingshed »

Ah, when I double-click the script absolutely nothing happens, not a glimmer. No errors, nothing.
If I right click it says it is executable but opens with wine! That can't be right!
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Help with bash script

Post by xenopeek »

Ah, Wine :) Wonderful program. Suddenly all your text files are opened in Wine with Notepad... Stuff of nightmares and I can't fathom why anybody would want to use Notepad on Linux -- let alone having it hijack your system like this :(

What extension have you given your bash script? .sh? That should be safe from Wine I'd hope. Else I'm not sure how to recover from this. You'll probably have to edit the file associations in Caja. You could also look at the file .local/share/applications/mimeapps.list and remove any association to Wine for text files, that might be the simplest approach.

And check your Caja configuration; that you've set its behavior to ask you each time what to do for executable text files. Should be somewhere in Edit > Preferences I think.
Image
Floatingshed

Re: Help with bash script

Post by Floatingshed »

Excellent! Editing the mimeapps.list has rescued my computer from the clutches of wine!
The script is now behaving as it should.
Many thanks for the help.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Help with bash script

Post by xenopeek »

Phew! Glad it was fixed with that one :)
Image
Locked

Return to “Scripts & Bash”