Lecture2 - Shell Tools and Scripting
Lecture2 - Shell Tools and Scripting
1:42 It's because the foo command is not working, like foo is
non-existent.
1:46 And here what is actually happening, we're not
assigning foo to bar,
1:47 what is happening is we're calling the foo program
1:52 with the first argument "=" and the second argument
"bar".
1:57 And in general, whenever you are having some issues,
like some files with spaces
2:03 you will need to be careful about that.
2:06 You need to be careful about quoting strings.
2:10 So, going into that, how you do strings in bash. There
are two ways that you can define a string:
2:16 You can define strings using double quotes and you can
define strings using single,
2:24 sorry,
2:26 using single quotes.
2:29 However, for literal strings they are equivalent,
2:32 but for the rest they are not equivalent.
2:35 So, for example, if we do value is $foo,
2:43 the $foo has been expanded like a string, substituted to
the
2:48 value of the foo variable in the shell.
2:50 Whereas if we do this with a simple quote, we are just
getting the $foo as it is
2:58 and single quotes won't be replacing. Again,
3:02 it's really easy to write a script, assume that this is kind
of like Python, that you might be
3:07 more familiar with, and not realize all that.
3:10 And this is the way you will assign variables.
3:14 Then bash also has control flow techniques that we'll
see later,
3:17 like for loops, while loops, and one main thing is you can
define functions.
3:24 We can access a function I have defined here.
3:28 Here we have the MCD function, that has been defined,
and the thing is
3:34 so far, we have just kind of seen how to execute several
commands by piping
3:38 into them, kind of saw that briefly yesterday.
3:40 But a lot of times you want to do first one thing and then
another thing.
3:44 And that's kind of like the
3:47 sequential execution that we get here.
about:blank 2/24
2023/4/21 10:42 For print
about:blank 3/24
2023/4/21 10:42 For print
about:blank 4/24
2023/4/21 10:42 For print
about:blank 6/24
2023/4/21 10:42 For print
about:blank 7/24
2023/4/21 10:42 For print
about:blank 8/24
2023/4/21 10:42 For print
about:blank 11/24
2023/4/21 10:42 For print
about:blank 12/24
2023/4/21 10:42 For print
about:blank 14/24
2023/4/21 10:42 For print
28:51 but will also work with some tools that you install from
the internet
28:55 for example, if the person that did the
28:58 installation made it so that the man
29:01 package were also installed. So for example
29:03 a tool that we're gonna cover in a bit
29:06 which is called "ripgrep" and is called with RG, this didn't
29:12 come with my system but it has installed
29:14 its own man page and I have it here and
29:17 I can access it. For some commands the
29:21 man page is useful but sometimes it can be
29:25 tricky to decipher because it's more
29:28 kind of a documentation and a
29:30 description of all the things the tool
29:32 can do. Sometimes it will have
29:35 examples but sometimes not, and sometimes
29:37 the tool can do a lot of things so a
29:41 couple of good tools that I use commonly
29:45 are "convert" or "ffmpeg", which deal with images and
video respectively and
29:50 the man pages are like enormous. So there's
29:52 one neat tool called "tldr" that
29:54 you can install and you will have like
29:58 some nice kind of explanatory examples
30:02 of how you want to use this command. And you
30:05 can always Google for this, but I find
30:07 myself saving going into the
30:10 browser, looking about some examples and
30:12 coming back, whereas "tldr" are
30:14 community contributed and
30:16 they're fairly useful. Then,
30:19 the one for "ffmpeg" has a lot of
30:23 useful examples that are more nicely
30:24 formatted (if you don't have a huge
30:26 font size for recording). Or even
30:30 simple commands like "tar", that have a lot
30:33 of options that you are combining. So for
30:35 example, here you can be combining 2, 3...
30:37 different flags and it can not be
30:41 obvious, when you want to combine
30:43 different ones. That's how you
about:blank 15/24
2023/4/21 10:42 For print
about:blank 17/24
2023/4/21 10:42 For print
about:blank 19/24
2023/4/21 10:42 For print
about:blank 20/24
2023/4/21 10:42 For print
about:blank 21/24
2023/4/21 10:42 For print
43:36 look for all the times you try to convert a favicon in your
history.
43:42 And it's also like fuzzy matching, whereas like by default
in grep
43:46 or these things you have to write a regex or some
43:49 expression that will match within here.
43:52 Here I'm just typing "convert" and "favicon" and
43:54 it's just trying to do the best scan,
43:57 doing the match in the lines it has.
44:01 Lastly, a tool that probably you have already seen, that
I've been using
44:06 for not retyping these extremely long
44:08 commands is this "history substring search", where
44:13 as I type in my shell,
44:15 and both F fail to mention but both face
44:19 which I think was originally introduced, this concept, and
then
44:22 zsh has a really nice implementation)
44:25 what it'll let you do is
44:26 as you type the command, it will dynamically search
back in your
44:31 history to the same command that has a common prefix,
44:34 and then, if you...
44:39 it will change as the match list stops
44:42 working and then as you do the
44:44 right arrow you can select that command and then re-
execute it.
45:05 We've seen a bunch of stuff... I think I have
45:09 a few minutes left so I'm going to cover a couple of tools
to do
45:16 really quick directory listing and directory navigation.
45:20 So you can always use the "-R" to recursively list some
directory structure,
45:30 but that can be suboptimal, I cannot really make sense
of this easily.
45:36 There's tool called "tree" that will be the much more
friendly form of
45:44 printing all the stuff, it will also color code based on...
45:47 here for example "foo" is blue because it's a directory
and
45:50 this is red because it has execute permissions.
45:55 But we can go even further than that. There's really nice
tools
about:blank 22/24
2023/4/21 10:42 For print
46:00 like a recent one called "broot" that will do the same
thing but here
46:04 for example instead of doing this thing of listing
46:07 every single file, for example in bar
46:09 we have these "a" through "j" files,
46:11 it will say "oh there are more, unlisted here".
46:15 I can actually start typing and it will again
46:18 again facily match to the files that are there
46:21 and I can quickly select them and navigate through
them.
46:24 So, again, it's good to know that
46:28 these things exist so you don't lose a large amount of
time
46:34 going for these files.
46:37 There are also, I think I have it installed
46:40 also something more similar to what you would expect
your OS to have,
46:44 like Nautilus or one of the Mac finders that have like an
46:49 interactive input where you can just use your navigation
arrows and quickly explore.
46:59 It might be overkill but you'll be surprised how quickly
you can
47:03 make sense of some directory structure by just
navigating through it.
47:07 And pretty much all of these tools will let you edit, copy
files...
47:12 if you just look for the options for them.
47:17 The last addendum is kind of going places.
47:20 We have "cd", and "cd" is nice, it will get you
47:26 to a lot of places. But it's pretty handy if
47:30 you can like quickly go places,
47:33 either you have been to recently or that
47:36 you go frequently. And you can do this in
47:40 many ways there's probably... you can start
47:42 thinking, oh I can make bookmarks, I can
47:44 make... I can make aliases in the shell,
47:46 that we will cover at some point,
47:49 symlinks... But at this point,
47:53 programmers have like built all these
47:54 tools, so programmers have already figured
47:56 out a really nice way of doing this.
47:59 One way of doing this is using what is
about:blank 23/24
2023/4/21 10:42 For print
about:blank 24/24