Home  >  Article  >  Backend Development  >  Shell's first shell script sharing

Shell's first shell script sharing

小云云
小云云Original
2018-03-02 09:26:461487browse

Open a text editor (you can use the vi/vim command to create the file), create a new file test.sh with the extension sh (sh represents shell), the extension does not affect the execution of the script, just know the name. , if you use php to write shell scripts, use php as the extension.

Enter some code, the first line usually looks like this:

#!/bin/bashecho "Hello World !"

#! is an agreed mark, which tells the system what interpreter this script needs to execute, that is, which shell is used .
The echo command is used to output text to the window.

There are two ways to run a Shell script:

1. As an executable program
Save the above code as test.sh and cd to the corresponding directory :
chmod +x ./test.sh #Enable the script to have execution permissions
./test.sh #Execute the script
Note, it must be written as ./test.sh, not test.sh, run The same goes for other binary programs. If you write test.sh directly, the Linux system will go to PATH to find if there is one called test.sh, but only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in PATH. Your current directory is usually not in PATH, so if you write test.sh, the command will not be found. You need to use ./test.sh to tell the system to look for it in the current directory.

2. Use the absolute path to execute
Assuming that test.sh is in the /root directory, it can be executed through the following command:
chmod +x /root/test.sh #Enable the script to have execution permissions
/root/test.sh #Execute script

3. As interpreter parameter
This mode of operation is to run the interpreter directly, and its parameter is the file name of the shell script, such as:
/bin/sh test.sh
The script run in this way does not need to specify the interpreter information on the first line, and it is useless if it is written.
And this method does not require execution permissions for the test.sh script file.

The first shell script

Open the text editor (yes Use the vi/vim command to create a file), create a new file test.sh with the extension sh (sh stands for shell), the extension does not affect the execution of the script, just know the name and understand it. If you use PHP to write shell scripts, Just use php for the extension.
Enter some code, the first line is usually like this:

#!/bin/bashecho "Hello World !"

#! is an agreed mark, which tells the system what interpreter this script needs to execute, that is, which shell is used.
The echo command is used to output text to the window.

There are two ways to run a Shell script:

1. As an executable program
Save the above code as test.sh and cd to the corresponding directory :
chmod +x ./test.sh #Enable the script to have execution permissions
./test.sh #Execute the script
Note, it must be written as ./test.sh, not test.sh, run The same goes for other binary programs. If you write test.sh directly, the Linux system will go to PATH to find if there is one called test.sh, but only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in PATH. Your current directory is usually not in PATH, so if you write test.sh, the command will not be found. You need to use ./test.sh to tell the system to look for it in the current directory.

2. Use the absolute path to execute
Assuming that test.sh is in the /root directory, it can be executed through the following command:
chmod +x /root/test.sh #Enable the script to have execution permissions
/root/test.sh #Execute script

3. As interpreter parameter
This mode of operation is to run the interpreter directly, and its parameter is the file name of the shell script, such as:
/bin/sh test.sh
The script run in this way does not need to specify the interpreter information on the first line, and it is useless if it is written.
And this method can execute the test.sh script file without execution permission.

Related recommendations:

php Analysis on shell script permission issues

Example of one-click installation of php7 by shell script

Code example of how to use shell script in php

The above is the detailed content of Shell's first shell script sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn