Open a text editor (you can use the Vi/vim command to create a file), a new file test.sh, the extension sh (sh for Shell), the extension does not affect the execution of the script, see the name is good, if you write Shell script in PHP, the extension is good for PHP.
Enter some code, the first line is usually this:
#!/bin/bashecho "Hello World!"
#! is a contract tag that tells the system what interpreter the script needs to execute, even if it uses a shell.
The echo command is used to output text to a 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 appropriate directory:
chmod +x./test.sh #使脚本具有执行权限
./test.sh #执行脚本
Note, be sure to write./test.sh, not test.sh, run other binary programs as well, direct write Test.sh,linux system will go to the path to find there is no test.sh, and only/bin,/sbin,/USR/BIN,/USR /sbin wait in path, your current directory is usually not in path, so write test.sh will not find the command, to use./test.sh tells the system that it is looking in the current directory.
2. Execute with absolute path
Assuming that test.sh is in the/root directory, it can be executed with the following command:
chmod +x/root/test.sh #使脚本具有执行权限
/root/test.sh #执行脚本
3. As an interpreter parameter
This works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:
/bin/sh test.sh
The script that runs this way does not need to specify the interpreter information in the first line, and it is useless to write.
And this approach does not require EXECUTE permission on the test.sh script file and can be executed
First shell script
Open a text editor (you can use the Vi/vim command to create a file), a new file test.sh, the extension sh (sh for Shell), the extension does not affect the execution of the script, see the name is good, if you write Shell script in PHP, the extension is good for PHP.
Enter some code, the first line is usually this:
#!/bin/bashecho "Hello World!"
#! is a contract tag that tells the system what interpreter the script needs to execute, even if it uses a shell.
The echo command is used to output text to a 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 appropriate directory:
chmod +x./test.sh #使脚本具有执行权限
./test.sh #执行脚本
Note, be sure to write./test.sh, not test.sh, run other binary programs as well, direct write Test.sh,linux system will go to the path to find there is no test.sh, and only/bin,/sbin,/USR/BIN,/USR /sbin wait in path, your current directory is usually not in path, so write test.sh will not find the command, to use./test.sh tells the system that it is looking in the current directory.
2. Execute with absolute path
Assuming that test.sh is in the/root directory, it can be executed with the following command:
chmod +x/root/test.sh #使脚本具有执行权限
/root/test.sh #执行脚本
3. As an interpreter parameter
This works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:
/bin/sh test.sh
The script that runs this way does not need to specify the interpreter information in the first line, and it is useless to write.
It is also possible to execute the test.sh script file without the need for execute permission.