Launch SH script in Linux

Anonim

Launch SH script in Linux

Step 1: Creating a check script

First, we propose to create a check script that will run in the future. Of course, you can skip this stage if the command line script is already available. Note that now we will not disassemble the syntax of programming languages, but only we will tell you that everyone can create your own script for each or insert the contents to the file for its further call in circumstances.

  1. Run the "Terminal" convenient for you, for example, through the application menu or the hot key Ctrl + Alt + T.
  2. Running the terminal for manual creation of SH format script in Linux

  3. Here, use the Sudo Nano Script.sh command, where Nano is the text editor used, and script.sh is the name of the created file. You can create a file, for example, through the same VI or GEdit, it will not change the essence, and the name of the element also changes according to personal preferences of the user.
  4. A command to launch a text editor before creating a SH format script in Linux

  5. Confirm this action by entering the password from the superuser account, since it is performed with the Sudo argument.
  6. Confirmation of a text editor to create a SH in Linux format script

  7. A new file will open in which you can insert the script strings. Below you see the standard example responsible for displaying the message "Hello World". If there is the contents of another character, simply insert it into the console, making sure that all rows are written correctly.

    #! / BIN / BASH

    Echo "Hello WORLD"

  8. Creating SH format script in Linux via text editor

  9. After that, you can save the settings by holding the Ctrl + O key combination.
  10. Switch to maintaining SH format script in Linux after its creation

  11. The file name is not needed, since we are asked when creating. Just click on ENTER to complete the saving.
  12. Select the name for SH format script in Linux after its creation

  13. Leave a text editor via Ctrl + X.
  14. Completing work in a text editor after creating a SH format script in Linux

As we see, nothing complicated in creating your own scripts for Bash is not, but the entire feature is to know the code. You will have to either write it from scratch yourself, or copy ready-made solutions from free sources. After the script is successfully implemented in the file, you can safely switch to the next step.

Step 2: Script Setup for EN Utility

This step is also not mandatory, but without it can not do users who use the ENV utility to start the script. Without prior configuration, it simply does not open, since the corresponding permissions were not originally received. They are added via the sudo chmod ugo + x script.sh command, where script.sh is the name of the required file.

The command to provide access to the SH script in Linux before it starts

Do not forget that all actions performed through the Sudo argument require confirmation of the authentication of the account through the input of the superuser password. After that, a new string will appear to start commands, which means the successful use of the setting.

Enter the password to confirm the opening of access to the SH script in Linux

Step 3: Starting an existing script

We turn to the main step, which is directly launching the existing script. To begin with, consider a simple command, which has the view ./script.sh and is responsible for launching a file from the current location. The output result you see the screenshot below. For example, we took the previously created script. In the same way, you can also specify the full path to the object so that the string has changed, for example, to /home/user/script.sh.

A command to open the SH script in Linux from the current folder

Linux has a PATH system variable. It includes a number of folders responsible for performing a wide variety of actions. One of them is called / usr / local / bin and is used for manual installation of programs. If you do not want to constantly specify the full path to the script to activate it, simply add it to one of the PATH folders. To do this, use the cp script.sh /usr/local/bin/script.sh string.

A command to move the SH format script in Linux to the variable folder

After that, the launch will be available by simply entering the file name with expansion.

Start SH format script in Linux after successful transfer to the variable folder

The second opening method consists in simultaneously calling the shell. You should move to the script directory and write Bash Script.sh. This method is good because it allows you to not enter the full path to the object or pre-add it to the corresponding directory of PATH.

Launch SH format script in Linux with a shell

That's all we wanted to talk about interaction with scripts in Linux. You only have to create an appropriate file or open existing, using the above recommendations.

Read more