Conversation started Oct 18, 2020 at 15:01.
Oct 18, 2020 15:01
meanwhile we better open a terminal
just shout out if you are stuck!
last week we made a directory for playing around in
Am I late ?
looks like you are right about on time :)
wait, wait just arrived
wonderful crowd
So last week I made a new directory in my home for playing around. If you were here, you might have done the same and still have the directory, and if so you can just cd into it
Otherwise, make sure you are in your home directory and then create a new directory and then cd into it, like
Oct 18, 2020 15:04
cd playroom ?
cd
mkdir playroom
cd playroom
@technastic_tc perfect, that's exactly what I'm going to do
so now my prompt is like zanna@peach:~/playroom$
none of those commands output anything
unless they fail...
but last week we ran some commands that produced output!
I'm here
yay
when you get some output from a command it appears in the terminal
(obviously)
But we can also send the output to a file using redirection. The redirection operator is this thing >
Since we are in this playroom directory where we can create files and clean up later without making a mess of our home or accidentally overwriting anything, we can safely redirect output from commands to create some files to play with. For example
echo $USER > name
We didn't see the output in the terminal because it was redirected elsewhere.
Now run ls here and see that it shows the file name
Oct 18, 2020 15:08
what if output is a stream?
@Zanna name
yes same
@nobody hmm we can try it out in a minute maybe...
If the target of redirection doesn't exist, it creates a file with the name (the path) of the target (even if there is no output to redirect - then we just get an empty file).
Now see what is in the file name by running cat name.
it's my username
@jokerdino same
Oct 18, 2020 15:11
cool
try this
can we use less name?
echo randomness > name
it got replaced!
with randomness
@technastic_tc yes, definitely, but it's a very short file, and then we have to hit q to escape, so unless you really don't want the contents of the file in the terminal, it's easier to cat it
@jokerdino :D
@Zanna ok
Oct 18, 2020 15:13
@jokerdino yes, the file got overwritten. We have to be careful with redirection!
To prevent redirection from overwriting files, you can make it append to the file instead by writing >> instead of >. This also works whether the file exists or not:
echo oregano >> tomato
can we add some cheese?
Do you have a file tomato" with the text "oregano" in it?
bash: syntax error near unexpected token `>'
@Zanna I get error
So echo command types text in a file?
Oct 18, 2020 15:15
@αғsнιη hmm did you run exactly that command
@Zanna there should not be any space between first & second > ?
@αғsнιη You probably had a space between the two >.
@user3140225 yum
i ate the cheese, nothing to add
terdon@tpad ~ $ echo oregano >> tomato
terdon@tpad ~ $ echo oregano > > tomato
bash: syntax error near unexpected token `>'
@technastic_tc echo just outputs its input. Output redirection sends command output to files. So echo didn't create the file, it was the shell
Oct 18, 2020 15:15
@terdon ahh, yes, I had one space between > >
The '>>' is a single token, it isn't two times '>', it is one time '>>'
@Zanna ok..
@jokerdino hmmmm
Let's add something else to see if we can keep the oregano
@user3140225 you can add whatever you like :)
Is the command tee -a also working
Oct 18, 2020 15:16
echo cheese >> tomato
@nobody yes, we are coming to that
sorry
@Zanna Thanks!
:D
We have just been using echo, but redirection is really useful for storing the output of any command.
In general the output you see in the terminal isn't saved anywhere - when you close the terminal it's just gone.
If you want to save the output of a command you need to store it in a file, which you can do with redirection. Try this:
uname -r > kernel
Now we created a file kernel. What is inside?
@Zanna 5.4.0-51-generic
@Zanna 4.4.0-18362-Microsoft
Oct 18, 2020 15:19
4.15.0-112-generic
@αғsнιη same as this
The uname command gives information about the version of the kernel. The -r option makes it output only the version string. This is the first Linux command I learned, so it's a friend of mine :)
5.4.0-51-generic
5.4.0-51-generic
5.4.0-51-generic
Oct 18, 2020 15:20
same for me
same not for me
Just checking if ppl has updated their kernel :-D
so we learned cool stuff about redirection and smartly redirection
Now sometimes you want to see the output of a command in the terminal as well as send it to a file. For that there is the tee command as mentioned by @nobody , which we can use with another useful thing, a pipe:
uname -a | tee kernel
not tee -a ?
Oct 18, 2020 15:22
this tee overwrites the stuff in my kernel
Linux XXXfilteredXXXX 4.4.0-18362-Microsoft #1049-Microsoft Thu Aug 14 12:01:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
yes, it overwrites stuff too, just like redirection
Linux HP 5.4.0-51-generic #56-Ubuntu SMP Mon Oct 5 14:28:49 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
but you also got the output in the terminal, right?
and yeah, it also thinks I am on PST
whereever that is
Oct 18, 2020 15:23
hahaha
@Zanna yes it did
Pacific :-)
@jokerdino I don't what that PST mean too
I got UTC
UTC for me too
Oct 18, 2020 15:23
UTC
yoU Take Care
@jokerdino maybe Please Stop Terminal output? :D
To make tee append instead of overwriting, use the -a (append) flag, as @nobody said
lol
@Zanna nice trick
Oct 18, 2020 15:25
I try to slow down
thanks @nobody
the punning part of my brain has been switched off for the duration of the lesson I think
I'm relying on y'all
This long vertical line character | is the pipe. What a great thing it is!
It takes the output of the command on the left of it and turns it into the input of the command on the right of it.
So, like a water pipe makes water flow on further, a shell pipe makes command output flow on further...
is that what people mean when they say pipe it down?
hahaha
btw, the time part did not change at all
maybe it be like that
Oct 18, 2020 15:27
@jokerdino I think that means mouth-a close
@jokerdino when what? I think I have missed something
5 mins ago, by αғsнιη
Linux XXXfilteredXXXX 4.4.0-18362-Microsoft #1049-Microsoft Thu Aug 14 12:01:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
@jokerdino The time refers to the time the kernel was originally compiled at. It won't change until you change your kernel.
Which is also why the time zone isn't necessarily your own.
That makes sense
I don't think I have ever bothered to read all that uname -a info before
hahahaha
Oct 18, 2020 15:30
Anyway, we can use piping in, like, a zillion other ways. For example, if the output of a command is long, we can pipe it into less to make it more readable, just like we can use less to read files.
I am worrying about people missing the session last week where we talked about less
I guess it's OK
it's ok
yep
I know less - wait that doesn't sound right.. :-D
Anyway last week we used the command printenv (we were having fun with that)
its output is pretty long, so you can try running printenv | less
I have mostly LS_COLORS
Oct 18, 2020 15:33
yep
@Zanna So much fun!!!!
how do we get out of less?
@user3140225 :D
you reboot the computer?
@jokerdino haha
Oct 18, 2020 15:34
@jokerdino hahahaha I thought that was how you get out of vim
@αғsнιη That's... something you really should get over. Pipes are one of the most basic, fundamental tools of the shell. The <() alternative you were given there is also often useful, but it isn't portable (not all shells support it) and it is far more complicated than a pipe for no real benefit (if you're just using it to avoid pipes).
pipes are the best
@αғsнιη embrace the pipe
The important thing is not to get confused between redirection and pipes. Output redirection* causes the output of a command to be written to a file. Pipes cause the output of a command to become the input to another command. That command might write a file (like `tee` does), but piping itself doesn't create or overwrite or append to files like redirection does.

* we have been using output redirection, but we can also redirect input from a file to a command.
2
Oct 18, 2020 15:36
lol unavoidable 5 minutes break XD
5 minute break it is
@user3140225 Or blue man group
@ArturMeinild Cool!
ok so we can pipe to get input from an output and use content of a file too?
what else is there??
@jokerdino Nothing else, 'cos everything is a file :-D
Oct 18, 2020 15:39
Everything is a file.
@ArturMeinild yeah, I am, like a pipe way |:|
@nobody mentioned redirecting streams
That's what I usually do at the beach, like, build some little sand fortifications and stuff
but I think that you are always redirecting a stream
Because the std output is a stream
Oct 18, 2020 15:42
like, what we were doing echo something > file redirects the standard output stream
if you want to discard warnings and errors from a command you can redirect the standard error stream to the black hole /dev/null, like command 2> /dev/null
@ArturMeinild yeah
sometimes I'll try to redirect the output of a command and get an emty file.
Anyway we already used most of the time!!
So just to elaborate on the above: > redirects stream 1 (std output) and 2> redirects stream 2 (std error)
@nobody like, maybe you redirected the wrong stream?
@ArturMeinild exactly
I wished I could remember.
Oct 18, 2020 15:45
@ArturMeinild what if I want both?
no worries... you can test it out later and report back :)
@nobody There's no other option. If the file is created (and simply '> file' with no command will create the file, or empty it if it exists), and is created empty, then you redirected something with no output.
@jokerdino Then you have to do some redirection between streams, and this gets a little more tricky
I'm sure I can find an askubuntu Q&A that covers this
@jokerdino redirect stderr to stdout and then redirect stdout to the file: command > outFile 2>&1
@Zanna How shall we use the remaining ~15 minutes?
Oct 18, 2020 15:46
@terdon Right on
I will hopefully come up with some fun piping stuff later
@Zanna oops
What I find to be nice about pipes is that one can chain as many commands as one needs:
printenv | nl | less
'&1' is "stream 1" (OK, it's actually the file descriptor for stream 1, but never mind). So 2>&1 will redirect stream 2 (stderr) to stream 1 (stdout)
@EliahKagan yess
@EliahKagan was going to talk about some expansion. Do you want to talk instead?
Oct 18, 2020 15:49
Here's some further reading on some of the things Zanna mentioned:
Pipes and redirection operators: https://askubuntu.com/q/447772/85695
More detail than you ever wanted on all control and redirection operators: https://unix.stackexchange.com/q/159513/22222
Redirecting stdout and stderr: https://askubuntu.com/q/625224/85695
@Zanna No.
I mean I don't want to talk instead.
we got another 10 minutes I think XD
Please proceed with expansions! :)
I was going to talk about brace expansion which has nothing much to do with any of the foregoing haha
try this
echo {a..z}
it prints a to z
Oct 18, 2020 15:51
O_O
also try
mkdir {01..04}
and ls to see the directories we created
01 02 03 04 kernel name
I have a tomato as well
2
me too hahaha
yum
@jokerdino Nice :-D
Oct 18, 2020 15:52
Brace expansion works for anything the shell can identify as a sequence. You can also use brace expansion with commas for things that Bash can't recognise as sequences:
@Zanna No, we only do Ubuntu here, no SuSe or anything else.
3
echo {one..three}
doesn't work
echo {one,two,three}
works
@terdon lol
that wasn't very useful... I mean it was more typing than echo one two three
can I use this to find a series of prime numbers?
er... probably yes
@terdon Yum is the package manager in Redhat, in case anyone missed that ;-)
Oct 18, 2020 15:54
@jokerdino you can do that for homework
@ArturMeinild what about dnf?
@jokerdino for i in {1..100}; do echo $i | awk '$1%2!=0'; done
woah what
You asked!
that is probably in part 24 of this lesson
Oct 18, 2020 15:55
:)
hahaha
@jokerdino :-D
That wasn't primes though, just odd.
Anyway the comma thing is actually useful... see how we can attach some common part to the brace
echo {fi,wi,ga}dget
Oct 18, 2020 15:56
@Zanna Cute example ;-)
@terdon Ohh, I found an easy way for that echo {1..100..2}
are these real words?
@αғsнιη O_O_O_O
That means "print the numbers between 1 and 100, in increments of 2"
@jokerdino I think so
@jokerdino They are
Oct 18, 2020 15:57
@αғsнιη cool, I didn't know that would happen :D
If you put a space between the brace and the common part, it doesn't work:
zanna@peach:~/playroom$ echo {soy,oat,almond} milk
soy oat almond milk
mmmhm
but you can escape/quote the space if you need one, as usual:
zanna@peach:~$ echo {soy,oat,almond}\ milk
soy milk oat milk almond milk
lottsa milk
haha yes, we got extra milk for our typing
One nifty use for this is to avoid typing the whole path of a file twice
try this
echo /etc/default/grub{,.old}
/etc/default/grub /etc/default/grub.old
Oct 18, 2020 16:00
yeah :)
We can use that with the cp command to make a backup, for example. We'll use cp another time!
nice!
@technastic_tc cool thanks
very nice!
Oct 18, 2020 16:02
@jokerdino welcome!
we ran out of time again
@ArturMeinild Nice!
@ArturMeinild Hey! That's beautilful! Thanks!
@ArturMeinild Please don't get into the habit of using CAPITALS for variable names though. That's a bad idea.
Oct 18, 2020 16:03
@EliahKagan You read the manual - that's cheating
@EliahKagan what is this document?
@EliahKagan Hardcore
@Zanna oh noo
@Zanna Personally I would be okay with running over. But should the date/time for part 3 be announced/determined first?
Oct 18, 2020 16:04
but does it mean we are getting part 3 next week??
@Zanna :-\
@terdon Agree - that part may be a little confusing, you're right
Can we do 30 minutes earlier next week, so we finish 30 minutes before @ArturMeinild 's session?
@Zanna it's fine for me
thank you
:D
Oct 18, 2020 16:05
@ArturMeinild People will tend to assume they are meant to be exported as environment variables. Also they are more likely to collide with same-named environment variables. Environment variables can be of any case, but they are most often named in shouty case (all caps).
@Zanna sounds good
@ArturMeinild It's only in a few of the examples, but it's a good idea to always use lower case variable names for your stuff since the global environment variables are in CAPS, so you can get unexpected results if there is a naming collision.
@EliahKagan Yep I know
So, @jokerdino, since you asked, here's a way to get primes (that actually prints primes):
@terdon I didn't make it, but I should gladly ask the author to correct it
Oct 18, 2020 16:06
$ for i in {1..30}; do echo $i | awk '{ prime=1; for(i=$1-1;i>1;i--){if ($1%i==0){ prime=0}}} prime'; done
1
2
3
5
7
11
13
17
19
23
29
@jokerdino The bash reference manual. It's longer, and also more friendly, than the manpage. You can also read it locally if you have the bash-doc package installed, by running info bash. The best thing to bookmark for the bash reference manual (at least what I find best) is gnu.org/software/bash/manual because that links to multiple formats.
@Zanna Should be fine for me too.
@terdon cool..
@Zanna Yes, this works for me.
great!
Oct 18, 2020 16:07
Of course, the {1..30} is only incidental, but still.
ok round of applause for @Zanna for another successful lesson!
Thank you @Zanna! I look forward to next week.
the next bit is related, but also pretty long, so I am OK with finishing here and trying to pick it up next time
 
Conversation ended Oct 18, 2020 at 16:08.