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!
I the majority of web pages I visit were providing the same level of usefulness as this one, I would cut off my fingers 🙂
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…