Home >Operation and Maintenance >Linux Operation and Maintenance >How to execute shell script in linux

How to execute shell script in linux

王林
王林Original
2020-03-11 16:35:195313browse

How to execute shell script in linux

There are usually four ways to execute shell scripts in Linux, namely working directory execution, absolute path execution, sh execution, and shell environment execution.

First, take a look at the content of our script

[tan@tan scripts]$ ll
total 4
-rw-rw-r--. 1 tan tan 68 May 8 23:18 test.sh
[tan@tan scripts]$ cat test.sh 
#!/usr/bin/bash
 
/usr/bin/python <<-EOF
print "Hello Shell"
    EOF

(Recommended tutorial: linux tutorial)

1. Execute in the working directory

Working directory execution means that when executing a script, first enter the directory where the script is located (at this time, it is called the working directory), and then use ./script mode to execute

[tan@tan scripts]$ ./test.sh
-bash: ./test.sh: Permission denied
[tan@tan scripts]$ chmod 764 test.sh
[tan@tan scripts]$ ./test.sh
Hello Shell

A permission error was reported, which is required here To empower, use chmod 764 test.sh. After the authorization, it can be executed normally.

2. Execute with absolute path

Execute in absolute path, which refers to the absolute path directly from the root directory/to the script directory

[tan@tan scripts]$ pwd
/home/tan/scripts
[tan@tan scripts]$ `pwd`/test.sh 
Hello Shell
[tan@tan scripts]$ /home/tan/scripts/test.sh 
Hello Shell

3. Execute with sh

sh execution refers to using the sh or bash corresponding to the script to follow the script execution

[tan@tan scripts]$ sh test.sh 
Hello Shell
[tan@tan scripts]$ bash test.sh 
Hello Shell

4. Shell environment execution

shell environment execution refers to the current shell environment To execute, you can use . to connect to the script or source to connect to the script

[tan@tan scripts]$ . test.sh 
Hello Shell
[tan@tan scripts]$ source test.sh 
Hello Shell

Related tutorial recommendations: linux video tutorial

The above is the detailed content of How to execute shell script in linux. 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
Previous article:where are linux logsNext article:where are linux logs