10 Useeful Tips for Writing Effective Bash Scripts in Linux

本文分享了提高Bash脚本质量的十个技巧,包括使用注释、确保脚本失败时退出、引用变量时使用双引号等。还介绍了如何通过函数模块化脚本、正确进行字符串比较以及调试技巧等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Always Use Comments in Scripts
2.Make a Scripts exit When Fails
    Sometimes bash may continue to execute a script even when a certain command fails.thus affecting the rest of the script (may eventually result in logical errors).Use the Command below to exit a script when a command fails.
    #let script exit if a command fails(remember:this command should use at the top of the scripts)
    set -o errexit
    OR
    set -e
3.Make a Scripts exit When Bash uses Undeclared Variable
    Bash may also try to use an undeclared script which could cause a logical error.Therefore use the following line to instruct bash to exit a script when it attemps to use an undeclared variable.
    #let script exit if an unused variable is used
    set -o nounset
    OR
    set -u
4.Use double quotes to Reference variables
    Use double quotes while referencing (using a value of a variable) helps to prevent word splitting (regarding whitespace) and unnecessary globbing匹配(recognizing and expanding wildcards)
    eg:
        #!/bin/bash
        #let a script exit if a command fails
        set -o errexit
        #let a script exit if an unsed variable is used
        set -o nounset
        echo "Names without double quotes"
        echo
        names="Michael Jordan"
        for name in $names;do
        echo "$name"
        done
        
        echo
        echo "Names with double quotes"
        echo
        for name in "$names"; do
        echo "$name"
        done
        exit 0
    (
        1.echo 单独执行表示换行
        2.for 变量 in 列表
          do
            语句
          done
    )

5.Use Functions in Scripts
    Expect for very small scripts (with a few lines of code ),always remember to use functions to modularize your code and make scripts more readable and reusable)
    The sysntax for writing functions is as follows:
    function chech_root(){
    command1;
    command2;
    }
    OR
    chech_root(){
    command1;
    command2;
    }
6.Use = instead of == for String Comparisons
    Note that == is a synonym同义词 for = ,therefore use only a single = for string comparisions,for instance
    value1="baidu.com"
    value2="dubai.com"
    if["$value1"="$value2"]
7.Use $(command) instead of legacy `command` for substitution(替换)
    user=`echo "$UID"`
    user=$(echo "$UID")
8.Use Read-only to Declare Static Variables
    A static variable doesn't change;its value can not be altered once it's defined in a script
    eg:readonly passwd_file="/etc/passwd"
        readonly group_file="/etc/group"
9.Use Uppercase Names for ENVIRONMENT Variables and Lowercase for Custom Variables
    All bash environment variables are named with uppercase letters ,therefore use lowercase letters to name your custom variables to avoid variable name conflicts
10.Always Perform Debugging for Long Scripts
    Methods of Enabling Shell Script Debugging Mode
    Below are the primary shell script debugging options:
    -v (short for verbose冗长) – tells the shell to show all lines in a script while they are read, it activates verbose mode.
    -n (short for noexec or no ecxecution) – instructs the shell read all the commands, however doesn’t execute them. This options activates syntax checking mode.
    -x (short for xtrace or execution trace) – tells the shell to display all commands and their arguments on the terminal while they are executed. This option enables shell tracing mode.

    

转载于:https://www.cnblogs.com/jycjy/p/7000301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值