Executable text file to open a terminal & execute .sh file

Forum rules
Before you post please read how to get help
Post Reply
User avatar
Larry78723
Level 6
Level 6
Posts: 1399
Joined: Wed Jan 09, 2019 7:01 pm
Location: Jasper County, SC, USA

Executable text file to open a terminal & execute .sh file

Post by Larry78723 » Tue Aug 06, 2019 1:59 pm

I have a .sh file in a folder under /home/larry/ that I'd like to execute from an executable text file in my home folder but have no idea how to do the following from the text file:

1. open a terminal
2. cd to the appropriate folder
3. then execute a "sudo bash xyz.sh"

I'd appreciate any help you can provide.
Larry
Image

rene
Level 12
Level 12
Posts: 4172
Joined: Sun Mar 27, 2016 6:58 pm

Re: Executable text file to open a terminal & execute .sh file

Post by rene » Tue Aug 06, 2019 2:19 pm

Given that you refer to one of the files involved as "a .sh file" and the other as "an executable text file" it is unclear whether or not you mean latter is (also) a shell script, or are referring to something more specific. If we're just talking about two shell scripts, the idea is not "to open a terminal". A terminal is an interface for humans; from one shell script you simply launch another directly. I.e.,

my_executable_file_in_my_home_folder:

Code: Select all

#!/bin/sh
cd /the/appropiate/folder
sudo bash xyz.sh
or directly sudo bash /the/appropriate/folder/xyz.sh if xyz.sh does not need /the/appropriate/folder as its current working directory, or even more directly sudo /the/appropriate/folder/xyz.sh if xyz.sh is itself executable, or...

That is; needs more context. Currently not too clear how to answer most usefully. One thing you're always going to run into is the "sudo"; you will want to tell sudo that the xyz.sh script can be executed without having to enter a password (or execute the first script as root, or...) but that's for later; first just experiment with an xyz.sh script that doesn't need privileges.

User avatar
Larry78723
Level 6
Level 6
Posts: 1399
Joined: Wed Jan 09, 2019 7:01 pm
Location: Jasper County, SC, USA

Re: Executable text file to open a terminal & execute .sh file

Post by Larry78723 » Tue Aug 06, 2019 2:46 pm

rene, perhaps I can explain the situation better. The program I want to execute must be run in a terminal from within its own folder (/home/larry/distroshare-ubuntu-imager). I'm trying to call it from my own /home/larry folder. The file that must be run from /home/larry/distroshare-ubuntu-imager has a #!/bin/bash first line. What I'm trying to accomplish is to create a way to execute that #!/bin/bash file from my /home/larry folder.

Using your example for my case:

Code: Select all

#!/bin/sh
cd distroshare-ubuntu-imager
sudo bash distroshare-ubuntu-imager.sh
Should this work after marking it as executable?
Image

gm10
Level 19
Level 19
Posts: 9248
Joined: Thu Jun 21, 2018 5:11 pm

Re: Executable text file to open a terminal & execute .sh file

Post by gm10 » Tue Aug 06, 2019 3:03 pm

