简体   繁体   English

有人可以帮我处理这些 shell 脚本行吗? 我无法理解在这些行中使用 DEBUG 是什么

[英]Can someone please help me with these shell script lines. I can' understand what is the use DEBUG in these lines

# Define DEBUG=true on cmdline, if needed DEBUG=${DEBUG:-false} [[ ${DEBUG} = true ]] || DEBUG="false"

Explanation解释

The first assignment sets the variable DEBUG to false if the contents of DEBUG is empty.如果DEBUG的内容为空,则第一个赋值将变量DEBUGfalse Please note that if DEBUG has not been initialized yet, it is also considered as empty and thus will receive the false value.请注意,如果DEBUG还没有被初始化,它也被认为是空的,因此会收到false值。

The second line uses the OR operator using ||第二行使用 OR 运算符使用|| , it is exactly the same as: ,它与以下完全相同:

if ! [[ ${DEBUG} = true ]]
then
  DEBUG="false"
fi

In other words, it assigns the value false to your variable if and only DEBUG does NOT equal to true .换句话说,当且仅当DEBUG不等于true ,它会将值false分配给您的变量。

In the end, the purpose of this code is to define the DEBUG variable to either true or false and nothing else, using the default value false if the variable does not contain a boolean value.最后,这段代码的目的是将DEBUG变量定义为truefalse ,如果变量不包含布尔值,则使用默认值false

Improvements改进

I think the first assignment is pointless because if the variable was empty at the beginning of this code, then the last line will assign the variable to false anyways.我认为第一次赋值毫无意义,因为如果此代码开头的变量为空,那么最后一行无论如何都会将该变量赋值为false

Also, the ${DEBUG} should be protected with quotes, eg [[ "${DEBUG}" = true ]] instead of [[ ${DEBUG} = true ]] because your script may not work properly if the variable contains whitespaces.此外, ${DEBUG}应该用引号保护,例如[[ "${DEBUG}" = true ]]而不是[[ ${DEBUG} = true ]]因为如果变量包含空格,您的脚本可能无法正常工作.

A bit overkill maybe, but I encourage checking case sensitivity and use a fallback code.可能有点矫枉过正,但我​​鼓励检查区分大小写并使用回退代码。

Let's say the user defines DEBUG=TRUE (uppercase) prior to calling your script, the value of DEBUG will be forced to false .假设用户在调用脚本之前定义了DEBUG=TRUE (大写), DEBUG的值将被强制为false I guess the user would not be very pleased.我猜用户不会很高兴。

The following code solves all those issues:以下代码解决了所有这些问题:

# Define DEBUG=true on cmdline, if needed

DEBUG=${DEBUG,,} # Force the variable to lowercase
[[ "${DEBUG}" = true ]] || DEBUG=false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以帮助我了解如何使用 pyModbus 将字符串数据编码到数字显示板吗? - Can someone help me understand how to use pyModbus to encode string data to a digital display board? 有人可以帮我翻译这行代码为 bash shell 吗? - Can someone help me to translate this line of code for bash shell? 有人可以帮我解释一下机器人在做什么吗? - can someone explain for me what the bot was trying to do please? 有人可以给我解释一下Pointer的用法吗? - Can someone please explain me this Pointer usage? 如何在 shell 脚本中显示两个文件之间不同的前五行? - How can i display the first five lines that differ between 2 files, in a shell script? 有人可以帮我分析这些valgrind输出吗? - Can someone help me to analyze these valgrind output? 有人可以帮我缩小这段代码吗? - can someone help me make this code smaller? 我可以在 Linux bash 脚本中使用什么命令将数值数据的可变行汇总为两个单独的变量,然后打印出来? - What command can I use in Linux bash script for summing up variable lines of numeric data into two separate variables that get printed out? 如何在shell脚本中使用行作为可选函数? - How to use lines as a selectable function in shell script? 有人可以帮我执行这些命令吗? - Can someone help me with executing these commands?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM