gpt4 book ai didi

shell - 从外部脚本调用内部脚本

转载 作者:行者123 更新时间:2023-12-01 13:48:08 26 4
gpt4 key购买 nike

我有一个名为 build_other 的脚本:

count=0
while [ $count -lt 6 ]
do
./build
count+=1
done

从这个脚本中,您可以看到我正在调用第二个脚本,名为 build:

echo "building job"
sleep 30s
echo "wake after sleep"
echo "file build" >> output.txt

我预计 build 应该被触发 5 次,因为 countbuild 运行后增加 1。这意味着 output.txt 应该有 5 行说明 file build。以下是我实际收到的输出。

building job
wake after sleep
building job
wake after sleep

output.txt 文件只有两行。

file build
file build

为什么循环没有像我预期的那样运行 5 次?

最佳答案

+= 运算符并没有按照您的想法进行操作;它不是“添加到”运算符,而是充当字符串连接运算符。

$ ./foo.sh
./build
count is now 01
./build
count is now 011

经过两次迭代,count011。使用数字小于运算符 -lt,字符串 011 被转换为数字 11。由于11不小于6,循环结束。

假设您使用的是 bash 或其他现代 Bourne 系列 shell,您可以通过几种方式解决此问题。

# Using arithmetic expansion -- $(( expression ))
count=$((count + 1))

# Increment variable in an arithmetic evaluation context -- (( ))
(( count++ ))

# When declaring count, declare that it is an integer value. Declared an
# integer, the += operator will behave as you originally expected it would.
declare -i count=0

下面是 bash 手册页的一些摘录。

关于 += 运算符的行为:

In the context where an assignment statement is assigning a value to a shell variable or array index, the += operator can be used to append to or add to the variable's previous value. When += is applied to a variable for which the integer attribute has been set, value is evaluated as an arithmetic expression and added to the variable's current value, which is also evaluated. When += is applied to an array variable using compound assignment (see Arrays below), the variable's value is not unset (as it is when using =), and new values are appended to the array beginning at one greater than the array's maximum index (for indexed arrays) or added as additional key-value pairs in an associative array. When applied to a string-valued variable, value is expanded and appended to the variable's value.

关于算术评估:

Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:

$((expression))

The old format $[expression] is deprecated and will be removed in upcoming versions of bash.

The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter and variable expansion, command substitution, and quote removal. The result is treated as the arithmetic expression to be evaluated. Arithmetic expansions may be nested.

关于算术评估上下文:

((expression))

The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".

关于shell - 从外部脚本调用内部脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34231588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com