Linux script run program

How To Run Bash Script In Linux?

The true power of a Bash script is utilized when it is run. But how to do that? Well, there are a plethora of ways to run a Bash script( shell script). Some of them may be useful in certain conditions, while it doesn’t matter how you run the script. Bash scripts are usually executed in the terminal or command-line interface.

To run a Bash script there are many ways. Some of them are given below:

  1. Using bash or sh.
  2. Using source.
  3. Running directly in a bash environment.

For making some of these methods work, the script must have a shebang as the header to indicate it’s a shell script or bash script in this case. So, be sure to include the command below at the top of the file.

This command will make the script run under the bash interpreter. It is recommended to write the shebang header even if it works without them.

Using bash or sh

This is the most standard way of executing the bash script. You must have git bash installed if you are using Windows. For Linux and macOS, bash is installed by default. In this method, we type bash followed by the file name with extension i.e. sh in this case. In a terminal, run the following code by replacing the filename with your bash script filename.

Here, bash is a program that contains the shell environments necessary to run the script from the bash shell. So this will execute the script from the bash interpreter.

Using bash command to run the script.

We can also use sh to run the script as it will direct to the default shell in the setup environment.

Using the sh command to run the bash script.

From the above example, we were able to run a bash script using bash as well as the sh command. If you are not in the same folder/directory as the script, make sure you specify the relative path to the script.

Using source

This method is quite easy to run a bash script, and all of them are quite simple. We just need to type in “source” before the file/script name with an extension. In a terminal, run the following code by replacing the filename with your bash script filename.

The script will simply get executed after “sourcing” the file. The source command will execute the shell script as the default bash command provided you are in the bash shell. You need to be in the bash shell to execute the script using the source command.

Читайте также:  What is raid partition in linux

Using Source to run a bash script

From the screenshot of the script running, we can see that the source works exactly like the bash or sh command. The above script is a very basic script, but that doesn’t matter as long as the script is errorless and bug-free. Also, you need to add the relative path here as well if you are not in the same directory as the bash script.

By specifying the path to the script and chmod

This is a standalone method to run a bash script. We have to execute the script as an executable, we can run the script anywhere provided we have a bash shell somewhere in the environment. To make it executable we need to make sure we have the rights to run the file as an executable. We will use chmod for changing the rights on the file/script. In a terminal, run the following code by replacing the filename with your bash script filename.

The above command will allow us to execute the file. So it changes the mode of the file, the file should be read-only, executable, or any other mode for files. If you are using Linux and are not the root user, simply use sudo before the command chmod. The +x command will make sure the file is executable by everyone in the environment.

After the permission of the file is taken care of, we can now simply execute the file as follows. The command below takes into consideration that you are in the same directory as the file/ bash script is in.

If you are not on the same path as the bash script, make sure you provide the relative path to the file or the bash script.

using chmod and executing the script.

Executing a script from a relative path.

The above snippets and screenshots show that we can run the scripts in a bash environment by changing the mode of the file using the chmod.

From the following guide, we were able to run scripts in Linux using various methods and programs. So, those were some methods to run a bash script on Linux or pretty much anywhere.

Источник

Linux: Run a binary in a script

i want to run a program via script. normally i type ./program in the shell and the program starts. my script looks like this:

#!/bin/sh cd /home/user/path_to_the_program/ sh program 

5 Answers 5

If ./program works in the shell, why not use it in your script?

#!/bin/sh cd /home/user/path_to_the_program/ ./program 

sh program launches sh to try and interpret program as a shell script. Most likely it’s not a script but some other executable file, which is why it fails.

Читайте также:  Enable sftp on linux

this worked but I’m unable to understand why it works this way but if I run it as . /home/user/path_to_program/program it gives an error

@VibhanshuBiswas probably parts of your script rely on the current working directory being the directory where the script is.

The shell tries to execute the program according to how it determines the file needs to be executed. If it is a binary, it will attempt to execute the entry subroutine. If the shell detects it is a script, e.g through the use of

the shell will pass the file (and any supplied arguments) as arguments to the supplied interpreter, which will then execute the script. If the interpreter given in the path does not exist, the shell will error, and if no interpreter line is found, the shell will assume the supplied script is to executed by itself.

when the first line of program contains

assuming that /bin/sh is the sh in your path (it could be /system/bin/sh, for example). Passing a binary to sh will cause sh to treat it as a shell script, which it is not, and binary is not interpretable shell (which is plain text). That is why you cannot use

in this context. It will also fail due to program being ruby, awk, sed, or anything else that is not a shell script.

Источник

Shell script to run a program with arguments

I am setting up game servers via SSH and was wondering if I could execute one file that could be run with arguments (like Windows Batch files) without having to type it out so long. What I want is a file that will execute

./srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128 

without me having to type all that out or copy it from a wiki page every time. Just to clarify for people coming after this has been solved, my server is running CentOS.

6 Answers 6

Just put a simple shell script in the directory where your game resides

#!/bin/sh cd $(dirname $0) ./srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128 

Should you want to put the script somewhere else, you should use the full path

#!/bin/sh /path/to/srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128 

I agree with this answer, but this script will work only if launched in the same directory of the srcds_run program. Use /path/to/file/srcds_run . instead.

@dr01 — what is the point of hardcoding a pathname in a script? Why write a script at all in that case?

Читайте также:  Stm cube ide linux

This worked for me. I copied this and put it in to the folder containing srcds_run, and then I typed bash game.sh (what I named it) when I was in that directory via SSH and it worked! Now I am planning to try the answer below this one too. Thanks a lot!

So you need to create an alias in your .bash_profile :

alias game="./srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128" 

After source .bash_profile or relogin (to load the new game command) and finally game .

After adding the alias you can also bind a fast key in your keyboard settings (depends on distribution ofc) to run it really fast n easy.

I suggest you use a shell function rather than an alias.

runds()< set srcds_run \ -game "$" \ -console \ -usercon \ +game_type "$" \ +game_mode "$" \ +map "$" \ +mapgroup "$" \ -tickrate "$" if command -v "./$1" >/dev/null then "./$@"; else "$@" fi > 

The above is a shell function which you might run at a command-prompt with copy/paste right now, or else paste into your shell’s environment file (such as ~/.bashrc ) which will parameterize your command. After the function has been defined in your shell’s environment you can enter the command runds without arguments to use only the argument defaults you named in your question, or else, if you do give it arguments it will use those you specify to replace the defaults assigned for $1 . $6 in order.

I tried to guess at which arguments you were more likely to want to change occasionally than others and ordered them accordingly, but also preserved the command-line order in the question in case it mattered. This is why they do not appear in numerical order in the set statement.

I also set it up so the function will try first to run ./srcds if it might, but, failing that, it will still attempt to run a srcds command which might be elsewhere located in $PATH just in case.

Anyway, the point is, if you do.

. at your shell prompt then the command the function will attempt to run will be.

./srcds_run \ -game csgo \ -console \ -usercon \ +game_type 0 \ +game_mode 1 \ +map de_dust2 \ +mapgroup mg_active \ -tickrate 128 

. if ./srcds_run is a file which the shell might attempt to execute, or else it will try the same command but without the leading ./ , or else it will fail with error and let you know.

Then the command attempted will be changed somewhat.

./srcds_run \ -game other_game \ -console \ -usercon \ +game_type 0 \ +game_mode 1 \ +map new_map \ +mapgroup mg_active \ -tickrate 128 

. will be the command it attempts to run instead, with the same notes as before about ./ and so on, of course.

Источник

Оцените статью
Adblock
detector