Free tech support / small logo


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?

Dave's Answer:

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!









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments

I the majority of web pages I visit were providing the same level of usefulness as this one, I would cut off my fingers :)

Posted by: Stuart Rothrock at September 28, 2010 5:02 PM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.