Add Dialogs And Menus To Shell Scripts With Whiptail

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I wonder to know whether it's a good decision to use such interfaces for terminal interactive programs. To clarify: open such TUI just when smth like --interactive option passed, otherwise behave like a usual CLI tool. 🤔

👍︎︎ 3 👤︎︎ u/EmilySeville7cfg 📅︎︎ Mar 15 2023 🗫︎ replies

I have had some issue with your recent content being more opinion or DTs maintenance... but this Sir is the content that I like. Will I be using Whiptail... probably not, but I learned something of value to me. It is your channel and you do you...but I missed this type of video. Thank you Sir!

👍︎︎ 1 👤︎︎ u/TheGassyNinja 📅︎︎ Mar 15 2023 🗫︎ replies
Captions
in the last couple of days I've spent several hours playing around with some scripting and in particular I've been scripting using a program called whiptail whiptail is essentially a smaller more minimal kind of alternative to the old dialogue program you guys probably know dialogue which is a in curses a text-based kind of widget program that allows you to have kind of you know dialogue boxes and menu systems and things like that for example if you were doing like a Linux installation at a TTY a lot of those old school in-curses installers used programs such as dialogue or in my case a whiptail whiptail doesn't use the in-curses library though whiptail actually uses the Newt programming Library which is very similar if I can actually show you you know some of Newt here I don't know if this is a whiptail box it probably is but you can see it looks pretty much the same as dialogue you couldn't tell the difference between dialogue and whiptail the one one thing with the using whiptail versus dialogue dialogue is a much more complicated program there's a lot more options there's a lot more functionality built into it which could be good for some users for me I found it a little complicated I didn't like working with it I used dialogue a little bit in my dtos installation script but I've been looking to swap out dialogue for something else which is why I've been trying out whiptail and I found whiptail to be pretty easy to use there's a couple of peculiarities with whiptail if I go to this page here bash shell scripting with whiptail I I'm pretty much based everything I did off of this page this page is great it gives you examples of various whiptail boxes such that's an example dialog box yes or no okay cancel this is just a message that all it has is the one button okay here is you entering a password here is a list of items to choose from there is a checklist and there is a radio list so you know you can pretty much do anything you want to do with whiptail one Oddity to whiptail if I switch over to emacs here this is the script I've been working on really as an example but it may eventually this code may make its way into dtos the dtos installation script one Oddity with whiptail is you have no way if I scroll down to the whiptail code this is basically creating the whip till box the window that would appear one of the strange things about whiptail compared to dialogue dialogue you could choose coloring and bold text and italics and all of that stuff you could actually place that inside for example the dash dash title flag you know and I'd have some string that would be the title of the window but you know I could do backslash capital Z and then some color I think one through seven or zero through seven is the first eight colors of your 16 base color scheme your X resources color scheme or whatever color scheme your terminal is using so I could do slash Z1 to color it with the the one color right or the two color or whatever you know or I could do uh backslash Z it's lowercase B I think for bold or whatever you know you can't do any of that in whiptail whip tail doesn't take that stuff it has no functionality for you to specify colors in the windows themselves what you actually have to do and I actually found this over on ask Ubuntu I made a note where I found the thread on ask Ubuntu how do you color how do you specify specific colors with these whip tail boxes and you do it through exports you have to export Newt again Newt is the widget library right there basically in curses type Library Newt underscore colors and then you export these various variables for root Windows Shadow border title there's probably twice as many variables as this available three times as many these were just some of the ones that I've found that I wanted to specify here but before we dive too deeply into the code let me just show you the script in action so I'm going to CD into the directory where I have the script now I'm just going to execute it and it's pretty self-explanatory what I'm doing here in this whip till window here I've created a checklist so what this is imagine this is part of dtos it's not part of the dtos script yet but it it may make its way there and right now I've got this screen the title is web browsers and select more and more web browsers to install and you'll notice I've got a list of about 12 different web browsers that are available for installation on Origin Arch Linux based systems some of these are proprietary software most of them are free and open source software but I included some popular proprietary browsers because I know for example A lot of people are going to want to install Google Chrome for example I get it now you'll notice that some of these web browsers I've got four of them that actually are marked with a asterisk that's because those four web brows are already installed on my system I've included some code in the script where it checks to see if the binary for all of these exist on my system and the ones that it does find on my system I've told it to turn the checkbox on meaning mark it with asterisk that way I know I don't have to install brave for example because it's already marked I already know it's installed on the system and this is important for dtos because dtos is able to be installed on any Arch Linux based distribution so a lot of times you're installing dtos on you know Manjaro or Arco or whatever it happens to be what programs are already installed out of the box well unless you've been using that distribution for a while you may not know and this way it'll let you know I don't have to install Firefox Firefox is already installed now let me hit tab twice so if I tab I get to the OK button and if I hit OK it would try to install these I guess I could go ahead and hit the OK button all it's going to do it's going to ask me for my sudo password because it's running a pack command install everything that had asterisk and then nothing happened because the four programs that were marked were the ones I already had installed so it didn't need to install anything if I just wanted to bypass that all together maybe I didn't want to install any of these I could hit tab a couple of times and get to cancel by the way the next screen is the other internet program so first screen was web browsers this is other internet stuff such as telegram transmission element filezilla Discord Thunderbird yada yada yada I'll hit cancel and then we get to the next screen multimedia programs I have another list of about 12 things most of them are already marked because most of them I already have installed let me tab down to cancel and then the next screen is Office programs and then finally I have a screen of mostly free and open source games and I've also have a steam and lutras as a part of the list as well let me cancel that so that is really all this script is so let me briefly walk you through this script because the way I've got it it is a little complicated to read because I've got some things out of order with the way these functions are laid out I'm actually going to scroll all the way to the bottom so my whip tail box has five screens right it was uh browsers other internet multimedia office and games you say I'm just running these five functions at the end of the script now I've defined all of these five functions earlier in the script so if I just scroll back this is the games function right here and what this is I've defined a title that's the window title the title of that whiptail box remember was games and then the text is there were three lines of text up under the title let me turn off truncated lines here the three lines are gaming programs available for installation new line all programs marked with asterisks are installed already uh new line unselecting them will not uninstall them just letting you know they're unselecting anything that's already been installed will not uninstall it from the whiptail box or at least right now the way the script is designed I don't have that functionality built in but so I've got the title and the text to find these variables that I'll use later when I create the whiptail box and then I create this local array and this local array is nice because I have two different values I can assign it and this is very important I actually assign the first value which is called the key these are the names of the actual program that Pac-Man install so this is the package name and then the value in the second value here is what this is is the executable binary that you would run if the program is installed because remember I'm going to run a check to see if this is installed for example if I install the game sourbraten that's the package but when I launch sour button to run it on my computer sauerbraten has two different executables one of them is sauerbraten Dash client one of them is seller rotten Dash server and honestly I could search for either one of them I just chose that particular binary if it exists on the system I know the package seller button has already been installed on your computer and that's how I know I can mark that with a asterisk or not depending on whether I actually locate that binary so again the First Column the keys are the package that gets installed the second column would be the executable binary to search for on the system to see if it's already installed so I have that games function and if I scroll up of course I've got the office function which is the same multimedia and the same other internet the same browsers the same so all five of those just do the same thing title text they create their array of programs that appear in the menu and all of them have this line here program choices and and selected programs and an exit or install so they run these three functions and so if I keep scrolling back up it's like everything is in reverse order but those three functions that they run program choices it basically creates our menu so the way this particular function works is we're creating an array of choices it's an empty array but we're going to add stuff to it later you can see down here what we're going to do choices plus equals we're gonna append some stuff to the choices array so we start out with an empty array but we're going to add stuff to it and we're going to add three values to the array each time we add key spacer and either the word off or on depending on whether that executable binary exists on the system or not because when you mark it on you get the asterisks when you mark it off you don't get an asterisk so whiptail when you're creating your windows takes three values you have to give it three different strings with the last one being the word owner off all caps the first value is the title it's what appears next to the check box and then if you want to give it a description that would be the second value even if you don't give it a description though you still have to include the second value you could just leave it empty what I've chosen to do is because I don't want some of these dialog boxes to be really small I want them to be spaced out a little bit I have actually added a spacer this variable spacer and you can see what I've done here spacer equals this for Loop four I integer in a sequence of numbers 1 through 42 I want you to take 1 through 42 and then I want you to Echo the that at a number of times as spaces I want you to basically use a spacer of 42 spaces what I'm doing here actually let me just go back and rerun the script so you can see what I based this off of is currently the longest program I'm installing is this one here Microsoft Edge stable Dash bin and I wanted 42 spaces just to space that out all the way to the end of the window because the window is 78 spaces in length I specified that I could actually change that if I wanted to but otherwise this blue section with the menu would only go up to about right here and it just made the window look weird that's why I added the spaces or again you could add a description behind each of these as well if that's what you wanted to do now let me cancel through all of these and get back to emacs now one neat little thing is checking if the executable binary already exists on the system that's how I know to add the asterisks and I do that with this if statement here what we're doing is we're running a test essentially we're testing if the executable for command Dash V name of key exists so I go back to the terminal if I zoom in a little bit so if I do command LS right it runs the ls command people will often use the command you know name of command to bypass their Alias because my LS is actually Alias to EXA but if I do command LS it's actually LS right well all I'm doing is I'm running command and then the check boxes array and the key value so I'm going back down here we'll take browsers for example we're taking each of these key values right that's the first field and we're running command name of this field and we're running a check basically to see Dash x what we're doing is we're checking if that command so command Firefox or is it in the path and is it executable if it's not in the path and it's not executable Firefox is probably not installed on the system so if it's not installed on the system Firefox will be marked off meaning there's no asterisks in the menu now if I run the check and Firefox is part of the path and is executable it will be marked with own I hope that makes sense so that's how we get the menu set up space correctly and the asterisks and then the next function selected programs here is actually us actually creating the whip till window you can see it's very simple it's really it's really one line but I've spaced it out to be four lines because I've got the line breaks here with the backslashes but you do whip tail and I've added a title dash dash title and the title is the title variable remember each of the functions I created browsers and other internet and multimedia each of them have a title and then dash dash checklist and then text and remember each of those functions of the five main Windows has a text box as well and then I've got the size 22 lines is how long it is 78 characters wide and then 12 is the check box itself so if I added 15 things to one of those checklists what will happen is you'll get a little scroll bar so you could scroll through the rest I can actually show you this in action If instead of 12 I make this 8. let's go back to the terminal I'll rerun whiptail here and now I can actually scroll down it's kind of hard to tell that there was a scroll bar here I probably should color that a little differently but you can actually scroll through the list see this one actually looks a little better I I see what the problem was on the first screen my spacer bled into the scroll bar so I need to fix that but you can see now we got a proper scroll bar where if you go through the list it's obvious what is going on all right so I should fix that what I'll probably do right now we'll just fix this on camera let's make the spacer a little smaller let's make it 38. let's go back to the window again just trial and error and now that's actually perfect right with one space uh to spare all right but that works and it's a little a little too much space on the wrist but I couldn't get it quite right on all of them but that works that's that's good enough so we got whip till dash dash title the title dash dash checklist our text and then the size of the window and then the next thing I feed it is the choices array so this is what actually is in the checklist so it's basically if I go back up program choices and inside it we had the choices array where we did the for Loop getting the spacer and then the on or off you know checking if the executables all that that is what is populating the actual list and then finally you have this weird stuff here which is another peculiarity with whip till that you really don't have in dialogue for whatever reason whiptail does some strange things with standard out and standard error I think it swaps them around so what's happening here I think you have to swap them back to the correct streams uh and that's a little weird we found some of this if I go back to the documentation on this website here at wikibooks.org I scrolled back they had some examples of some checklists and some radio lists and you can see right here it's basically kind of what I had it so you know they've got it all on one line instead of broken up but at the end they have three and then greater than sign and one and one greater than sine and two two greater than sine and three they're just swapping all the standard and standard out standard error they're just directing the streams to different places and it's kind of weird I I don't know why they do that I have seen complaints online doing some research about whiptail that some people really don't like the way it redirects standard error but that's the way the program works so just know that and then finally I have this function here exit or install I found this online I think in the whiptail documentation but all this does is it checks the exit status for the program so if the whip tail box works correctly so if I select some stuff and hit okay it should exit with a code 0 meaning the program succeeded now if the exit code is anything other than zero it's a failure of some kind so I have this if statement if the exit status is zero meaning the program succeeded I want you to do this here I want you to get a list of all the programs so this will be the ones that we've marked with the asterisk and then I want you to run some said substitution because I want you to get rid of all the quotation marks around them you know because I'm going to feed these into Pac-Man or in this case I'm actually going to use paru because some of these are Aur programs then paru is going to install the programs that were marked and then I've got the two pipe symbols so this is or so paru is either going to install the programs or it's going to basically have an error right if the error is going to be I just want you to Echo it failed to install the required packages for some reason then the else part of the if statement so any exit status other than zero is a failure so just Echo user selected cancel because that's what the failure is going to be it's me not choosing okay it's going to be me choosing cancel so again so if I for example hit OK here so tab down and hit OK let's exit status zero so it's going to run paru install all of these programs or it's going to at least try to let me give it my sudo password and it didn't install them it tried to but they were already installed so it just bypassed it but the next part here if I hit cancel that'll be an exit status of a code other than zero so it will not try to install any of these right so you know I don't go back to the terminal and get paru trying to to run an install so that is how that works with the exit statuses and I know some of this is a little complicated I again I spent several hours over the last couple of days working on this script let me Zoom back out it's it's actually kind of lengthy and I know the way I wrote it is probably not the best again I'm not much of a programmer and you know I do a little bit of bash scripting but I don't do a lot of bash scripting but this was fun I've got to say making this was really nice and I may I I'm seriously thinking about adding this to the dtos script the installation script because I know a lot of people do have some issues with dtos the way it's currently set up because so much of the input is done at the command prompt for example when you get to what window manager would you like to install it asks you right there in the TTY at the command prompt well I can do a whip till little dialog box hey which window managers you want to install and have a list X monad awesome Q10 whatever else I want to add to dtos eventually and that just makes it a little more user friendly it's also obvious when you get to that section of the installation because because if you're not actually watching dtos install sometimes you'll get to that yes or no question about installing xmo net or the yes or no question about installing qtol and it's not obvious that this the installation has paused and waiting for input but with these whip tail boxes that'll pop up it's obvious hey you need to come answer this question right now so that's kind of what I've been thinking is is adding these whiptail dialog boxes to make things a little more user friendly originally I was actually planning on making the entire dtos installation nothing but command line nothing but the command line interface because I thought that would be simpler at least simpler for me because I didn't have to worry about a UI experience but the more I see people playing with dtos the more issues I get on gitlab the more I see people trying to install DTS dtos on video the more I realize I need to make this thing I guess a little easier right a little more obvious what is going on so uh whip Tails pretty cool for those of you that want this example script what I'll do is I'll actually push this example code let me get back to the desktop here I'll push this example script to the dtos repository so go to gitlab.com dtos slash dtos and you will find whiptail-example.sh over on that gitlab now before I go I need to thank a few special people I need to thank the producers of this episode Gabe James Maxim Matt Mitchell Paul Royal West Armor Dragon bash potato Chuck Commander angry George Lee Mythos Nader John Paul peace or Polytech realities for Less red profit role in tools dibbler and Willie these guys they're my highest tiered patrons over on patreon without these guys this episode about whiptail 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 want to see more freaking open source software subscribe to distro 2 over on patreon alright guys peace whip it whip it good
Info
Channel: DistroTube
Views: 14,237
Rating: undefined out of 5
Keywords: bash tutorial, shell scripting, bash scripting, linux tutorial, linux, gnu linux, linux distro, linux for beginners, switch to linux, learn linux, command line, linux terminal, windows vs linux, dialog box, whiptail, user interface, command line tools, linux operating system, linux software, linux programming, ncurses, linux scripting, free software, open source, how to, linux distro review, arch linux, debian, ubuntu, fedora, linux mint, distrotube
Id: KsiPYMKSHU8
Channel Id: undefined
Length: 23min 9sec (1389 seconds)
Published: Wed Mar 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.