相关疑难解决方法(0)

Bash变量范围

请解释为什么最后一个"回声"声明是空白的?我希望它在while循环中增加到值1:

#!/bin/bash
OUTPUT="name1 ip ip status" # normally output of another command with multi line output

if [ -z "$OUTPUT" ]
then
        echo "Status WARN: No messages from SMcli"
        exit $STATE_WARNING
else
        echo "$OUTPUT"|while read NAME IP1 IP2 STATUS
        do
                if [ "$STATUS" != "Optimal" ]
                then
                        echo "CRIT: $NAME - $STATUS"
                        echo $((++XCODE))
                else
                        echo "OK: $NAME - $STATUS"
                fi
        done
fi

echo $XCODE
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下语句而不是++ XCODE方法

XCODE=`expr $XCODE + 1`
Run Code Online (Sandbox Code Playgroud)

它也不会在while语句之外打印.我想我在这里缺少一些关于变量范围的东西,但是ol'手册页并没有显示给我.

bash scope pipe

102
推荐指数
3
解决办法
8万
查看次数

如何在bash中使用全局数组?

我正在搜索SO和谷歌搜索我的问题2天,但我没有运气.我的问题是我有一个数组,我用0来启动它的元素,并在\ while循环范围内修改其元素的值.但是当我在循环外部打印数组的值时,我发现它的初始值都是0!

这是我的代码:

#!/bin/bash
#
# This is a script to reformat CSV files into pretty-formatted txt files.

clear

echo 'This is a script to reformat CSV files into pretty-formatted txt files.'
echo

# Get number of elements.
elements=$(cat CSV_File | sed -n 1'p' | tr ',' '\n' | wc -l)
# "cat CSV_File" for getting text of the file.
# "sed -n 1'p'" for getting only first line.
# "tr ',' '\n'" for replacing ',' with '\n'.
# "wc …
Run Code Online (Sandbox Code Playgroud)

linux bash shell ubuntu

2
推荐指数
1
解决办法
6894
查看次数

标签 统计

bash ×2

linux ×1

pipe ×1

scope ×1

shell ×1

ubuntu ×1