Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add shell script execution permission under Linux

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to add shell script execution permissions under Linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to add shell script execution permissions under Linux" this article.

Create a 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

Write a simple script

Open the newly created file using the vim editor and add the following bash script to the file:

[root@localhost ~] # vim hello_script.sh

Here is the content of the script added to the file:

#! / bin/bash echo "Hello World"

After editing, save and exit.

Execute Bash script

There are two ways to run the bash file. The first is by using the bash or sh command. The other is to add executable permissions to the file and run it directly. Let's run the following command to execute the bash script using the bash or sh command.

[root@localhost ~] # sh hello_script.shHello World [root@localhost ~] # bash hello_script.shHello World

Set executable permissions for script files

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

[root@localhost ~] # chmod + x hello_script.sh

You can see that the hello_script.sh file has x executable permissions again.

Execute script

After you assign executable permissions to a script, you can run the script without the bash command, as follows:

[root@localhost ~] #. / hello_script.shHello World

Example

In the following example, I will write and execute a bash script to back up from the source directory to the destination directory:

[root@localhost ~] # vim backup_script.sh

Paste the following into the backup_script.sh file.

#! / bin/bashTIME= `date +% Yee% masks% d`DESTINATION = / tmp/backup-$TIME.tar.gzSOURCE=/var/logtar-zcvf $DESTINATION $SOURCE

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

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

Run the script:

[root@localhost ~] # / backup_script.sh above are all the contents of this article entitled "how to add shell script execution permissions under Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report