Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

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!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments
Rather amazingly, there are no comments on this article yet.

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

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









Remember personal info?


Please note that I will never send you any unsolicited commercial 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.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 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]