r/linuxquestions • u/billhughes1960 • Apr 16 '21
How I spent the pandemic: Learning Bash scripting, but now I want to take it to the next level...
I am NOT a programmer and have no formal training, but ever since the 80s I've written little Basic (now Bash) scripts to do some specialized chores for me. My current script has grown quite large and is more of a menu driven utility vs. command line with parameters.
So I was thinking of putting a true GUI front end on it. The app is useful in both Gnome (GTK) and KDE (QT). Personally, I spend the majority of my time in Gnome, and I know GTK apps run on KDE even if they don't look the best, but I thought I'd ask here...
Any suggestions on how to tackle this as an entry level project: GTK, QT, ncurses? Something else?
EDIT: Thanks for al the comments. As many of you have said, python looks great. I should be able to rewrite the current bash script to python then I'll look into Zenity to put a GUI on it.
22
Apr 16 '21
Python is today's BASIC (in the sense that anyone can learn it) except unlike BASIC, this tends be far more adequate for far more many things. You find 8 year olds writing it. Hobbyists. Enterprise. Academia. Everywhere.
I'm learning it slowly. I know Perl. I can do things quickly in Perl. Perl is getting long in the tooth, and results in difficult-to-maintain code if you have to read someone else's. I've been lazy. I have so little free time now, learning something new is kind of a drag.
But learning Python I am, because it's everywhere.
Everywhere.
https://www.tutorialspoint.com/python/python_gui_programming.htm
2
u/Istalriblaka Apr 17 '21
Coming from C++, Python's ubiquity is both a boon and a bane in my opinion. It's foreign enough to my skillset that I need to figure out what a generic syntax error is. But it is a very diverse tool, and I think the only place you won't see it is commercial products. I actually had a Python codebase I was given to do some interview homework not all that long ago even though the position deals almost solely with C++.
20
u/Darnel1l_ Apr 16 '21
For QT,GTK,ncurses you still need to learn C, C++ or Python etc..
You can try zenity.
13
u/ZaitsXL Apr 16 '21
The next level after Bash is usually a programming language, these days mostly Python and Go
8
u/addisonbean Apr 16 '21
It definitely depends on what the scripts are doing and why you want a gui. It is more for a learning experience? Or do you just want something easier to use?
Depending on what the scripts do, hooking them up into another script using rofi could be a good solution. I use rofi for a few things on my desktop, including switching themes and connecting to bluetooth devices. Hearing you say it's very menu driven makes me thing rofi would be a good option.
7
u/hwoodice Apr 16 '21
zenity allow you to use simple graphical widgets in bash scripts.
2
u/Parker_Hemphill Apr 17 '21
Ah zenity. Forgot all about this little gem! Back when I was a SysAdmin working in a NOC I used zenity to pop-up messages on our network node board. (It was ONLY for showing network so I could only make messages that would "pop up" and go away after a few seconds. We ran Solaris so I was limited on how I could do it.
5
5
u/DonkeyTron42 Apr 16 '21
For something like that, I would recommend using Python with Tkinter. Or, If really want to keep it old school, just use Tk.
2
u/zoharel Apr 16 '21
You've been writing code since the eighties, but you aren't a programmer? Also, you all how to do "this," but then the only thing you say about what you're actually doing is that it has a menu. Could you provide more detail about what you're doing? It might matter.
2
u/billhughes1960 Apr 17 '21
Here. Have a function. :)
function MenuChoice () { clear echo What would you like to do: echo 1 - Download and install icons then select folder color echo 2 - Already installed, just want to change folder color echo 3 - Make Symbolic Link arrows transparent echo 4 - Exit read InstallType if [ $InstallType = "1" ] then InstallIcons elif [ $InstallType = "2" ] then ColorFolders elif [ $InstallType = "3" ] then TransparentLink fi exit }See, nothing fancy. Certainly nothing worth calling yourself a Programmer over.
2
u/zoharel Apr 18 '21
Well, you don't need to be a programmer of sophisticated software to be a programmer. It's fine to recognize your limitations, but a programmer is one who writes programs. You might consider it dabbling up to a point, but after that long, I think we may as well just let you into the club.
Anyway, the good news is that if you wanted to switch to something like Python, as people are suggesting, it doesn't seem like it would be a huge problem. Though, if you're switching languages, you might just try out a couple of options and see where they get you.
1
u/billhughes1960 Apr 18 '21
:) Thanks u/zoharel ! I feel like that moment in Tron when Jeff Bridges realizes he's the Programmer! (Now I really am dating myself!)
My actual career is in audio production for TV & radio. I picked up coding as a way to manipulate data (sound effects databases), machine control (xmodem over rs-232!), and the automation of mundane tasks like label printing!
It has provided me a competitive edge over my competition.
Hey, when you stop learning, you may as well be dead.
1
u/zoharel Apr 18 '21
Absolutely. I've been writing code since the early eighties, myself, and still haven't tried everything I probably should. I've got some Z80 assembly projects nearing the top of the list at the moment.
4
u/aaronryder773 Apr 16 '21
If you don't mind I would also like to learn Bash scripting and I would like you to point me to some guides, books, tutorials, websites, etc. Thank you!
4
u/findmenowjeff Apr 17 '21
https://mywiki.wooledge.org/BashGuide this is probably one of the best learning resources I've seen out there. I'd be wary about most of the guides/tutorials on the internet, but that is a great one. Specifically I'd stay away from the TLDP guide, and the ABS guide, which are full of mistakes and errors.
1
7
u/matjam Apr 16 '21
stop; don't be doing this in bash.
Python would be a good choice. There are bindings to GUI libraries for Python that work very well. I've used PySimpleGUI for the kind of thing you're doing with great success.
2
u/flavius-as Apr 16 '21
I would suggest instead to stick to shell scripting, with a twist:
Pick up xonsh and start scripting it, you can start with your knowledge, and slowly mold it into python.
I have written thousands of lines of shell (as a part of a project solving specific needs) and I can tell you: never again!
You cannot compare real data structures and error control to things like set -x or set -e
2
u/atred Apr 16 '21
For something simple that you can use in bash to displays menus look into "yad" (a fork of zenity)
2
1
1
1
1
u/klc3rd Apr 17 '21
Never tried to do this but I believe you may be able to use ncurses in bash scripting. I think Gtk or QT may be out of the question unless you switch languages.
1
Apr 17 '21 edited Apr 17 '21
Use PyQT5 or PySide 2. Are you sure that a simple menu based interface won't work for you? those are easy enough in bash
1
u/ntropy83 Apr 17 '21
I'ld go with c++ and qt. I have developed some programs with it and c++ can be easier than bash scripting. If you are apt in c++, learning python will be easy. Another thing is portability, you can use the qt program on every OS. And I recently discovered a library for Web Assembly, so I ported my program to run in a web page.
1
u/OldSunGuy Apr 17 '21
I'd say the next scripting level is https://www.tcl.tk/
I'd recommend programming with python.
1
u/reditanian Apr 16 '21
Python, definitely. Its syntax is straightforward and it reads almost like plain English. It is vastly more powerful than bash, and has bindings for many UI toolkits.
1
u/Phantomofthesoup Apr 16 '21
Python and the click library!
The click library allows you to make some great cli interactions
1
u/themedleb Apr 17 '21
GTK apps looks better in KDE than QT apps in Gnome. I wish Gnome improves its QT theming.
0
u/Peetz0r Apr 16 '21
What others already said: don't do this in bash.
Shell scripts don't really scale beyond a couple of lines. Most shell scripts exist to automate small mundane tasks, or to start pother programs.
Learning a new programming language sounds like a hard thins to do. And it kinda is. But it's also learning the right tool for the job. Using the wrong tool is not going to make anything easier.
I also agree with many others that Python is probably the best option here. Easy enough to learn, lot's of people using it for similar stuff, common enough to ask for help basically anywhere, plenty of libraries available, etc. But feel free to pick anything else if that feels right.
1
Apr 17 '21
I have a shell script that is 500 lines long and has been in use for almost a decade, and I've only had to add things to it a couple times. It really depends on your use case and what you're doing. Sure I wouldn't want to add much more to it but it is fine for what it is. It is well organized and documented. Python is great and all but if you're doing tons of file manipulations/interacting with the file system/using sed/awk a lot then it's fine. Any yeah I know Python and use it practically everyday. If I need to do a similar task to what that bash script is doing, I'd probably still use bash.
0
u/og-cheeselover97 Apr 17 '21
Go learn an actual pl , learn python to start that'll be easy to get a gui after .
0
Apr 16 '21
[deleted]
26
u/bigfig Apr 16 '21 edited Apr 16 '21
I agree absolutely. OP's proposal would be like someone becoming really good at horseback riding and concluding they should enter Formula 1 or the Daytona 500.
Yes there are reasons to push bash to its limits, just as there are reasons to use a team of horses in the 21st century to do work like harvesting timber, but the intended use of bash is to control other programs, and that is its forte.
1
u/Extension_Flatworm_2 Apr 17 '21
C++ is the main language for Linux I am tols.
2
u/Aberry9036 Apr 17 '21 edited Apr 17 '21
C++ is a fairly low level language and probably complicated/overkill for reproducing a bash script with a ui. Perhaps python, golang, or maybe nodejs if they only wanted a web ui, would all be relatively easy to learn high level / heavily abstracted languages.
1
u/Sol33t303 Apr 17 '21
Don't even need to reproduce the script. Just make a GUI wrapper in python, this is what is done in many languages and it usually works quite well.
1
u/Aberry9036 Apr 17 '21
Depends how much of a challenge they want and if they want to write tests - the latter would mean that they could test the calls to the script and the script’s outputs separately, but it’s rather more clunky.
3
u/egoalter Apr 16 '21
I'm really curious how your bash scripting is useful with a GUI. Or it sounds like your scripts are more an attempts at making a program treating bash as a full programming language vs. a method to automate complex tasks?
The scripts I tend to write are very much based on being a utility. Something that takes input and produces an output. If you don't mind, could you describe one of the bash scripts you've created? It would be interesting to see what you've created.