Help with bash script

Forum rules
Before you post please read this

Help with bash script

Postby Floatingshed on Sat Mar 15, 2014 4:27 am

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.
Floatingshed
Level 1
Level 1
 
Posts: 30
Joined: Sun Feb 23, 2014 4:38 pm

Linux Mint is funded by ads and donations.
 

Re: Help with bash script

Postby xenopeek on Sat Mar 15, 2014 6:04 am

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.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14561
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Help with bash script

Postby Floatingshed on Sat Mar 15, 2014 11:01 am

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.
Floatingshed
Level 1
Level 1
 
Posts: 30
Joined: Sun Feb 23, 2014 4:38 pm

Re: Help with bash script

Postby xenopeek on Sat Mar 15, 2014 11:34 am

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.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14561
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Help with bash script

Postby Floatingshed on Sat Mar 15, 2014 12:08 pm

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!
Floatingshed
Level 1
Level 1
 
Posts: 30
Joined: Sun Feb 23, 2014 4:38 pm

Re: Help with bash script

Postby xenopeek on Sat Mar 15, 2014 12:42 pm

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.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14561
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Help with bash script

Postby Floatingshed on Sat Mar 15, 2014 1:18 pm

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.
Floatingshed
Level 1
Level 1
 
Posts: 30
Joined: Sun Feb 23, 2014 4:38 pm

Re: Help with bash script

Postby xenopeek on Sat Mar 15, 2014 1:50 pm

Phew! Glad it was fixed with that one :)
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14561
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest