How do I write a full-screen bash shell script?
Dear Dave: I am trying to write a full-screen utility that displays the permissions of a directory and all the directories in the directory's path. It should also show other information about the directories, as the cursor is moved up and down the list of directory names. Can you offer me some pointers to get me started?
Well, first off, this kinda seems like it might be a homework assignment from a class that this chap is taking, so I'm not going to just write a script and post it. In fact, as a teacher, I have to say that I'm not in favor of people finding shortcuts to solve their programming assignment projects. If you're stuck on an assignment, your obligation is to ask the instructor for help or pointers, not to search the Internet for people you can hoodwink into writing your assignments.
However, I could be wrong. :-) And in any case, I also remember that when I was an undergraduate, I got in trouble for referencing Don Knuth's wonderful Art of Computer Programming: Volume 1, Fundamental Algorithms book when we were supposed to ascertain which algorithm would produce an optimal sort for a specific type of unsorted input.
Plus, it's an interesting question!
So, yeah, I won't write the program for you, dear reader, but I will offer you some hints regarding what direction you can take to accomplish this task.
First off, to gain rudimentary cursor control within a shell script, you'll want to use tput (you can read the tput man page to learn more) with friendly and mnemonic invocations like "tput cup 0 0", which moves the cursor to the top left corner, and "cols="$(tput cols)" to store the number of columns on the screen in a variable. It's not easy, as you can see, but you can definitely write a script that has complete control over the screen.
However, if I were trying something like that, I think I'd just move right to curses and write the program in C, not as a shell script. It would doubtless save your sanity. (learn more about curses by reading the curses man page).
You also have a bit of a challenge with accepting input from the keyboard, so you'll want to look at the specific settings to stty and then use a read statement within some sort of processing loop.
Finally, in terms of ascertaining more information about a file or directory within a shell script, that's something I cover in detail within my popular book Wicked Cool Shell Scripts, which I'll recommend you pick up and read through. You can start by recognizing that you can invoke ls with a variety of different parameters, then parse and separate out the resultant parameters into different variables, then output those variables as you desire. You can also recursively navigate a directory tree with ls or its power nephew find.
Good luck with this project and let me know how it all comes out!
Related Shell Script Programming articles:
✔ Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...✔ Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...✔ Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...✔ Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...✔ Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
I do have a comment, now that you mention it!
Check This Out Too...
Look for Answers
All Our Categories
Apple iPad Help
Articles and Reviews
Auctions and Online Shopping
Blogs and Blogging
Building Web Site Traffic
Business and Management
Computer and Internet Basics
d) None of the Above
Google Gmail Help
Google Plus Help
Industry News and Trade Shows
iPhone and Cell Phone Help
iPod, Sony PSP and MP3 Player Help
Kindle Fire Help
Mac OS X Help
Pay Per Click (PPC) Advertising
Search Engine Optimization (SEO)
Shell Script Programming
Tech Support Video Help
The Writing Business
Twitter, LinkedIn and Social Network Help
Unix and Linux Help
Video Game Tips and Help
Windows PC Help
Find Me on Google+
ADT on G+