Home > System Tutorial > LINUX > body text

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

WBOY
Release: 2024-07-24 09:51:06
Original
562 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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
Copy after login

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>
Copy after login

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
Copy after login

权限执行此操作_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>
Copy after login

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>
Copy after login

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>
Copy after login

运行脚本:

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

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!

source:itcool.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!