Skip to main content
added recommendation to use a better language
Source Link
cas
  • 84.1k
  • 9
  • 136
  • 205

The simplest solution is to assign an array (i.e. enclosed in parentheses) rather than a string to $status.

Also set IFS to a newline (\n) so that it puts each line (rather than each word) into a separate array element:

$ IFS=$'\n' status=( $(upsc myups 2>/dev/null | grep '^battery\.') )

$ printf "%s\n" "${status[@]}"
battery.capacity: 9.00
battery.charge: 90
battery.charge.low: 20
battery.charge.restart: 0
battery.energysave: no
battery.protection: yes

$ declare -p status   # reformatted slightly for readability.
declare -a status='([0]="battery.capacity: 9.00" [1]="battery.charge: 90"
                    [2]="battery.charge.low: 20" [3]="battery.charge.restart: 0"
                    [4]="battery.energysave: no" [5]="battery.protection: yes")'

PS: If you're going to do significantly more processing with these upsc values, I strongly recommend using perl or awk or python instead of bash - they're all far better suited to writing complex text-processing tools than bash alone.

The simplest solution is to assign an array (i.e. enclosed in parentheses) rather than a string to $status.

Also set IFS to a newline (\n) so that it puts each line (rather than each word) into a separate array element:

$ IFS=$'\n' status=( $(upsc myups 2>/dev/null | grep '^battery\.') )

$ printf "%s\n" "${status[@]}"
battery.capacity: 9.00
battery.charge: 90
battery.charge.low: 20
battery.charge.restart: 0
battery.energysave: no
battery.protection: yes

$ declare -p status   # reformatted slightly for readability.
declare -a status='([0]="battery.capacity: 9.00" [1]="battery.charge: 90"
                    [2]="battery.charge.low: 20" [3]="battery.charge.restart: 0"
                    [4]="battery.energysave: no" [5]="battery.protection: yes")'

The simplest solution is to assign an array (i.e. enclosed in parentheses) rather than a string to $status.

Also set IFS to a newline (\n) so that it puts each line (rather than each word) into a separate array element:

$ IFS=$'\n' status=( $(upsc myups 2>/dev/null | grep '^battery\.') )

$ printf "%s\n" "${status[@]}"
battery.capacity: 9.00
battery.charge: 90
battery.charge.low: 20
battery.charge.restart: 0
battery.energysave: no
battery.protection: yes

$ declare -p status   # reformatted slightly for readability.
declare -a status='([0]="battery.capacity: 9.00" [1]="battery.charge: 90"
                    [2]="battery.charge.low: 20" [3]="battery.charge.restart: 0"
                    [4]="battery.energysave: no" [5]="battery.protection: yes")'

PS: If you're going to do significantly more processing with these upsc values, I strongly recommend using perl or awk or python instead of bash - they're all far better suited to writing complex text-processing tools than bash alone.

redirect stderr to /dev/null and add grep after `upsc` command.
Source Link
cas
  • 84.1k
  • 9
  • 136
  • 205

The simplest solution is to assign an array (i.e. enclosed in parentheses) rather than a string to $status.

Also set IFS to a newline (\n) so that it puts each line (rather than each word) into a separate array element:

$ IFS=$'\n' status=( $(upsc myups 2>/dev/null | grep '^battery\.') )

$ printf "%s\n" "${status[@]}"
battery.capacity: 9.00
battery.charge: 90
battery.charge.low: 20
battery.charge.restart: 0
battery.energysave: no
battery.protection: yes

$ declare -p status   # reformatted slightly for readability.
declare -a status='([0]="battery.capacity: 9.00" [1]="battery.charge: 90"
                    [2]="battery.charge.low: 20" [3]="battery.charge.restart: 0"
                    [4]="battery.energysave: no" [5]="battery.protection: yes")'

The simplest solution is to assign an array (i.e. enclosed in parentheses) rather than a string to $status.

Also set IFS to a newline (\n) so that it puts each line (rather than each word) into a separate array element:

$ IFS=$'\n' status=( $(upsc myups) )

$ printf "%s\n" "${status[@]}"
battery.capacity: 9.00
battery.charge: 90
battery.charge.low: 20
battery.charge.restart: 0
battery.energysave: no
battery.protection: yes

$ declare -p status   # reformatted slightly for readability.
declare -a status='([0]="battery.capacity: 9.00" [1]="battery.charge: 90"
                    [2]="battery.charge.low: 20" [3]="battery.charge.restart: 0"
                    [4]="battery.energysave: no" [5]="battery.protection: yes")'

The simplest solution is to assign an array (i.e. enclosed in parentheses) rather than a string to $status.

Also set IFS to a newline (\n) so that it puts each line (rather than each word) into a separate array element:

$ IFS=$'\n' status=( $(upsc myups 2>/dev/null | grep '^battery\.') )

$ printf "%s\n" "${status[@]}"
battery.capacity: 9.00
battery.charge: 90
battery.charge.low: 20
battery.charge.restart: 0
battery.energysave: no
battery.protection: yes

$ declare -p status   # reformatted slightly for readability.
declare -a status='([0]="battery.capacity: 9.00" [1]="battery.charge: 90"
                    [2]="battery.charge.low: 20" [3]="battery.charge.restart: 0"
                    [4]="battery.energysave: no" [5]="battery.protection: yes")'
Source Link
cas
  • 84.1k
  • 9
  • 136
  • 205

The simplest solution is to assign an array (i.e. enclosed in parentheses) rather than a string to $status.

Also set IFS to a newline (\n) so that it puts each line (rather than each word) into a separate array element:

$ IFS=$'\n' status=( $(upsc myups) )

$ printf "%s\n" "${status[@]}"
battery.capacity: 9.00
battery.charge: 90
battery.charge.low: 20
battery.charge.restart: 0
battery.energysave: no
battery.protection: yes

$ declare -p status   # reformatted slightly for readability.
declare -a status='([0]="battery.capacity: 9.00" [1]="battery.charge: 90"
                    [2]="battery.charge.low: 20" [3]="battery.charge.restart: 0"
                    [4]="battery.energysave: no" [5]="battery.protection: yes")'