Score:1

How to make an sh script execute commands in terminal

gb flag

I have recently developed an habit of programming. I saw this tutorial that allows me to use a program called SearXNG which allows me to make my own private search engine. (BTW click here for the tutorial) This has made me have to execute this command in particular to run the Search Engine:

cd searxng-docker && sudo docker-compose up -d

(BTW the cd is there because searxng-docker is the directory where the code is installed)

Now i want to create an sh script that if I run then the Search engine will automically start. I have seen multiple tutorials that show me how to code in sh scripts but not one have I found useful.

exec cd searxng-docker && sudo docker-compose up -d

I tried to put this into the script, and it opened the terminal, but did not execute the command. How do i fix this?

cn flag
I don't understand what "the terminal" means here. Are you launching a new gnome-terminal (or some other) and expect to see output in that window?
Samanway Karjee avatar
gb flag
@gienn By the terminal i mean that i want that command to be executed in bash
Score:1
gb flag

Well...thanks all the people that tried to answer my question but i found the answer myself. So if you are trying to find the answer to this question, look no farther than this.

First a little bit of clarification. All .sh scripts can be made to run bash commands. You just have to add the SHEBANG line to do so.

The SHEBANG line specifies which application will be used to run the script. This application can be anything from Python to Bash.

To make the script run with bash what we do is type this at the starting of the script:

#!/bin/bash

Now that we've added the SHEBANG line, all of the lines in this script will be run as bash commands.

Next, we can simply add the commands that we wanted to execute. Now the script will be looking something like this:

#!/bin/bash
cd searxng-docker
sudo docker-compose up -d

Now there is a simple change we CAN make to make the script a little bit more efficient. If you look at the cd line, you can see that we are cd-ing into the searxng-docker folder. This means that the cd command is pointing to the relative location of the folder. This is bad as if we are in any other directory than our home directory, the cd command will not work.

To fix this, we have to replace the cd searxng-docker command with the cd /home/<your-username>/searxng-docker command. Don't forget to replace the <your-username> with your real username.

Now, just one problem remains. Making the script executable.

To do this, type in the terminal: chmod +x ./<your-script-name> and again, replace <your-script-name> with the name of the script you created.

Now your script is ready and you can execute it by right-clicking on the script and choosing "Run as a program" or by typing ./<your-script-name> in the terminal, again replacing <your-script-name> with the name of the script you created.

pa4080 avatar
cn flag
In addition, you can do `sudo usermod -aG docker "$USER"` in order to add your user to the Docker's group (log-out/log-in), thus you will no longer need to use `sudo` when manage Docker.
Score:0
cn flag
exec cd searxng-docker && sudo docker-compose up -d

First a general explanation before we come to your specific case. This command will first change directory (cd to searxng-docker). Only if that succeeds (&&), the following command will be run.

As currently written, the script requires you to be in the directory containing the searxng-docker directory. Else, the second command will not proceed.

To make sure the script can be started independently from the current directory, change the relative path searxng-docker to the absolute path (e.g. /home/username/searxng-docker if the directory resides in the home directory of user usrname).

In your particular case, since you change directory in order to find an executable, there is a much better solution: replace your entire line directly launching the executable by its absolute path name:

#!/bin/sh
sudo /home/username/searxng-docker/docker-compose up -d
Samanway Karjee avatar
gb flag
Well... thanks but i already figured out a way to do that.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.