 Okay today we're going to be talking about configuration files. Let's say you're creating a bash script and the end user is going to have different information that they want to input and you don't want them to have to edit the script for every user that uses your script. So what we're going to do is we're going to create a config file and we're going to load that into our bash script. But not only are we going to create a config file that our bash script can load, we are going to make it so that if the config file doesn't exist the bash script will ask the user for the information and create the config file. Now in this example I'm going to be using a username and password as two of the variables. This is not a video about security and the way we're doing it, we're storing the username and password in plain text in the config file. Normally don't want to do that. This is not a video about security. This is just an example of different variables being stored in the config file and loaded and then asked for if they don't exist. And again these are basic examples that you can tweak. So what I have done is I set up a web server and I'm going to connect to it but we're going to create a config file that has the address of the server, the port number, a message you want to send, a username and a password. So let's go ahead and dive right in. We're going to look at our shell here and I have a script that exists already. It's called login.sh. If we look at it, it's just a wget command that passes a username, a password, and of course our URL with the IP address, port number and message to be sent. And if it succeeds it will print out the message from the server. If it fails it will echo out fail. Let's go ahead. Next to this it's Ori made xcubal or he has permissions to run. I'm going to say login.sh and we get success. If I go back in there and I change either the username or the password I could just remove a letter from there. If I run it now it's going to get a fail. Great. So right off the bat there's better ways to do this than just having this wget command. So let's go ahead and delete that and what we would normally want to do in the minimal case is to use variables. So let's go ahead. I'm going to quickly copy and paste here all that information into our script as variables and then we're going to use our wget command replacing the information from before with the variables listed above. So now we can run this and it should output the same thing success. If we go in here and we change one of the variables like my username to a username that doesn't exist and we try running it we will get fail. Let's go back in and change this to the proper things. So again it will run properly. Real quick let's go well I'll go into the file with vim and what I'm going to do is I'm going to select these rows here I'm going to delete them from there and what I'm going to do is I'm going to make a config file I just call it config okay and I will paste in those variables. Now if we try to run our login script obviously it's going to fail right because it doesn't have any of that information but if we go back into it well not into our config file but into our script itself and I just say source and I source that config file now oops sorry now it does work. So basically what the source command does I keep going into the wrong file we are done going into the config file okay source basically it's like a header file for c basically it takes what's in that file in this case our config file which is just a plain text file and basically it's inserting it right there in this script so it's just like typing all that stuff into this file. Now I'm using a config file that is directly in the same directory as my script normally you wouldn't want that normally it would go in your home directory most likely under the dot config and then a folder with the name of your project but just for simplicity of this tutorial I'm putting it in the same directory you just will adjust the name for namesake so we have a config file but what if that config file doesn't exist and of course you need to generate it right off the bat so what we would do before we source it is that we would check see if that file exists and of course I right here said config you're going to want the name and of course the full URL or full path name to our config file so let's use a variable variables are always good to use what I'm going to do is I'm just going to call it config file and again in this case it's in the same directory we're going to call it config so I'm going to take it and what I'm going to do is before I source it I'm going to check does that file exist so here we're saying does this file exist what file the file that we've listed up here to pipe symbols means if that fails does it exist if it fails then we're going to create the config file which we're going to create a function to do that here in a moment and of course down here when we do source it we're going to source that file that we have in the variable with me I hope so let's go on we are going to now create a function that if that file doesn't exist so far we will ask the user for that information and create it so we're going to say function again as I've mentioned in the past when creating functions in bash I like to write function but really you don't need to do that you can just give it the function name I just think it looks clearer especially someone who's not familiar with bash if you write function before that this is a function and I'm copying pasting stuff just so you don't have to see me type 100 different things we are going to use the read command right so we're going to read and we're going to prompt username then we're going to put that into a variable for user here I'm going to say read and I'm going to do dash s since this is a password it's secret it won't show as the user type since I'm doing that dash s here we're going to put that into a variable called password and then I'm echoing just because when you're using that dash s option it doesn't give you a new line character by default so we want that so that the next message doesn't show up on the same line as this but we go through for each of those variables that's great we've got them all now what do we want to do well we want to put them into our config file so again we have our variable created config underscore file and all I'm going to do is I'm going to echo in user equals dollar sign user password equals dollar sign password so forth and so on and we're going to pipe this greater than symbol so we're going to override an existing file if it does exist although we just checked if it does exist but we're going to check some other things later on to where it might exist but we still ask for this information so let's go ahead let's save this and let's go ahead and run it and it should off the bat because we haven't created that or actually we do have a config file it's just going to work it's going to say success but if I was to remove our config file and run our script now it's going to start asking me information and I can type in chris I can type in password for password I can type in the IP address and again with the password it didn't show the password because I gave it that dash s flag saying this is a secret and then port 8080 and then our message can be anything hello people boom and then I finished doing that it created the config file and it ran our command successfully right so again if I list out files here we have our script and we have our config file again in our current directory but in real life you want to put that somewhere in the home directory preferably the dot config file and in a subdirectory within there again I can cat out our config file here great what happens if when they're typing that information they decide to leave one of those blank well let's go a little bit further with our script so I will go into our script file again and then here after we source it what I'm going to do is I'm going to do this and there might be a cleaner way to do this but what I'm doing here is I am saying does the variable user exist if not when let's run the function create config I could just ask for the username but I'm going to just go through the whole creating the config file again and overriding it so now if I was to run this of course it's going to run properly because our config file is is proper if I was to remove our config file and run our script again like so it sees that uh it's that the config file doesn't exist and it's going to ask me for username and a password and let's just say I start leaving stuff blank and I'll type a message whatever it is going to ask me again because it saw that one of those variables was blank and it's saying I need all these variables so now I can type that I can type in password I can type in the server name and I can type in the port 8080 and our message which is whatever boom success I run the script again success and it'll be successful now you could add onto this that's pretty much all I've done so far but what we could do is instead of fail you could say okay if it fails then go back and create the config file but it might fail for other reasons maybe the server's down I don't know so that that's a little more in depth specific to your script but I hope you learned something I'm going to put a link to the description to this example code so you can look over it uh but yeah this is sourcing files and that's a great way to make configuration files so now anyone who downloads this script can run it and it'll ask for their information and store it for their user if you have it saved to their home directory and there could be multiple users on one system and they can all have their own config files again don't save passwords and plain text like this that's usually a bad idea um and this is not a video on security but there could be a lot of variables for individual users that you may want and you don't want to hard code that into your script that's just a bad habit of hard coding variables that are variable to different users into your script so where they have to edit the script you don't want end users having to edit the script just for simple things like that creating a config file is a great way to go and this is a simple way to do it again we went from a one line script to a multiple line script but it's much user friendly for for that end user who may not be a programmer so check it out links in the description please visit my website filmsbychrist.com that's chris of the k there there's uh links to all my videos you can search through them all there's also a support section a section where you can go to all my different scripts both on gitlab and pasteman and again you can support me through libre pay pay pal and i do have a patreon and i really appreciate my patreon supporters i only have a few though i have a lot of subscribers very few patrons um if you like my videos think about supporting i would appreciate that i don't think i've ever had more than 10 patrons uh at a time and i have almost or maybe over 60 000 subscribers at this time so if you get a lot on my videos uh you know a dollar or two maybe more a month would help and i would appreciate it but if not think about liking sharing subscribing commenting and as always i hope that you have a great day