Use a table driven menu
The menu to select from /bin
, /sbin
and other is very repetitive. A table-based approach would be more compact, easier to write and maintain. Try to use an array, as you did in other places of the script.
First, put the directories in an array, for example:
dirs=('' /bin /sbin /usr/bin) # fill the rest
Note that I added a dummy empty first element. This is to make the 1-based indexes of your menu work with the 0-based array indexes.
Build the menu from this array using a counting loop from 1, the first non-empty element:
for ((index = 1; index < ${#dirs[@]}; index++)); do
echo "$index) ${dirs[$index]}"
done
To validate the user input, simply check if ${dirs[$index]}
is empty. If yes, the input is invalid.
Security
Maybe it is normal to have plain text files in this directory or maybe this is something I did unintentionally when experimenting writing this script. (An earlier version of this script wrote all the man pages to plain text files.)
No, it is not normal to have .txt
files in any of those directories. So I guess your earlier script put them there. But a normal user should not have write access to these directories. Which seems to suggest that you're playing with a privileged account, possibly root
. That's a bad idea, avoid using root
casually.
Simplify
You can simplify [[ $n = 'b' || $n = 'B' ]]
using pattern matching: [[ $n == [bB] ]]
.
Strangeness
You don't need the -1
here.
menu=( $(ls -1 ${dir}) )
When inside $(...)
, ls
does not columnize its output.
In this code, I don't see why you need basename
:
echo "$(( i++ ))) $(basename $m)"
It seems the value of $m
will always be a simple filename without a path part.
manpreet
Best Answer
2 years ago
I wrote the following script in the hopes of streamlining the finding and reading of multiple manual pages. Since I am always looking up different utilities' manual pages I thought this would a good learning aid.