Home  >  Article  >  System Tutorial  >  Multiple ways to execute shell scripts in Linux and sharing of specific methods

Multiple ways to execute shell scripts in Linux and sharing of specific methods

WBOY
WBOYOriginal
2024-07-24 09:51:06565browse

Linux 中执行 shell 脚本的多种方式及具体方法分享

There are many methods to choose from to execute shell scripts in Linux. In this article, I will share with you the specific method of adding shell script execution permissions in Linux.

Create script file

The first step is to create a new file with a .sh extension using the following command:

[root@localhost ~]<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px"># touch hello_script.sh</span>

Write a simple script

Open the newly created file with vim editor linux execution permissions and add the following bash script to the file:

[root@localhost ~]<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px"># vim hello_script.sh</span>

The following is the script content added to the file:

<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px">#!/bin/bash echo "Hello World"</span>

After editing, save and exit.

权限执行不_权限执行此操作_linux 执行权限

Execute Bash script

There are two ways to run bash files. The first is by using bash or sh commands. Another way is to add executable permissions to the file linux execution permissions, and you can run it directly. Let us run the following command to execute bash script using bash or sh command.

[root@localhost ~]<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px"># sh hello_script.sh</span>
Hello World
[root@localhost ~]<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px"># bash hello_script.sh</span>
Hello World

Set executable permissions for script files

The second way to execute a bash script is to set executable permissions.

权限执行此操作_linux 执行权限_权限执行不

[root@localhost ~]<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px"># chmod +x hello_script.sh</span>

You can see that the hello_script.sh file has been granted executable permissions.

Execute script

After assigning executable permissions to the script, you can run the script directly without the bash command to see what system Linux is, as shown below:

[root@localhost ~]<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px"># ./hello_script.sh</span>
Hello World

权限执行此操作_linux 执行权限_权限执行不

Examples

In the example below android linux, I will compile and execute a bash script to backup from the source directory to the target directory:

[root@localhost ~]<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px"># vim backup_script.sh</span>

Paste the following content into the backup_script.sh file.

<span class="hljs-meta" style="color: #9b9b9b;line-height: 26px">#!/bin/bash</span>
TIME=`date +%Y_%m_%d`
DESTINATION=/tmp/backup-<span class="hljs-variable" style="color: #bd63c5;line-height: 26px">$TIME</span>.tar.gz
SOURCE=/var/<span class="hljs-built_in" style="color: #4ec9b0;line-height: 26px">log</span>
tar -zcvf <span class="hljs-variable" style="color: #bd63c5;line-height: 26px">$DESTINATION</span> <span class="hljs-variable" style="color: #bd63c5;line-height: 26px">$SOURCE</span>

Save the script file and exit. Add executable permissions to script files:

[root@localhost ~]<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px"># chmod +x backup_script.sh</span>

运行脚本:

[root@localhost ~]<span class="hljs-comment" style="color: #57a64a;font-style: italic;line-height: 26px"># ./backup_script.sh</span>

The above is the detailed content of Multiple ways to execute shell scripts in Linux and sharing of specific methods. 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