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
489 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 ~]# touch hello_script.sh
Copy after login

Write a simple script

Open the newly created file with vim editorlinux execution permissionsand add the following bash script to the file:

[root@localhost ~]# vim hello_script.sh
Copy after login

The following is the script content added to the file:

#!/bin/bash echo "Hello World"
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 filelinux 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 ~]# sh hello_script.sh Hello World [root@localhost ~]# bash hello_script.sh 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 ~]# chmod +x hello_script.sh
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 ~]# ./hello_script.sh 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 ~]# vim backup_script.sh
Copy after login

Paste the following content into the backup_script.sh file.

#!/bin/bash TIME=`date +%Y_%m_%d` DESTINATION=/tmp/backup-$TIME.tar.gz SOURCE=/var/log tar -zcvf $DESTINATION $SOURCE
Copy after login

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

[root@localhost ~]# chmod +x backup_script.sh
Copy after login

运行脚本:

[root@localhost ~]# ./backup_script.sh
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
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!