简体   繁体   English

echo 和 printf 不打印在 bash 循环中分配的变量

[英]echo and printf don't print variables assigned in a loop in bash

I have a script with while loop that reads a file and collects some values .我有一个带有 while 循环的脚本,可以读取文件并收集一些值。 After loop stops some variables can be manipulated again:循环停止后,可以再次操作一些变量:

POWER=$(expect cisco_stats lab-asr9k-2 | awk '!/Total:/ || ++n <= 1' | egrep -v "show envi|CEST|RSP[0-1]")

 while read -r line
    do 
        if [[ $line == "Total:"* ]]
        then t_use=$(echo "$line" | awk '{print $NF}')
            continue
        fi

        if [[ $line == *"Type:"* ]]
        then acdc=$(echo "$line" | awk '{print $NF}')
            continue
        fi

        if [[ $line == "Total Power Capacity:"* ]]
        then t_cap=$(echo "$line" | awk '{print $NF}')
            continue
        fi

        if [[ $line == "Supply Protected Capacity"* ]]
        then np1_av=$(echo "$line" | awk '{print $NF}')
            npn_av="---"
            break
        fi

        if [[ $line == "N+1 Supply Protected"* ]]

        then np1_av=$(echo "$line" | awk '{print $NF}')
            continue
        fi

        if [[ $line == "N+N Supply Protected"* ]]
        then npn_av=$(echo "$line" | awk '{print $NF}')
            break
        fi

    done <<< "$POWER"


if [[ $np1_av == *"Protected"* ]]
then np1_av="Not_Pr."
fi

if [[ $npn_av == *"Protected"* ]]
then npn_av="Not_Pr."
elif [ -z ${npn_av+x} ]
then npn_av="---"
fi

echo "$t_cap"
echo "$t_use"
echo "$acdc"
echo "$np1_av"
echo "$npn_av"

printf "%-10s %-10s %-10s %-10s %-10s\n" "Type" "Tot.cap." "In use" "N+1 prt." "N+N prt."

printf "%-10s %-10s %-10s %-10s %-10s\n" "$acdc" "$t_cap" "$t_use" "$np1_av" "$npn_av"

if I echo each variables separately - i see the correct result.如果我分别回显每个变量 - 我会看到正确的结果。 If i try to echo or printf the variables in one line, i see only variables that where set outside of the loop:如果我尝试在一行中 echo 或 printf 变量,我只会看到在循环之外设置的变量:

4200W
1266.7
DC
Not_Pr.
---
Type       Tot.cap.   In use     N+1 prt.   N+N prt.  
    Not_Pr.   

to remove DOS line endings your file, you can use this;要删除文件结尾的 DOS 行,您可以使用它;

POWER=$(expect cisco_stats lab-asr9k-2 | awk '!/Total:/ || ++n <= 1' | egrep -v "show envi|CEST|RSP[0-1]" | sed 's/\r//' )

sed 's/\\r//' is that removes carriage returns in the file sed 's/\\r//'是删除文件中的回车

eg;例如;

#!/bin/bash

# add carriage return variables
t_cap=$(echo '4200W' | sed 's/$/\r/')
t_use=$(echo 'DC' | sed 's/$/\r/')
acdc=$(echo 'Not_Pr.' | sed 's/$/\r/')
npn_av=$(echo '---' | sed 's/$/\r/')
np1_av=$(echo 'np1_av' | sed 's/$/\r/')

printf "%-10s %-10s %-10s %-10s %-10s\n" "Type" "Tot.cap." "In use" "N+1 prt." "N+N prt."
printf "%-10s %-10s %-10s %-10s %-10s\n" "$acdc" "$t_cap" "$t_use" "$np1_av" "$npn_av" 


# remove carriage returns
t_cap=$(echo $t_cap | sed 's/\r//')
t_use=$(echo $t_use | sed 's/\r//')
acdc=$(echo $acdc | sed 's/\r//')
np1_av=$(echo $np1_av | sed 's/\r//')
npn_av=$(echo $npn_av | sed 's/\r//')

printf "%-10s %-10s %-10s %-10s %-10s\n" "Type" "Tot.cap." "In use" "N+1 prt." "N+N prt."
printf "%-10s %-10s %-10s %-10s %-10s\n" "$acdc" "$t_cap" "$t_use" "$np1_av" "$npn_av"

when run this;运行时; second printf works as below;第二个 printf 的工作原理如下;

user@host:/tmp/test$ ./test.sh
Type       Tot.cap.   In use     N+1 prt.   N+N prt.  
      - np1_av
Type       Tot.cap.   In use     N+1 prt.   N+N prt.  
Not_Pr.    4200W      DC         np1_av     ---  

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM