Transcript from the "Arguments" Lesson
>> Let's talk about arguments. So we're gonna go back into our gen_files again. Let's say we wanna put the destination to be something that the user can input, right? Cuz right now it's just always generating this temp file for us. And it'd be better if the user could tell, I want those temporary files to go to this place.
[00:00:19] So the way that we do that instead of making destination equal to temp like this, what I'm gonna do here is I'm gonna make this equal to $1. And that's going to be the first argument provided to it, right? Now keep in mind there actually is a $0 but the $0 will be gen_files.
[00:00:37] It's gonna be the command that was run. So if you wanna figure out how did the user invoke this, which in our case, it would be obvious it would be gen files, but you could do that with $0 to figure that out. So $1 will be technically the second thing, but it's the first argument being provided.
[00:00:52] As you might imagine, $2 would be the next one, $3, $4, and so on and so forth, right? So $1 is gonna be equal to that. And now anywhere that we refer to destination, cuz we've basically renamed $1 to be named destination. I totally could have renamed this to be just $!, and $1 here, but I like giving names to my arguments so I can figure out what that actually is.
[00:01:18] It makes my bash script a little bit more readable. So I tend to do that, but no reason you have to. So now I can do gen_files, and I can give it a yet-different-folder. And now let's see where am I? I'm in primary right, so if I look in here there's a new folder called yet-different-folder, and if I go into yet-different-folder, you can see I got all my temporary files, right?
[00:01:44] So that's how you give arguments to your bash script. And now what I wanna do is I wanna go back one, and I want to vi this again. Let's talk about how to receive user input interactively. So the next thing I wanna do is I'm going to delete this line here called file prefix.
[00:02:06] And we're gonna use a command called read. So I'm gonna say read -p, -p as being the prompt that was like what are you asking the user for? I'm gonna say enter a file prefix. So we can ask these are the question please enter something, and we're gonna store in a variable called file prefix.
[00:02:26] Okay, so if I right and quit that, this is the line that we added here. Everything else will still work the way we anticipated, cuz we call it file prefix and we're referring to file prefix down there. So now I'm gonna say right quit, and I'm gonna run gen_files, and I'm gonna put that in a still-different-folder.
[00:02:45] So a new folder called still-different, and it's gonna ask you please enter a file prefix. I'm going to call this frontendmasters. Okay, and if I hit Enter, it'll say done. And if I go into still-different-folder, and we look all the files are called frontendmasters1, frontendmasters2, frontendmasters3. So that's how you grab user input is using that read Command.
[00:03:10] So basically you're saying like, wait for the user to enter something. As soon as they hit Enter, then continue, and this will be whatever the user gave.