Beginners Guide To Piping?

Hello,

I have to learn how to pipe effectively in Linux, I was wondering if anyone know any books / papers / links that explains this from the ground up? What I have found so far has been pretty gash and while I get the fact that it’s stdin and stdout and std2(error) or whatever I don’t understand how linked programs know what info is coming in and out etc?

Hope this makes sense

Regards,

Ultirian

Have a read:
https://www.gnu.org/software/bash/manual/html_node/Redirections.html

6 Likes

Joking aside, lemme take a crack at your question here:

So programs get a file descriptor for standard input, standard output and standard error. That’d be stdin, stdout and stderr.

unix allows you to use the pipe | to connect stdout of program A to stdin of program B.

You can also use output redirection to, for example, connect stderr and stdout, like so: 2>&1

Pipes and redirects are a feature of all unix shells.

3 Likes

df -h / | grep -v Filesystem | sed 's/ \{1,\}/g'

Why would anyone care about the data structure this creates?

Well - it creats a comma separated string of a fedora/sles/any BTRFS linux file systems root file systems root disk.

Ok who cares?

Well if I want to cut the percent left of the OS I can do this:

df -h / | grep -v Filesystem | sed 's/ \{1,\}/g' | cut -d',' -f5

for me that returns 56% as a string.

Ok cool. Let’s say you use something like ansible, and you understand python. Let’s say you run that as a shell module and then run a python-like “split” on the “,” you have now created an array. You can now access this data structure using traditional references.

For example, in pseudo-code:

diskspace =  df -h / | grep -v Filesystem | sed 's/ \{1,\}/g

print diskspace[5]

If that doesn’t make sense, you should want it to. Start by man paging everything between the pipes. I’ve just provided a nugget of gold. However, I’m happy to answers to any questions because I’m willing to pay it forward.

1 Like

It creates a byte stream. Every time.

Well, the pipes.

Respectfully, not true.

1 Like

oh?

Yes, if you run something like:

  shell: df -h / | grep -v Filesystem | sed 's/ \{1,\}/g
  register: cool

- debug:
    msg: cool.stdout

...
Do something with cool.stdout

cool.stdout is string

Counter point:

- name
  shell: zfs send storage/music
  register: zfslol

- debug:
    msg: zfslol.stdout

I do not expect ansible to react well with this. please don’t try this.

Huh? Is that even comparable? ZFS vs df -h?

Well, I thought we were talking about the stdout/stdin

Maybe I misunderstood your intent

Wait? How did you even come to that?

I gave specific command!

Anyways -

What does df -h / report on zfs

Same shit

Same shit like a string of shitty spaced data? Like below?

image

Yep

Dude! You can trim out the table header line with grep -v stuff and then remove the space and replace with commas.

I know you well enough to know you understand how a comma separated string can be used on a “split” method to do all sorts of fun stuff programmatically.

image

You could tear that string up with a split method and python in 5 seconds, no doubt!

yeah, you could also use python’s .split() method. By default, it cleans out all whitespace and splits.

oh, lol

What do I know? I’m a self-taught firefighter - lol.

I only question because you’re educated, and I’m a damn hacker haha

1 Like

educated

Yep, got me that 2 semesters of Womens Studies* at the local UC.

*womens studies is chasing skirts

I just have no life outside tech

I don’t want to drift off topic, but you know I’ve been here since 2015 right?

@sgtawesomesauce When some people were learning the right way — I was cutting people out cars, loading people people cut in half by I-Beams into choppers, delivering babies, and saving kittens out of trees while reading books on data structures and other stupid computer stuff right?

Forget it — pipes are useful. Good luck OP…

2 Likes