That would work (the bash in there is optional if you've got a shebang), although I do not understand why you wouldn't just call it directly.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

User avatar
Larry78723
Level 6
Level 6
Posts: 1399
Joined: Wed Jan 09, 2019 7:01 pm
Location: Jasper County, SC, USA

Re: Executable text file to open a terminal & execute .sh file

Post by Larry78723 » Tue Aug 06, 2019 3:09 pm

I'm trying to do it this way because I can never remember that long folder name. :? Now how do I set it up so I don't need to enter my password for the sudo command?
Image

gm10
Level 19
Level 19
Posts: 9248
Joined: Thu Jun 21, 2018 5:11 pm

Re: Executable text file to open a terminal & execute .sh file

Post by gm10 » Tue Aug 06, 2019 3:26 pm

Larry78723 wrote:
Tue Aug 06, 2019 3:09 pm
I'm trying to do it this way because I can never remember that long folder name. :? Now how do I set it up so I don't need to enter my password for the sudo command?
You could add the folder to the path.

For a sudo exemption you can run

Code: Select all

sudo visudo
and then add something like this

Code: Select all

ALL ALL = NOPASSWD:/home/larry/distroshare-ubuntu-imager/distroshare-ubuntu-imager.sh
to allow every user on the system to execute that file with root privileges without having to authenticate. Remove the bash after the sudo in the previous script for this one.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

User avatar
Larry78723
Level 6
Level 6
Posts: 1399
Joined: Wed Jan 09, 2019 7:01 pm
Location: Jasper County, SC, USA

Re: Executable text file to open a terminal & execute .sh file

Post by Larry78723 » Tue Aug 06, 2019 3:54 pm

gm10, rebooted after making suggested changes. Still asking for password. Any other ideas?
Image

rene
Level 12
Level 12
Posts: 4172
Joined: Sun Mar 27, 2016 6:58 pm

Re: Executable text file to open a terminal & execute .sh file

Post by rene » Tue Aug 06, 2019 3:55 pm

gm10 wrote:
Tue Aug 06, 2019 3:26 pm
You could add the folder to the path.
As a variation on that I'd simply move the initial script to ~/bin, a directory which if it exists is added to your PATH automatically. I.e., my launcher script would be, say, ~/bin/imager, consisting of

Code: Select all

#!/bin/sh
cd ~/distroshare-ubuntu-imager && sudo ./distroshare-ubuntu-imager.sh
and marked executable. If your ~/bin didn't exist before now, restart the terminal after creating it, otherwise should be fine immediately. As another slight variation I personally tend to deploy /etc/sudoers.d. I.e., I'd (as root) create a file /etc/sudoers.d/distroshare-ubuntu-imager,

Code: Select all

larry ALL=(ALL) NOPASSWD: /home/larry/distroshare-ubuntu-imager/distroshare-ubuntu-imager.sh
with "larry" replaced by your username and give correct permissions with sudo chmod 440 /etc/sudoers.d/distroshare-ubuntu-imager. This has a mostly documentation benefit over editing /etc/sudoers itself: a simple ls -l /etc/sudoers.d will document your password(-less) or other access tweaks if you keep to one file per executable or purpose. Yes, you should also make sure to mark /home/larry/distroshare-ubuntu-imager/distroshare-ubuntu-imager.sh executable in that instance for sudo's benefit (i.e., it seeing that script itself rather than bash being executed).

gm10
Level 19
Level 19
Posts: 9248
Joined: Thu Jun 21, 2018 5:11 pm

Re: Executable text file to open a terminal & execute .sh file

Post by gm10 » Tue Aug 06, 2019 4:04 pm

Larry78723 wrote:
Tue Aug 06, 2019 3:54 pm
gm10, rebooted after making suggested changes. Still asking for password. Any other ideas?
You don't need to reboot, just make sure you removed the bash from the script (otherwise you'd have to add it to the sudoers entry).
rene wrote:
Tue Aug 06, 2019 3:55 pm
I'd (as root) create a file /etc/sudoers.d/distroshare-ubuntu-imager,
Please keep doing that via visudo (-x output_file parameter) for it performs a sanity check on your changes. Otherwise you can end up in the situation that you make a typo in that file and break your sudo access. ;)
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

rene
Level 12
Level 12
Posts: 4172
Joined: Sun Mar 27, 2016 6:58 pm

Re: Executable text file to open a terminal & execute .sh file

Post by rene » Tue Aug 06, 2019 4:06 pm

Alternatively, don't make typos.

gm10
Level 19
Level 19
Posts: 9248
Joined: Thu Jun 21, 2018 5:11 pm

Re: Executable text file to open a terminal & execute .sh file

Post by gm10 » Tue Aug 06, 2019 4:15 pm

rene wrote:
Tue Aug 06, 2019 4:06 pm
Alternatively, don't make typos.
I call it fuzzy typing, it's an advanced technique. :lol:
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

User avatar
Larry78723
Level 6
Level 6
Posts: 1399
Joined: Wed Jan 09, 2019 7:01 pm
Location: Jasper County, SC, USA

Re: Executable text file to open a terminal & execute .sh file

Post by Larry78723 » Tue Aug 06, 2019 4:16 pm

Thanks folks, it's working just the way I want it to. I'll bookmark this in case I need it sometime.

rene, I always use copy/paste to avoid typos. If there is one, it was supplied by someone else. :P
Image

Post Reply

Return to “Scripts & Bash”