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

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
User avatar
Larry78723
Level 14
Level 14
Posts: 5476
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 »

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
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.
Image
If you have found the solution to your initial post, please open your original post, click on the pencil, and add (Solved) to the Subject, it helps other users looking for help, and keeps the forum clean.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

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

Post by rene »

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 14
Level 14
Posts: 5476
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 »

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
If you have found the solution to your initial post, please open your original post, click on the pencil, and add (Solved) to the Subject, it helps other users looking for help, and keeps the forum clean.
gm10

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

Post by gm10 »

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.
User avatar
Larry78723
Level 14
Level 14
Posts: 5476
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 »

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
If you have found the solution to your initial post, please open your original post, click on the pencil, and add (Solved) to the Subject, it helps other users looking for help, and keeps the forum clean.
gm10

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

Post by gm10 »

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.
User avatar
Larry78723
Level 14
Level 14
Posts: 5476
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 »

gm10, rebooted after making suggested changes. Still asking for password. Any other ideas?
Image
If you have found the solution to your initial post, please open your original post, click on the pencil, and add (Solved) to the Subject, it helps other users looking for help, and keeps the forum clean.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

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

Post by rene »

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

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

Post by gm10 »

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. ;)
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

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

Post by rene »

Alternatively, don't make typos.
gm10

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

Post by gm10 »

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:
User avatar
Larry78723
Level 14
Level 14
Posts: 5476
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 »

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
If you have found the solution to your initial post, please open your original post, click on the pencil, and add (Solved) to the Subject, it helps other users looking for help, and keeps the forum clean.
Locked

Return to “Scripts & Bash”