Transform Your Scripts With Bash Simple Curses

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when it comes to scripting or programming you really have to start thinking about how the user is going to interact with the program that you write and there's really three main interfaces as far as programs you have cli or command line interface so these are simple command line programs where you enter something at the command line and you get output given to you at the command line so those are like your standard gnu core utils almost all of those are command line interface programs all you do is interact at the command line there's no real user experience to talk of right and then the next step is the terminal user interface or sometimes people refer to it as the text user interface and this would be what we often just call tui programs 2e programs are they're they're kind of like graphical programs but they really they have to run inside a terminal emulator they're designed to run inside a terminal they use a library called in curses typically and examples of these n-curses programs would be something like h-type or vim these are tui programs they have to run inside a terminal emulator they can't be run outside of a terminal and then of course we have our traditional gui applications or graphical user interface applications and these are your traditional graphical windows with menus and buttons and widgets and these are typically written using the gtk toolkit or the cute tool kit at least on linux and like most people i started with some basic scripting most of my basic scripting was strictly cli programs and then i wanted to get more into graphical programs so you guys know here lately i've been experimenting a little bit with writing my own gtk applications but then i i skipped over that middle ground what about tui interfaces what about creating something using in curses well in curses i may eventually explore it but it's written in c i really don't know the c programming language it would have a little bit of a learning curve that again i may explore it at some point but there is an easier option to get into kind of like a beginner in curses kind of option it's not really in curses at all this is called bash simple curses i found this on github a few days ago and i've been playing around a little bit with it and all it is is some simple bash functions that it will draw windows like boxed windows inside the terminal and display your information inside like these display boxes and it kind of makes it almost like a in curses program even though technically it's not an incursive program this is running strictly bash bash simple curses is written under a free license it is licensed under the bsd 3 license there is documentation pretty easy documentation you have a basic tutorial here there's not a whole lot you can do with bash simple curses it's not very deep anybody could use this thing which is one of the reasons why i'm making today's video i think a lot of people would find this useful and then of course they have several example shell scripts that they've written using the bash simple curses uh functionality so what i've done is i have cloned this repository from github and let me open a terminal and i'm going to zoom in just a little bit here i'm not going to zoom in too much here because when i run these programs because they're boxed programs if i zoom in it's going to distort them a little bit so i don't want to zoom way in like i normally do on these videos but i'm going to cd into that examples directory where we have all of the example scripts plus there's three or four of my own example scripts where i played a little bit with bash simple curses and you want to make sure that all of these scripts are executable so you can play with them now i have already made these executable but if they weren't executable when you clone the repository then you need to make them executable you need to use the ch mod command so change mod space plus x x stands for executable is adding basically the x here those three x's means that the owner the owner and the group and that anyone else all have execute permissions for this so anybody can run this script and then of course you need to give it the path to the script that you want to make executable in this case i'm going to make the bash bar script executable it was already executable i really didn't need to do that but anyway then to run a script you need to do dot slash name of script and this is the bash bar example script i'm getting a little bit of the error down here it says d message yada yada yada operation not permitted there's a part of the script probably that that command probably needs sudo permissions i'm not running this as sudo so that's why that error is happening it's probably trying to print something down here as part of the network interfaces and you see the error message flashes every second it looks like they've got this script timed to rerun every second or two but it's a very basic script it's just pulling out some very basic information for example the host name of this machine i believe is arco linux or they may have actually gotten the linux distribution name from the lsb underscore release command part of the new core utils and they're just running the date command right then they're getting our uptime our users they're getting memory total memory free memory then they're getting some of the top running processes on my system right now what's taking up the most memory and cpu and it's going to print some kernel messages there's nothing to print and then of course our network interface this is my ethernet right here enps 0. of course it would be very easy to write a simple bash script that displays all of this information but to display it all in a really neat boxed sort of way where you have boxes and line separators and you notice that the heading of the boxes you can specify colors that's really really neat let me show you the actual the script itself if i actually open this with them so let's open the bash bar script and actually see what's going on and then let me zoom in a little bit now so you guys can read some of the script so to use bash simple curses the first thing you want to do is you want to import this shell script here right you're going to have to source this simple curses shell script which contains some of the functions that you're going to use for bash simple curses if i actually go back to the github here i go down into the documentation i believe it mentions yeah typically what you want to do is you want to do source and then the location of this file and you could place this file anywhere here in this example they place it in user local lib which is typically where libraries are placed on a gnu slash linux system but you could place it anywhere but you just need to source the location to that shell script in my case here since it's already in this cloned git repository i'm just doing a period deer name dollar sign zero yada yada yada basically find this file here in this project that i'm currently working in but if this was something that i wanted to package up and actually make it an installable program i probably would place this in something like user local lib and then actually writing your script is very easy all you need to do is make sure you have a main function and inside the main function you're going to use these other functions that were created from the simple cursive shell script so you have window window takes a couple of arguments it takes the text basically the header of the window in this case they were doing hostname remember archo linux and the bash bar script the very top was arco linux that is the hostname of my computer and then red is the color of that let me open a new terminal and actually just rerun the bash bar script while we're looking at the text so arco linux and the color is red so back to the script we have window hostname red and window means drawing box and then at the top have the host name in red and then the window function also adds a line a little separator up under the header you know the title the host name so that is how we've got the box the header and this first line but then we have this next line here this is actually a separator and all you need to do is use this command here add sep and that just adds a line and then other than that you just add your information append this particular function it just prints out some text or the output of a command for example inside the double quotes is where you would put text but if inside the double quotes you do the backtext and then run the date command right you actually just print out the date or append here and append you could keep appending things and they will appear centered inside the box one line after another but sometimes you don't want that sometimes you want to run this command here append underscore tabbed and this is how you get tabbed information like up since and then eight days users one load average and then the load average right there they're two columns instead of having everything kind of centered in the middle of the screen now you can actually have columns and down here we have three different columns and that is done with append tabbed now by default append tab uses a colon to separate your columns of information so you do a pen tab and then inside double quotes the text that you want to appear but in this case again you can also use shell commands the output from standard gnu core utils like uptime cut said etc so first of all it's going to print out this text users and then colon colon is the field separator it is actually what breaks the columns so if i go back to the script running you have users and then colon colon does not get printed that is just letting us know that's a break and then the rest of the information goes in the second column how does it know there's only two columns of information well that's the next part that you give you give 2 as the number of columns for this append tabbed command you could also give it one other argument if you didn't want to use colon as a separator then you could give it one last argument to let it know use this other item as a field separator for example you notice append tab up here actually uses a pipe symbol as the column separator because now you have a pen tab and then the text that should appear and then you have two for there's two columns and then tacked on here at the end you have a new field separator instead of using the colon they're using the pipe symbol for some reason in this command and then you have this end wind so this is the end of the window that's signifying that that's the end of this window block here this very first one and then you create a new window for memory usage and you see window memory usage red again is the color and then you do some information and then end wind and then another window yada yada yada and you end up with you know these four or five different windows or boxes here displayed displaying the various information with the formatting the right number of columns the right colors and it's just it's it's a really neat way to do this now for those of you not familiar with bash scripting you have this main function here but just because you define this main function doesn't actually make it run after you define your main function with all the windows yada yada at the very end you actually want to run the main loop you do main underscore loop and then you need to give it a time how often does it need to run and it looks like this bash bar script is running every half second so that is why you're seeing this blinking message here pretty regularly it's because the script is actually constantly updating itself of course depending on the nature of the script and the kind of information you're displaying you may not need it to update so frequently of course the more frequent that the shell script has to run itself of course the more taxing that could be on the system especially if you're crunching really big numbers or you're doing some heavy operations so this was the bash bar example script they have several other example scripts here for example here is bash bar two let's see if this differs no they're just doing the boxes in a little different order instead of having five boxes stacked one on top of each other they're doing columns here with the boxes and if you want to see some of the code for it let's quit out of this and open bash bar to shell script here and you see how they're accomplishing this is now there's one other argument that the window command can take window takes the text it's going to display host name again the color red again but this time they added this argument 50 meaning only take up 50 of course of the width of the terminal the reason is because the next window is going to take up the other 50 right so that is how they're getting these 50 percent kinds of windows here let me quit out of that and then do an ls some of the other really interesting ones they have one here called view meter so this would be like a volume meter so you can do kind of like these uh volume bars as well there's an example of that and i did one actually i did a few here but i played around a little bit here is one i did just an example of some system information that i think i would find useful this is dtos system information and i talk about the packages that are installed on my system the package is installed from pacman flat packs now updates that are available for me my uptime yada yada yada software versions of course the most important software is the linux kernel but then i also printed out the various current versions of other important pieces of software for dtos yada yada this this is really i could play with this the whole day this is pretty neat so let me show you some other ones as well there was one here called wind test that i believe was part of the example scripts of theirs it looks like it's a little broken it's trying to run some kind of command that just doesn't quite work right but you can see they had a calendar they had a tree file view it looks like they've got some text boxes here you could even make it almost like a note taking application where it displays like a latest notes that's you know written to a certain note file that you always write to so this is just a little introduction to bash simple curses this really neat program that i found and i'm just really impressed with it because even for such a simple thing it solves a real world problem because a lot of people they don't like command line interface programs they want something with a much more ui kind of experience right something that you can actually draw boxes and place things exactly where you want on the screen more kind of a gui style program and to do that in a terminal it's complicated if you have to start getting into in curses and learning the c programming language and stuff like that where with bash simple curses anybody can be up and running with this within a couple of minutes and i think it's fantastic now before i go i need to thank a few special people i need to thank the producers of this episode dustin gabe james matt maxum michael mitchell paul west want your ball and homie allen armor dragon chuck minder angry day yokai dylan marstrom eryan alexander peace arch and fedor polytechnic realities for less red prophets stephen tools devler and willie these guys they're my highest here patrons over on patreon without these guys this episode about bash simple curses would not have been possible the show is also brought to you by each and every one of these fine ladies and gentlemen all these names you're seeing on the screen right now these are all my supporters over on patreon because i don't have any corporate sponsors i'm sponsored by you guys the community if you like my work and want to see more videos about linux free and open source software bash scripting subscribe to distro tube over on patreon alright guys peace and if you create something with bash simple cursor share it in the comments below
Info
Channel: DistroTube
Views: 106,620
Rating: undefined out of 5
Keywords: bash scripting, shell scripting, bash scripting tutorial, bash script, shell scripting tutorial, linux tutorial, linux, gnu linux, gnu bash, bash tutorial, bash scripting tutorial for beginners, gnu coreutils, programming for beginners, simple shell scripting, ncurses, bash simple curses, bashsimplecurses, bash tutorial interactive, ncurses windows, user interface, terminal user interface, tui programs, terminal apps, terminal scripts, distrotube
Id: 4G_cthFZeJ8
Channel Id: undefined
Length: 16min 14sec (974 seconds)
Published: Fri Jun 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.