Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Set multiple variables from output of 1 command?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
finalturismo
Guru
Guru


Joined: 06 Jan 2020
Posts: 410

PostPosted: Sat Oct 15, 2022 5:01 pm    Post subject: Set multiple variables from output of 1 command? Reply with quote

Ok so lets say i want to set multiple variables from the output of 1 command without running the command multiple times to set the variables.....

How would i go about doing this?

For example i want to create a variable based on each stat from the output of zfs get all rpool


Code:
NAME   PROPERTY              VALUE                     SOURCE
rpool  type                  filesystem                -
rpool  creation              Sat Jun 11 20:36 2022     -
rpool  used                  1.27T                     -
rpool  available             620G                      -
rpool  referenced            316G                      -
rpool  compressratio         1.16x                     -
rpool  mounted               yes                       -
rpool  quota                 none                      default
rpool  reservation           20G                       local
rpool  recordsize            16K                       local
rpool  mountpoint            /                         local
rpool  sharenfs              off                       local
rpool  checksum              edonr                     local
rpool  compression           lz4                       local
rpool  atime                 on                        local
rpool  devices               on                        default
rpool  exec                  on                        default
rpool  setuid                on                        default
rpool  readonly              off                       default
rpool  zoned                 off                       default
rpool  snapdir               hidden                    default
rpool  aclmode               passthrough               local
rpool  aclinherit            passthrough               local
rpool  createtxg             1                         -
rpool  canmount              on                        local
rpool  xattr                 sa                        local
rpool  copies                1                         local
rpool  version               5                         -
rpool  utf8only              off                       -
rpool  normalization         none                      -
rpool  casesensitivity       sensitive                 -
rpool  vscan                 off                       default
rpool  nbmand                off                       local
rpool  sharesmb              off                       local
rpool  refquota              none                      default
rpool  refreservation        20G                       local
rpool  guid                  11335222168571905936      -
rpool  primarycache          all                       local
rpool  secondarycache        all                       local
rpool  usedbysnapshots       333G                      -
rpool  usedbydataset         316G                      -
rpool  usedbychildren        647G                      -
rpool  usedbyrefreservation  0B                        -
rpool  logbias               latency                   local
rpool  objsetid              54                        -
rpool  dedup                 edonr,verify              local
rpool  mlslabel              none                      default
rpool  sync                  standard                  local
rpool  dnodesize             16k                       local
rpool  refcompressratio      1.19x                     -
rpool  written               74.9G                     -
rpool  logicalused           1.12T                     -
rpool  logicalreferenced     338G                      -
rpool  volmode               full                      local
rpool  filesystem_limit      none                      default
rpool  snapshot_limit        none                      local
rpool  filesystem_count      2                         local
rpool  snapshot_count        76                        local
rpool  snapdev               hidden                    default
rpool  acltype               nfsv4                     local
rpool  context               none                      default
rpool  fscontext             none                      default
rpool  defcontext            none                      default
rpool  rootcontext           none                      local
rpool  relatime              on                        local
rpool  redundant_metadata    all                       local
rpool  overlay               off                       local
rpool  encryption            off                       default
rpool  keylocation           none                      default
rpool  keyformat             none                      default
rpool  pbkdf2iters           0                         default
rpool  special_small_blocks  0                         local
rpool  snapshots_changed     Sat Oct 15  9:00:01 2022  local




i would like to make the variables simple by nature so for example

rpoolencryption=off
rpoorelatime=on
rpoolsnapdev=on


I guess i pretty much want to merge col 1 and 2 and make it =col3 for the variable.

so far i got this but which does that but i need to xargs it to go back to the same file the script is running at on a specific line. like xargs -I {} echo {} > $0 but at a specific line?
Code:
zfs get all rpool | awk '{print $1$2"="$3}'
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23026

PostPosted: Sat Oct 15, 2022 5:34 pm    Post subject: Re: Set multiple variables from output of 1 command? Reply with quote

Can you get the output to be less ambiguous? As shown, some of your lines have 4 columns. Some have many more columns, such as creation, which has the following columns:
  • rpool
  • creation
  • Sat
  • Jun
  • 11
  • 20:36
  • 2022
  • -
The easiest fix for that would be to get the output to be printed using tab separators, assuming tabs never appear in the value field.

Ignoring that problem, this is easy:

bash required due to process substitution:
Code:
while read name property value source; do
    printf -v "$name$property" '%s' "$value"
done < <( zfs get all rpool )
Back to top
View user's profile Send private message
finalturismo
Guru
Guru


Joined: 06 Jan 2020
Posts: 410

PostPosted: Sat Oct 15, 2022 6:18 pm    Post subject: Re: Set multiple variables from output of 1 command? Reply with quote

Hu wrote:
Can you get the output to be less ambiguous? As shown, some of your lines have 4 columns. Some have many more columns, such as creation, which has the following columns:
  • rpool
  • creation
  • Sat
  • Jun
  • 11
  • 20:36
  • 2022
  • -
The easiest fix for that would be to get the output to be printed using tab separators, assuming tabs never appear in the value field.

Ignoring that problem, this is easy:

bash required due to process substitution:
Code:
while read name property value source; do
    printf -v "$name$property" '%s' "$value"
done < <( zfs get all rpool )


wow thanks
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23026

PostPosted: Sat Oct 15, 2022 7:01 pm    Post subject: Reply with quote

If you can't get zfs to produce machine-readable output, you could try to slice up the output based on column positions. That is fairly simple if we assume fixed width columns. It is rather messier if we assume variable width columns, and need to dynamically detect column width from the header. It is unreliable if the column headers are of fixed position, and the excessively wide fields simply overflow into the next column.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20581

PostPosted: Tue Oct 18, 2022 10:46 pm    Post subject: Reply with quote

You may want to check output options. At least for a seemingly old version (2010 maybe?), there are/were options -H and -o.

Quote:
Querying ZFS Properties for Scripting

The zfs get command supports the -H and -o options, which are designed for scripting. You can use the -H option to omit header information and to replace white space with the Tab character. Uniform white space allows for easily parseable data. You can use the -o option to customize the output in the following ways:

The literal name can be used with a comma-separated list of properties as defined in the Introducing ZFS Properties section.

A comma-separated list of literal fields, name, value, property, and source, to be output followed by a space and an argument, which is a comma-separated list of properties.
Code:
https://docs.ora cle.com/ cd/E19253-01/819-5461/gazuk/index.html

_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum