

 CLI sucks, help me learn it. Parsing Output. - marknadal
http://stackoverflow.com/questions/11495526/how-to-assign-cli-variables-from-cli-output

======
ColinWright
Firstly, read this: <http://www.catb.org/~esr/faqs/smart-questions.html>

Now, I take it you are running one command and need to take a portion of its
output and use it in the construction of another command - is that right?

So:

* What is the first command you're running?

* What output do you get?

* From that, what command do you want to construct?

* How much do you know about grep, sed, awk, and bash?

Be clear, precise, specific, and only waffle about generalities after you've
solved a single instance.

~~~
marknadal
Yes.

Thanks, most of the last two years of using CLI have all been using "single
specific scripts", so I'm getting sick of not actually knowing how CLI works
and being a confused, brainless C&P robot. Everybody who writes the tutorials
and provides me with the scripts seem to know what they are doing, but it is
hard to figure it out. Thanks for the link.

In terms of specifics, it is the ones I mention at the bottom of the post,
regarding finding EBS blocks with lsblk, passing the names into an MDADM
command, and then the rest of the script doesn't need anything more dynamic.

I will look up grep/sed/awk (I'm writing a bash script). From what I know of
them so far, they are just regexp, which concerns me, because that feels like
a very dangerous "hacky" way of performing very delicate and important tasks.
But it seems that everybody is saying that is the only way to handle outputs.

Anything else? Thanks for your help.

~~~
ColinWright
This is going to look like a rant. In a way it is, but I am honestly trying to
help you, not just in this issue, but also in getting help in general. Bear
with me.

OK, so it looks to me like you didn't actually answer the questions I asked.
In particular, I asked:

    
    
      > * What is the first command you're running?
      > * What output do you get?
      > * From that, what command do you want to construct?
    

I also said:

    
    
      > Be clear, precise, specific, ...
    

You are asking me for help, I have indicated that I'm willing to help you, but
you aren't actually answering the questions I ask, and it appears that you're
not actually paying attention to what I write. If I'm going to give up some of
my time to help you, someone I've never met, then you really, _really_ should
make it easy for me.

Instead you've made me go back to the original posting on StackOverflow and
try again to understand the exact question, even though I've clearly asked for
some clarification.

For example:

    
    
      > lsblk -l -list_the_names_of_blocks_not_mounted
    

I know nothing about the lsblk command, I don't know if these are real
parameters or part of the "pseudo code" you've invented to explain your
problem, and I certainly don't know what you're expecting it to output. So let
me repeat:

    
    
      > * What is the first command you're running?
      > * What output do you get?
    

I assume (and it appears that you've confirmed) that you need to extract some
information from the output and use it to construct a new command. That's
pretty easy to do and I'll happily talk you through the process. Mostly these
processes use standard tools like grep, sed, and awk, but it would appear at
the time of writing that all you have of them is a negative impression that
"they are just regexp" and as such that "feels like a very dangerous 'hacky'
way of performing very delicate and important tasks."

Perhaps you should judge that after you've actually seen them in operation for
real. If you still consider them "hacky" then you will have acquired enough
information to do it all in Python (or other languages) if you choose.

So, if you'd still like me to help, please re-read this reply carefully, in
full, and you'll know how to proceed.

 _Added in edit: I've just read your comment on the one reply you've had over
there, and now I really don't know what you're expecting. In the CLI you can
take the output of one command and feed it into a pipeline that does stuff to
it. What more are you expecting than tools designed to work with a pipeline of
text?_

~~~
marknadal
Thanks, Colin.

1) MDADM requires specific parameters in order to mount physical drives into a
virtual RAID drive. 2) Meaning, piping outputs doesn't work, since the text
that is output does not equal the parameters needed. 3) First, the script has
to automatically determine which drives should be used. This is done by
calling lsblk -l (easier to parse than fdisk) and looking for non-mounted
partitions. It outputs:

    
    
      NAME MAJ:MIN RM   SIZE RO MOUNTPOINT
      sr0   11:0    1  49.8M  0 /media/VBOXADDITIONS_4.1.18_78361
      sda    8:0    0     8G  0 
      sda1   8:1    0   7.5G  0 /
      sda2   8:2    0     1K  0 
      sda5   8:5    0   510M  0 [SWAP]
    

4) MDADM's command parameter is the following:

sudo mdadm --create --verbose /dev/md0 --level=10 --chunk=256 --raid-
devices=$RNUM $DRIVES

Where $RNUM is an integer corresponding to the number of drives, and $DRIVES
is a space-separated list of the paths to the unmounted drives (which should
probably further be filtered on size, since one of them is only 1K but let's
ignore its size for now). We also have to remember to prefix '/dev/' since
that is where the drives are mounted onto. So explicitly:

DRIVES=/dev/sda /dev/sda2 RNUM=2

Which then makes:

5) sudo mdadm --create --verbose /dev/md0 --level=10 --chunk=256 --raid-
devices=2 /dev/sda /dev/sda2

Notes: a) I've been able to accomplish this using node, since in JS I can just
do things like string.split(" ") and get an array which I can loop through, or
split on newlines, or whatever. But I don't know how this is accomplished in
bash scripting.

Thanks for helping out somebody very unintelligent with respect to CLI,
despite the last year and a half being fully involved, and trying to google
the help out of every issue and tutorials. I hope some day I'll be good enough
to help teach people as well.

