In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the skills of efficiently writing Bash scripts in Linux. It has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.
Shell scripting is the easiest way to learn or practice programming under Linux. This is especially true for system administrators dealing with automated tasks and developing new simple utilities or tools, to name just a few.
In this article, we'll share 10 practical tips for writing efficient and reliable bash scripts, including:
1. Write more comments in the script
This is a recommended practice that applies not only to shell scripting programs, but to all other types of programming. Commenting scripts helps you or others to understand what the different parts of your script do when they flip through your script.
For beginners, comments are defined with #signs.
# TecMint is a *** site for browsing various Linux articles
2. Exit the script when it fails
Sometimes bash may continue to execute scripts even though some commands fail, which affects the rest of the script (eventually leading to logic errors). To exit script execution when a command fails, use the following line:
#If the command fails, let the script exit execution set -o errexit #or set -e
3. Exiting scripts when Bash uses undeclared variables
Bash may also use undeclared variables that can cause logical errors. So let bash know when it tries to use an undeclared variable to exit script execution with the following line:
#If there is an unset variable, let the script exit execution set -o noun #or set -u
4. Use double quotes to quote variables
Using double quotes when quoting (using the value of a variable) helps prevent word splitting due to spaces and unnecessary matching due to wildcard recognition and expansion.
Consider the following example:
#!/ bin/bash #exit script if command fails set -o errexit #exit script if unset variables are used set -o nounset echo "Names without double quotes" echo names="Tecmint FOSSMint Linusay" for name in $names; do echo "$name" done echo "Names with double quotes" echo for name in "$names"; do echo "$name" done exit 0
Save the file and exit, then run as follows:
$ ./ names.sh
Double quotes in scripts
5. Use functions in scripts
Except for very small scripts (just a few lines of code), always remember to use functions to modularize code and make scripts more readable and reusable.
The syntax for writing functions is as follows:
function check_root(){ command1; command2; } #or check_root(){ command1; command2; }
When written in single-line code, each command is followed by a terminator:
check_root(){ command1; command2; }
6, string comparison with = rather than ==
Note that == is synonymous with =, so only use a single = for string comparisons, for example:
value1="tecmint.com" value2="fossmint.com" if [ "$value1" = "$value2" ]
Use $(command) instead of the old command
Command substitution replaces the command itself with the output of the command. Use $(command) instead of quotation marks for command substitution.
This practice is also recommended by the shellcheck tool, which displays warnings and recommendations for shell scripts. For example:
user=`echo "$UID"` user=$(echo "$UID")
8. Declare static variables with readonly
A static variable does not change; its value cannot be modified once defined in the script:
readonly passwd_file="/etc/passwd" readonly group_file="/etc/group"
Environment variables are named in upper case letters, while custom variables are named in lower case.
All bash environment variables are named in upper case letters, so name your custom variables in lower case letters to avoid name conflicts:
#Define custom variables in lowercase, and environment variables in uppercase nikto_file="$HOME/Downloads/nikto-master/program/nikto.pl" perl "$nikto_file" -h "$1" Thank you for reading this article carefully, I hope Xiaobian share "What are the skills of writing Bash scripts efficiently in Linux" This article is helpful to everyone, but also hope that everyone will support it, pay attention to the industry information channel, more relevant knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.