Executable text file to open a terminal & execute .sh file
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
- Larry78723
- 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
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
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
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.
Re: Executable text file to open a terminal & execute .sh file
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:
or directly
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.
my_executable_file_in_my_home_folder:
Code: Select all
#!/bin/sh
cd /the/appropiate/folder
sudo bash xyz.sh
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.
- Larry78723
- 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
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:
Should this work after marking it as executable?
Using your example for my case:
Code: Select all
#!/bin/sh
cd distroshare-ubuntu-imager
sudo bash distroshare-ubuntu-imager.sh
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.
Re: Executable text file to open a terminal & execute .sh file
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.
- Larry78723
- 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
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?
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.
Re: Executable text file to open a terminal & execute .sh file
You could add the folder to the path.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?
For a sudo exemption you can run
Code: Select all
sudo visudo
Code: Select all
ALL ALL = NOPASSWD:/home/larry/distroshare-ubuntu-imager/distroshare-ubuntu-imager.sh
bash
after the sudo
in the previous script for this one.- Larry78723
- 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
gm10, rebooted after making suggested changes. Still asking for password. Any other ideas?
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.
Re: Executable text file to open a terminal & execute .sh file
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
Code: Select all
larry ALL=(ALL) NOPASSWD: /home/larry/distroshare-ubuntu-imager/distroshare-ubuntu-imager.sh
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).Re: Executable text file to open a terminal & execute .sh file
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).Larry78723 wrote: ⤴Tue Aug 06, 2019 3:54 pm gm10, rebooted after making suggested changes. Still asking for password. Any other ideas?
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. Re: Executable text file to open a terminal & execute .sh file
Alternatively, don't make typos.
- Larry78723
- 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
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.
rene, I always use copy/paste to avoid typos. If there is one, it was supplied by someone else.
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.