How two executable files communicate on Linux
Execute script / file or make it executable
Before a file can be executed, it must be created. It doesn't matter where the file is created. It is best to do it where it is most likely to be found again. The home directory of the user Pi can be used for this: "/ home / pi". You will find yourself there automatically when you open the terminal or log in via SSH.
What rights is the file created with?
There are basically several ways to create scripts. Here are two examples.
Here the note that the file extension ".sh" is only intended to indicate that it is a script file. ".sh" does not mean that the file is necessarily executable. The file extension ".sh" only indicates that it is a script. You could also leave out ".sh".
The difference is that with the first command the file is created with root rights and can therefore only be changed and executed with root rights. 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. During the execution, his rights also apply.
What is better now A script with root or user rights? It depends on what the script is doing. If the script is to do something that is relevant for a certain user, then the script should also be created and executed with user rights.
If the script does something that requires root privileges, then the script should be created with root privileges and later run with root privileges.
A user script in which commands must be executed with "sudo", i.e. with root rights, does not make that much sense. In such a case, it would be better to create a script with root rights if the script executes commands that require root rights.
Create sample script with user rights
For further testing, we create a script file with the "nano" editor:nano test.sh
Or, if the script is to be executed with root rights:sudo nano test.sh
If the script is executed as the "root" user or with "sudo", all commands in it are also executed with the rights of the "root" user. And so the command within the script does NOT have to be preceded by "sudo".
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 of how to execute the script or how to make it executable.
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 processes the commands it contains. However, you cannot 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 gives it the file name of the script to be executed. 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 for the script. Just because the file ends in ".sh" doesn't mean it is a sh script. For this reason, a script is usually made executable, which makes it more time-consuming at first, but simpler later.
Make script executable
As a rule, you don't call a script via a suitable interpreter, but make the file executable.
If the script was created with root rights as the "root" user or as a user with "sudo":
If the script was created with user rights (without sudo):chmod + x test.sh
But "making it executable" is not enough. Because depending on the configuration of the command line, it assumes that it is a bash script by default../test.sh
If that doesn't work, then the right "shebang" is missing. This is the first line in the script that indicates the correct interpreter when executed.
The correct "Shebang" depends on the interpreter:
- Bash: #! / Bin / bash
- Perl: #! / Usr / bin / perl
- Python: #! / Usr / bin / python
In the following, we assume that our test script is a "bash script". So "#! / Bin / bash" must be in the first line. To do 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 be executed. The correct interpreter is determined from the first line of the script (shebang).
But just because a "Shebang" is set doesn't mean the file is executable. In order for the "Shebang" to be used, the file must also be made executable (see above).
There are two ways to run a script. Either you execute the script via the interpreter, whereby a "Shebang" contained in the file is ignored.bash test.sh
Or you can make the file executable and set the correct interpreter in the "Shebang".chmod + x test.sh
But just because a "Shebang" is set doesn't mean the file is executable. In order to use the "Shebang", the file must be executable.
You can then run the file. Provided the correct "Shebang" is set, then it works../test.sh
Which form of execution you choose, whether with an interpreter or as an executable file and "Shebang", is in principle irrelevant.
Storage location for scripts
Where does it make sense to save your own shell scripts? Linux knows special directories for this.
- System scripts with root rights: "/ usr / local / sbin"
- General user scripts with user rights: "/ usr / local / bin"
These and other directories are contained in the global variable "$ PATH" by default. If you create a file in these directories and make it executable, you do not have to specify its directory. It will be recognized 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".
Other related topics:
Everything you need to know about computer technology.
Computer technology primer
The Computertechnik-Primer is a book about the basics of computer science, processors, memories, interfaces and other hardware components.
I want that!
- What kind of tattoo can I get
- AMC is easier than usmle
- How was Amir Khan's secret superstar
- What is heat in physics
- Are whirlpool air conditioners efficient
- What are the forestry products
- Which country invented the visa and when
- Why are arctic wolves white
- Kill Lady Stone Heart Brienne
- Is there a way to treat arteriosclerosis?
- How would you calm a scared dog?
- Why do Tollywood films have casteistic names
- Who are Spotify's Biggest Suppliers
- What do books mean to you?
- What are the job portals in Chennai
- What is Maxwell Unified Theory
- What are the surprise gifts for you
- Who would win Winged Hussars or Spartans?
- What is a UHV transformer
- Which business intelligence tool is the best
- Imports Tata Motors parts from China
- How do I crack VITEEE 2016
- How is the IFB service in Coimbatore
- Works reflected sunlight for photosynthesis