
How can I count executable binaries on my Linux box?I've been looking at your script to count executable binaries in the book Wicked Cool Shell Scripts and I think that there's a problem in the script. Specifically, what happens if I have a directory in my PATH more than once? Great point. The script you're talking about is called how-many-commands.sh: #!/bin/sh
# how many commands: a simple script to count how many executable commands # are in your current PATH. myPATH="$(echo $PATH | sed -e 's/ /~~/g' -e 's/:/ /g')" count=0 ; nonex=0 for dirname in $myPATH ; do directory="$(echo $dirname | sed 's/~~/ /g')" if [ -d "$directory" ] ; then for command in $(ls "$directory") ; do if [ -x "$directory/$command" ] ; then count="$(( $count + 1 ))" else nonex="$(( $nonex + 1 ))" fi done fi done echo "$count commands, and $nonex entires that weren't marked executable" exit 0 The problem is indeed stuck in that script, because when I break down the PATH, I manage the problem of having spaces in the directories neatly (I convert them to '--' then convert them back after the "for" loop) but what if there's more than one occurrence of a directory in the path? Turns out that's pretty easily done by having a bit more code in the myPATH statement: myPATH="$(echo $PATH | sed -e 's/ /~~/g' -e 's/:/ /g' | sort | uniq)"
Can you see what I added? Not too difficult, but I'm using the super-helpful "uniq" command to remove duplicates, but it doesn't work until you ensure that your path entries are in order, hence the use of the word "sort"). Now you'll have an accurate executable / non-executable file count for your system and your set PATH value.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Shell Script Programming
(Article 6942)
Tagged: bash, linux, shell scripting, shell scripts Previous: How do I build a Google Co-op Custom Search Engine? Next: How can I save mp3 links from email messages? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.
Rather amazingly, there are no comments on this article yet.
I have a lot to say, but ...
I do have a comment, now that you mention it!
|
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!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|