Posted in

如何创建你的第一个 Linux Bash 脚本_AI阅读总结 — 包阅AI

包阅导读总结

1. 关键词:Linux、Bash 脚本、终端、备份、自动化

2. 总结:

本文介绍了 Linux 的 Bash 脚本,包括其特点和用途,详细讲述了创建“Hello, World!”和备份脚本的步骤,还提到了脚本的运行方式、权限设置、自动化等内容。

3. 主要内容:

– 介绍 Linux 及 Bash 脚本

– Linux 优势,灵活性重要

– Bash 脚本特点及适用范围

– 创建“Hello, World!”脚本

– 需文本编辑器,如 nano

– 文件名加.sh 扩展名

– 首行添加`!/bin/bash`

– 包含`echo “Hello, New Stack!”`语句

– 可通过`sh`命令或设置文件为可执行后运行

– 创建备份脚本

– 设定日期、目的地、源文件夹等变量

– 使用`tar`命令执行备份

– 给予文件执行权限,可通过 sudo 运行

– 可通过 cron 作业实现自动化,需抑制命令输出

思维导图:

文章地址:https://thenewstack.io/how-to-create-your-first-linux-bash-script/

文章来源:thenewstack.io

作者:Jack Wallen

发布时间:2024/8/14 16:39

语言:英文

总字数:1086字

预计阅读时间:5分钟

评分:87分

标签:Linux,Bash 脚本,自动化,命令行,备份脚本


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

Linux has taken over so many sectors of business and done so while always remaining free, open source, secure and highly flexible. As a long-time Linux user (nearly 30 years), flexibility has always been one of the more important aspects of the operating system for me. With Linux, I can do anything. And if there isn’t an app for something, I could even write a Bash script to serve the needed purpose.

What’s a Bash script, you ask?

Have you ever written a text-based application? Maybe in Python? Bash scripts are similar, but they are limited to running within the Bash shell. In other words, if you wrote a Bash script and attempted to run it in a shell other than Bash, it would fail. No need to worry because nearly all modern Linux distributions use (and default to) Bash.

Bash scripts are terminal only (so no GUIs) and can be used for just about anything. If you can do it in the terminal, you can do it with a Bash script. Even better, Bash scripts even make it possible to automate tasks (with the help of corn).

Bash scripts are often used for backups, to set variables, update your operating system and much more. In fact, with a bit of creativity, there’s no limit to what you can do with Bash.

Fortunately, writing your first Bash script isn’t hard. Yes, you can get very intricate with these, but the basics are very easy to grasp.

I want to walk you through creating your first Bash scripts. We’ll start off easy with the tried and true “Hello, World!” app.

Creating a ‘Hello, World!’ Bash Script

As you probably guessed, you’re going to need a text editor to create your Bash scripts. Because these are command-line only, I tend to prefer a basic text editor. My text editor of choice is nano.

Create your script file with the command:

You don’t have to add the .sh extension, I do so that I can more easily discern a script file from a basic text file. Do know that .sh is the official extension for shell scripts.

The first line you’ll add is this:

What is that? The above line is often referred to as “shebang”) and informs the shell interpreter which application to use. (In this case, it’ll use bash.)

We can finish our script with a single line that is:

Our entire script looks like this:

#!/bin/bash

echo “Hello, New Stack!”

Save and close the file.

There are two ways to run the script. The first is by using the sh command (the dash command interpreter) like this:

The output will be:

There’s a better way. First, you’ll want to give the file executable permission with the command:

Now, to run the command you can run:

The reason this is the better alternative is that you could move that file to any directory in your user $PATH and then run the command from any directory in the file system hierarchy. You could move it to /usr/local/bin with the command:

sudo mv hello.sh /usr/local/bin

Once you’ve done that, change the ownership with a command like:

sudo chown $USER.$USER /usr/local/bin/hello.sh

At this point, you could run the command by simply typing:

Let’s create a backup script with Bash.

Creating a Backup Script with Bash

Let’s say you want to back up /var/www/html on a nightly basis and you want to back it up to an external drive mounted at /media/USER/backup (where USER is your username). What we’ll do is create a simple compressed tar file of the /var/www/html directory to the backup drive and attach the date to the file name.

Create a new script file with the command:

The first line in the script will be:

Next, we’re going to set a variable for the date with:

What we’ve done above is set the variable DATE with the command date +%b-%d-%y, so the variable will always be the time and date when the script is run. Take note that the command is enclosed in backticks (and not single quotes), which is required for it to function.

Next, we’ll set a variable for the destination of our backup, which is /media/USER/backup (where USER is your username). That variable is set with the line:

DESTINATION=/media/USER/backup/HTMLBACKUP-$DATE.tar.gz

The important thing to take notice of in the above line is $DATE. What that does is inform Bash that what follows the $ is a variable. In our case, it will add the current time/date after BACKUP- and before .tar.

The final variable we’ll set is the source folder (which is the folder to be backed up). In our case, the source folder is defined as:

The last piece of the puzzle is the actual command that runs for the backup, which is:

tar cpzf $DESTINATIOn $SOURCE

The entire script looks like this:

#!/bin/bash

DATE=`date +%b-%d-%y`

DESTINATION=/media/USER/backup/HTMLBACKUP-$DATE.tar.gz

SOURCE=/var/www/html

tar cpzf $DESTINATION $SOURCE

Save and close the file.

We’ll now give the file executable permissions with:

Run the script with:

We use sudo because /var/www/html/ requires elevated permissions.

You should now see a compressed archive file of your backup in the destination directory.

If you want to automate the script, I would suggest moving the file to /usr/local/bin (as I explained earlier) and then create a cron job with:

At the bottom of the file, add something like this (to run the script at midnight every day):

We have to use > /dev/null 2>&1 to suppress the output of the command, otherwise, the cron job will fail.

And that’s how you take your first steps with Bash scripts. We’ll revisit this topic another time to create more complex Bash scripts (which could include loops!).

YOUTUBE.COM/THENEWSTACK

Tech moves fast, don’t miss an episode. Subscribe to our YouTubechannel to stream all our podcasts, interviews, demos, and more.

GroupCreated with Sketch.