Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


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?


Dave's Answer:

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.


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  





Categorized: Shell Script Programming   (Article 6942, Written by )
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?




Reader Comments To Date: 1

Antriksh Pany said, on March 6, 2010 2:13 AM:

I think we may have to have each PATH directory in a separate line (rather than just space separated) to be able to sort them.

So instead of
myPATH="$(echo $PATH | sed -e 's/ /~~/g' -e 's/:/ /g' | sort | uniq)"
We may want to have
myPATH="$(echo $PATH | sed -e 's/ /~~/g' -e 's/:/\n/g' | sort | uniq)"


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, Dave, for all your helpful information by buying you a cup of coffee!

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











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 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. Further, 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. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.