 All right, good afternoon. I was gonna write a very brief shell script and I figure I might take you along for the what ride This is a very it's gonna be a very simple script It's really just gonna be a case statement with a couple more things added on here's the problem that I want to solve In case you don't know there are two different ways of doing sound volume and everything on Linux Okay, so there's also also is what's just you know installed by default on all the Linux distributions It's a standard Linux sound system. I think it stands for something like that So these are the commands for changing volume for muting for toggling the mute of the master You know output or whatever it is Sync, I guess they call them an also I always forget But this is for changing volume muting your volume on also, but there are a lot of Linux distributions that use also Pulse audio pulse audio is some stuff that you install on top of also some people call it bloat Some people call it very useful, but these are the commands for doing the same thing in pulse audio and these are specifically binds that Increase the volume by five percent decrease it by five percent increase it by 15 decrease it by 15 The reason I have these particular numbers is those are things that are actually key binds on my computer So if I press, you know Hold down the mod key and plus press plus it increases my volume by five points or decreases it If I press minus stuff like that Anyway, I just want to automate that I want to have a wrapper that instead of having to know these two commands Or these two types of commands I just want to have a script that can choose which one is the correct one to run and this is just so people who Are using my dot files don't have to you know reset all of my bindings if they want to use pulse audio Okay, so here's how I'm gonna start I'm gonna start by naming the script LMC now if you've been around a while You know I used to have a script that did something similar that was named LMC And I'm just gonna say it stands for Luke's music control Well, I guess it's not necessarily music, but you know whatever so I'm gonna say It's a shell script in our shebang here And I'm also gonna go ahead and make this executable by running change mod Plus X on it. So now it can be executed. So to be clear. I'm also in local bin which happens to be in my path directory. I Expect you know what that is if you don't look it up It's just the folders that you can run a script directly from so I don't have to I can just type LMC as opposed to its exact location Okay, so here's how I want it to work. I want to be able to write run this I want to be able to run let's say LMC toggle and it's gonna pick either the also or the pulse audio version to run but it's gonna run that command or LMC up increase the volume So let's start at a basic level level. I'm just gonna put in the also commands at the start So how I want to do that if we're reading the first argument the first thing you give that script up or toggle You're gonna want to do something like this. Okay, so I'm gonna say just as an example Case argument one in we'll say toggle. Okay, and I'll explain what that is if you're lost already Master toggle notice. I'm just copying that command over there. Okay So this is how a case statement looks if you don't know anything about shell scripting Basically, it says okay. Take dollar sign one dollar sign one means again the argument you give it Okay, so in this case if I run this LMC up It up is gonna be dollar sign one or if I run it as down The string down is going to be dollar sign one But in this case Check dollar sign one if it's equal to toggle do this. Okay, so if it's equal to toggle toggle the the volume to be either Mute or unmute. Okay, so I also want a command for just muting it So I'm gonna change this to mute in the case that I give it the mute command run all this a mixer S set master mute. Okay, if I give it the up command Okay, I want it to do this I want it to do a mixer set master and then we'll say five percent increase that's just the idiosyncratic syntax of Ulsa and the same thing for down. Okay, down is nearly the same thing It's set for this is gonna be a minus, right? we're just looking at the commands over here and Let's just throw in these 50. We're gonna change this in just a second, but just as an example We'll go ahead and put in these 15% commands, but we have to call them something different. Let's call it big down and big and oops, excuse me big down and big I want one extra back. Okay, so just to be clear. What did we just do? We made a little script here and how it's good what it's what it is going to do is it's gonna if I run LMC down it's gonna run that command that decreases our audio So now it is 68% if we run it again, it's gonna decrease it by five percent to 62 I know that's not exactly five percent, but that's not how a mixer actually works It's it sometimes it'll be a little bit different, but it's working Okay, so we can go down down five percent or we can go up five percent or we can toggle the mute Okay, so you might be wondering what is all this output? It's giving me this is just This is just the typical output of a mixer if we want to suppress all of that I can just say dead, you know put the entire state case statement to dev null meaning instead of Showing me what it's doing. It's just going to it should automatically Excuse me suppress all of this output. Okay now to be clear up here in my a Status bar, you'll see I actually do have a volume thingy up here that shows what my volume is But it has to be manually updated by running this command that's at the bottom So I'm actually gonna put this in my script that is let me the reason I have it like this Some status bars that are a little more memory intensive. They're always checking to see like what your volume is mine You have to manually tell it. Oh, I've changed this so update my volume and this is the command that does it So now if I run LMC up now It's going to update my volume. Actually, it's muted right now. Let me Unmute it. So we'll say toggle. Okay, so now it has toggled this and you'll see the volume So we can change it, you know to up now. Let's go on up stuff like stuff like that's very small up there But you can see that it's working. Okay, so we've integrated or also commands in here But how are we gonna put in pulse audio? All right now? There are different things you can do here. Actually before I even do that. Let's make something easier I think it's ugly for us to have big up and big down a separate commands because really they're basically the same as up And down right we can integrate these into one thing. So let's say I want to do this Let's say I want to say Let's say I want this LMC up Five to mean go up by five percent or up ten to mean go up by ten percent Well, we can pretty easily change this. So here's what I'm gonna do And by the way, this is gonna make these two commands Obsolete because we can just give them 15 in this context. So let's say I'm gonna replace five with The variable numb. Okay, that's a variable. I haven't defined yet, but let's define it Let's say numb is gonna be equal to this Now I'm gonna do something a little special here I'll explain what this is in a second. Okay, so what this is saying is the following now As I said dollar sign one that of course is going to be your first argument dollar sign to This is what this is is gonna be your second argument and what this is saying is Define numb as the following define it as dollar sign to your second argument But if you don't explicitly say what that is, let's say if we just run LMC up Assign the variable or it's assigned the value five to that. Okay. What does that mean? Okay? That means if I run LMC up it is going to increase my volume by five percent. You can check it going up in my You know my thing up there or if I say down it's gonna go down five percent Whereas if I say down ten, it's gonna go down by ten percent or if I say up Thirty it's gonna go up by thirty percent. All right. That's what that is and this is a very terse way of saying that that is Use argument two if argument two doesn't exist just assume we're gonna use five Okay, that's all this line is saying and it actually says a lot for one little tiny line Okay, so now we've got we've got a good bit of stuff here now But we don't have the main thing we were actually making the script for and that is use either also or pulse mixer Okay, we only have the also command. So here's what I'm gonna do. Here's what I'm gonna change about the script I'm actually gonna take this stuff and copy it out and I'm gonna put it over here I'm actually gonna change it a little bit. I'm gonna redefine I'm gonna make these all defining functions Okay, what I mean by that is we're gonna do this Well, let's move these over first off and I'm gonna change this I'm going to use some Vim magic here to make these all Functions, okay, so we're going to yeah. All right. So now these are all we what we've done is we've Defined these as functions now we have a function called toggle and what that does is runs this command So we can actually go down here and we can replace all this stuff with just us calling the functions Now you might be like, okay, why why are you doing this that doesn't make any sense? You're just you're just moving things out of the case statement You're just defining functions. Wouldn't it be a waste to do that? No, the the good thing is now is we can define that we can have a check to check if we are running either Also or pulse audio and if we're running also we can define the functions like this if we're running pulse audio We can define the functions in a different way. Okay, so let's just say we'll start We'll just do it this way. We'll say this We'll say pulse equal to true All right, and we'll say if you're a pulse user all you have to do to make your my dot files Compatible with the script as you come in here and you unset you Uncomment this line so pulse equals true and here's what we're gonna say if pulse equals true If the variable pulse equals true then We're gonna well, we'll do some stuff in a second, but we're gonna define the functions with their pulse definitions Which we'll do in a second. Otherwise Define the functions like this. These are their alf alse definitions. Okay. Oops. I don't mean else I mean close your if statement. So to be clear Here's what we want to do. We want to define the Statements how we want them to look in pulse audio in this area Whereas our alse statements are already here. We've already finished for that. So let's actually so now what we have to do So I'm gonna copy this stuff And I'm gonna put it in here and I'm gonna replace all the also commands with pulse audio commands these Pulse mixer commands, so I'm not actually familiar with but whatever I'll just copy them blindly because I haven't used pulse audio in a long time So toggle mute Okay, and we'll replace that as well except for its only mute and then here we have what we have pulse mixer change Change charge Volume now if I edited videos, this would be a nice time to edit out But you know, I don't do that. We don't do that on this channel. We're real here Okay, so I think that's it. All right, so we've redefined all these functions. I'll check them later And here's how it works now This script can handle if you're running pulse audio or if you're running also all you do in this case is you come in here and you check If you want to switch the pulse if you've installed pulse audio you come here and you Uncomment this line and you will be running pulse audio commands. So now just to double check So we can increase our volume. We can increase it by 20. We can decrease it by 50 or something like that We can mute it. Okay, we can do all that kind of stuff toggle the mute and Now if we're pulse audio users we can come in here and automatically You know change everything now, of course There are ways of checking to see if you're running pulse audio right now just to be clear I I know because if I don't know that people will say, oh, well, you could have done this here For example is one example. We could say this instead of saying Make up this variable that you just define As be, you know, you if you have pulse define this variable You could do something like this. You could say this. Let's say p-grap x pulse audio Okay, so p-grap it checks to see if a process is running x means it has to be exactly It has to look exactly like this. Now if I run this, it's not gonna have output But if I started running pulse audio, it would return for it would return true It would return the value the process number of pulse audio Okay, so what what you could do is you could replace that instead of saying if blah blah blah you could say if p-grap x pulse audio Okay, and that every time you ran the script it would check to see if pulse audio is running If it is running it would run a pulse audio command if it's not running it would run something else Now you might they might say well, why don't you just do this luke? um It's a little bloated. I'll just say uh, if I ran p-grap. Let's run it actually p-grap x pulse audio Okay, p-grap just takes a little bit of time It takes probably even a little more time now that i'm recording Video, but let's just try it out. You see it takes like, you know a third of a second Okay, and if i'm running an audio command to change my volume or muted or something like that I don't really want that kind of latency. So i'm not going to put it in there But if you're using a task where a you know, a third of a second doesn't really matter You might want to consider that or you might know some smarter way to test for if pulse audio is running or not And you can go ahead and tell me Maybe there's some environmental variable that pulse audio sets or something like that There might be some kind of file you could check but either way this will do pretty much simple enough So anyway, um, this video has been a little disjointed. We've talked about different things But I really just wanted to put out the process like what my mindset is when i'm i'm going through writing a shell script Very simple You get things done pretty quickly and it actually has it's very useful to me because i'll probably be using this My dot files and it will probably avoid a lot of Headaches on some people's, you know, just so they don't have to worry. You don't have to recompile dwm if they want to use Pulse audio. All right, that's it. I'll see you guys next time