Follow by Email

Blog Archive

Search This Blog

Execute script file or make executable

Execute script / file or make executable Before you can run a file, it must be created. It does not matter where the file is created. It's best to do it where you find it most easily. For this purpose, the home directory of the user Pi offers
Share it:
Execute script / file or make executable

Before you can run a file, it must be created. It does not matter where the file is created. It's best to do it where you find it most easily. For this purpose, the home directory of the user Pi offers: "/ home / pi". There you are automatically, if you open the terminal or by SSH logs.
With which rights is the file created?
There are basically several ways to create scripts. In the following two examples.
Here is the note that the file extension ".sh" should only indicate that it is a script file. ".sh" does not mean that the file is necessarily executable. With the file extension ".sh" you just make it clear that it is a script. You could also omit ".sh".
 sudo nano test.sh
or
 nano test.sh
The difference is that the first command creates the file with root privileges and thus can only be changed and executed with root privileges. Of course, the rights can be changed later.
In the second variant without "sudo" the file is created with the rights of the currently logged in user. In the execution also its rights apply.

What is better now? A script with root or user rights? It depends on what the script does. If the script should do something that is relevant for a particular user, then the script should also be created and executed with the user rights.
If the script does something that requires root privileges, then the script should be created with root privileges and run later with root privileges.

A user script in which commands with "sudo", ie with root rights, must be executed does not make much sense. In such a case, it would be better to create a script with the same root privileges, if the script executes commands that require root privileges.
Create an example script with user rights
For further testing we create a script file with the editor "nano":
 nano test.sh
or if you want to run the script with root privileges:
 sudo nano test.sh
If the script is run as user "root" or with "sudo", all commands within it are also executed with the rights of the user "root". And thus the command within the script NO "sudo" must be prefixed.

In the editor we enter the following line:
 echo "test"
Then save and close: Ctrl + O, Return, Ctrl + X.

After the script has been created, the question arises how to execute the script or make it executable.
Execute script
The easiest way to execute a script is to pass the file to a suitable interpreter. The interpreter is a program that calls the file and executes the commands it contains. However, you can not use every interpreter. The commands contained in the script must already match the interpreter. In this case it is the bash interpreter.

The call for this bash script is as follows:
 bash test.sh
This command calls the Bash interpreter and passes the file name of the script to be executed. Here it is important that the file is in the current directory.
The example script then does nothing other than output "test" on the command line.

The disadvantage of this command is that you have to know and enter the interpreter to the script. Just because the file ends in ".sh" does not mean it's a sh script. For this reason, a script is usually executable, which makes it more complex at first but easier later.
Make script executable
As a rule, a script is not called via a suitable interpreter, but makes the file executable.
If the script with root privileges was created as user "root" or as user with "sudo":
 sudo chmod + x test.sh
If the script was created with user rights (without sudo):
 chmod + x test.sh
The "make executable" is not enough. Because depending on the configuration of the command line, this assumes that it is by default a bash script.
 ./test.sh
If that does not work, then the right "Shebang" is missing. It is the first line in the script that points to the correct interpreter during execution.
The right "Shebang" depends on the interpreter:
  • Bash: #! / Bin / bash
  • Perl: #! / Usr / bin / perl
  • Python: #! / Usr / bin / python
We further assume that our test script is a "bash script". In the first line must be "#! / Bin / bash". For this we open the script file.
 nano test.sh
In addition, we add the following in the first line:
 #! / Bin / bash
Then save and close: Ctrl + O, Return, Ctrl + X.

After that we can run the script:
 ./test.sh
"./" causes the file to execute. The correct interpreter is determined from the first line of the script (Shebang).
But just because a "shebang" is set does not mean that the file is executable. For the "Shebang" to be used, the file must also be made executable (see above).
Summary
There are two ways to run a script. Either the script is executed via the interpreter, whereby a containing "shebang" in the file is ignored.
 bash test.sh
Or you make the file executable and set the correct interpreter in the "Shebang".
 chmod + x test.sh
But just because a "shebang" is set does not mean that the file is executable. For the "Shebang" to be used, the file must be executable.

Then you can run the file. Assuming the right "Shebang" is set, then it works too.
 ./test.sh
Which form of execution one chooses, whether with an interpreter or as an executable file and "shebang", is irrelevant in principle.
Location for scripts
Where do you logically store your own shell scripts? Linux has special directories for this purpose.
  • System scripts with root privileges: "/ usr / local / sbin"
  • General user scripts with user rights: "/ usr / local / bin"
These and other directories are included by default in the $ PATH global variable. If you create a file in these directories and make them executable, you do not have to specify their directory. It is detected automatically.

Copy the sample script:
sudo mv test.sh / usr / local / bin
Then run it:
 test.sh
Here it is important that the script is executable and contains the "Shebang".
Share it:

Raspberry Pi

Post A Comment:

0 comments: