[Note] This article is translated from: An Introduction to Bash Scripting
Introduction to Bash scripts
Fancy yourself as a computer scientist, hobbyist, or technical nerd? Then at some point, you will or should consider using Bash scripts in your digital workspace.
Bash (Bourne Again Shell) ") is an interpreter, responsible for handling commands on Unix system command line. It is written by Brian Fox free software , and released in 1989, free software, as Sh (Bourne Shell) alternatives. Bash is used by developers, data scientists, system administrators, network engineers, and anyone else who relies heavily on the Unix operating system in their daily work. Generally speaking, Bash scripts are used to automate what computer scientists may undertake Daily remedial tasks. In short, a shell script is just a series of commands stored in a file (such as a list).
You can Linux and MacOS machines, and even use the Windows Subsystem for Linux on Windows 10 machines. Bash usually runs in a text window, where users can type commands to make the computer perform operations. The language can also be used to read and execute commands from files, called shell scripts. Shell script itself is a programming language, and like any other language, Bash is a tool that can be used in many ways.
If you have seen a machine running a Linux operating system (or Unix-like environment) before, you may have also seen a terminal console. The terminal is a way for users to interact with the shell interpreter using certain commands. Such as cd navigation file directory, ls lists the files in the current directory, and nano edit files and other commands.
Use the Bash code in the terminal and it will be run by the Bash interpreter. such as 1618ba63b7d893 ls are binary executable files located in the /bin When the shell receives the command (when you type it in the terminal and press Enter), it executes the ls file and lists the files in the current directory for the user. Use the command LS / bin the path / bin as the option to perform a binary LS , listed / bin files in the directory. Executing ls -al will run with the flags -a and -l ls ls 1618ba63b7d8ab ls 1618ba63b7d8a7 -l 1618ba63b7d8a9 as an option to list all the files and detailed information about the path of these items in the current directory.
touch is another such binary executable file, a command that users can use in the terminal. The output of this command is a new file with the name entered by the user as an option. For example, the user can write touch hello.txt and the output will be a file hello.txt .
How to run multiple Bash commands
To run multiple Bash commands and have them executed at once, users can save these commands in a single file for execution with bash. Assuming you are working in a Unix/Unix-like environment, let us consider what we discussed earlier.
After opening the command terminal, first use your favorite text editor, such as nano or vi . Write:
nano make_a_file.txt
Then, write the following:
#create a file
touch hello.txt
#list files from this directory
ls -al
Save and exit the file, and run the new script using one of the following command syntax:
sh make_a_file.txt
or
./make_a_file.txt
or
bash make_a_file.txt
If an error occurs when executing the file, please continue to set the executable permissions for the script file you just wrote by entering the following:
chmod +x hello.sh
If you followed this example, you have just created a file containing multiple Bash commands. The Bash interpreter will run these commands in order and ignore the lines beginning with the hash symbol # because these lines are comments. Running this file will produce a terminal output of a file list, which will contain a hello.txt , which did not exist before.
Usually, a Bash script file is .sh extension, which indicates that the file is a shell script. However, when the file starts with "she-bang" or "hashbang", we can execute it like a binary file.
When creating scripts, we should take into account that every binary shell file starts with what is commonly known as "she-bang" (also known as sh-bang or hashbang). This is the beginning of the script title, and the first line of code indicates which shell you will use. When making scripts, we have a variety of options to choose from, including shell (sh) , C Shell , Z Shell etc. In this case, we will continue to use Bash to meet our scripting needs. She-bang is a set of symbols “#” and “!” at the beginning of the script. We know that the pound sign (#) means a line is a comment. However, with she-bang, the program interpreter of Unix-like systems will parse the rest of the first line into interpreter instructions. In this case, when writing #!/bin/bash , the hash symbol and the exclamation point are used as indicators of the program loader, instructing it to use the Bash Shell program /bin/bash
How to create variables in Bash
Like most other Unix shells, Bash has variables, pipes, file name wildcards, here documents, command substitution, and control flow. Bash also supports alternation (which is shared with the C shell), command line completion, and signal processing and basic debugging. With these features, bash has become the default command interpreter for Unix and Unix-like systems.
Like other programming languages, we can declare variables when writing scripts in Bash. However, unlike other languages, Bash does not require keywords to declare variables or assign data types to them. Bash has no type system and only saves variables as string values. However, the Bash can automatically converted according to certain operations (e.g. arithmetic) variable to the appropriate type. To write a variable and fill it with a value, please VARIABLE=VALUE , making sure that it does not contain spaces. Here is an example showing how to create variables in Bash:
#!/bin/bash
#write a variable
NAME=“William”
#use that variable
echo “Hello $NAME”
The user can also fill in variables through user input:
#!/bin/bash
echo “Hello $1, that is a $2 name”
In the terminal:
~$bash name.sh “William” “great”
Hello William, that is a great name
You can also use read to use variables entered by the user at runtime:
#!/bin/bash
echo “What is your name?”
read name
echo “Hello $name”
In the terminal:
~$bash name.sh
What is your name?
~$William
Hello William
If statement in Bash
We can also implement if statements for additional functions.
#!/bin/bash
echo “Who is there?”
read name
if [ $name ]
echo “Hello $name”
else
echo “Must’ve been my imagination”
fi
In the terminal:
~$bash name.sh
Who is there?
~$
Must’ve been my imagination
How to create a backup management script in Bash
Other items to consider include setting up backup management scripts. This can be a simple project that can be started and revisited later. With this, you can make a simple script that uses a tar library to compress one or more files and folders and place them in a new backup directory of your choice. The following script is a basic backup script that creates a .Zip file for the files that need to be backed up and marks them based on the creation date:
#!/bin/bash
#get the month, day, and year of the current date
TIME_OF_BACKUP=`date +%m-%d-%y`
#create a backup file using the current date in its name
DESTINATION=/path/[BACKUP FOLDER]-$TIME_OF_BACKUP.tar.gz
#the folder that contains the files that we want to backup
TARGET_FOLDER=/path/[TARGET FOLDER]
#create the backup
tar -cpzf $DESTINATION $TARGET_FOLDER
As a bonus, you may want to increase some of the complexity of this project and increase the level of automation by adding scheduled execution to the backup script. For this, you can use the crontab program and command library. If you need to install cron, please make sure to update your current package repository before proceeding to install cron.
sudo apt-get update
sudo apt-get install cron
After successful installation, you can continue to use the cron library to schedule the execution of the script.
crontab -e
This will open the /etc/crontab file and allow you to write the following commands to schedule the execution of the script:
@weekly /path/backup_script.sh
I will not discuss further what you can do with crontab , because it is beyond the scope of this article.
Summarize
Through this getting started guide, you will have an understanding of what Bash is, what is a script, and what scripts in Bash are. You can do many things with Bash, and you don't need to know much about programming to piece together different Linux applications and tools and make some useful things. Bash script is a very useful tool, I hope you can get inspiration from this article to automate your ideas.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。