1

I have a script with while loop that reads a file and collects some values . 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:

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

3
  • You have DOS line endings in either your script or the output of the command you are reading from. Commented Oct 6, 2016 at 13:01
  • Do you meant that this is the issue? How can i change this? Commented Oct 6, 2016 at 13:14
  • stackoverflow.com/tags/bash/info Commented Oct 6, 2016 at 13:18

1 Answer 1

1

to remove DOS line endings your file, you can use this;

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

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;

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     ---  
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I removed the DOS line ending while setting the POWER variable and it just works. Thank you so much. (

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.