Compare Output of Command and Match to CSV List or File

The following script provides a method for scanning the output of a command, matching it in another list, and performing an action. The example script, install.sh, shown is using the lsusb command to scan USB devices inserted against the known-devices.csv file and perform an action. Here’s how it looks in motion:


* This script will automatically determine the wireless
* adapter and install the appropriate drivers.

Detected: Asus WL-167g
Running Script in 3rd Field of CSV... OKAY!

Scripting language is pretty much self-documenting so I won’t bother explaining too much. If you have questions or suggestions, please let me know. The following script is flexible and can be used in many different situations so I encourage you to customize it to fit your needs. Enjoy!

install.sh: The main script that is ran to try and match the output of the lsusb command with another files list and act accordingly.


# Create tmp files for verification along the way
touch tmp
touch tmp2

echo "* This script will automatically determine the wireless"
echo "* adapter and install the appropriate drivers."

# Run lsusb and grab only the prod/vend id field
lsusb | cut -d' ' -f 6 | while read line
   # Use grep to search for $line of lsusb output in known-devices.csv file and save to tmp
   grep "$line" known-devices.csv > tmp
   if [ $? = 0 ]; then
      echo -n "Detected: "

      # Use awk to print only field 2 of the tmp file (also field 2 of known-devices.csv)
      awk 'BEGIN{FS=",";}
         {print $2}
      }' < tmp
      # Prepare and execute the third field of the known-devices.csv file
      echo -n "Running Script in 3rd Field of CSV... "
      cut -d',' -f 3 tmp > tmp2
      chmod +x tmp2

# Check the tmp2 file and see if it is empty.  If so, display error.
if [ `ls -l tmp2 | awk '{print $5}'` -eq 0 ]
  echo "No known devices found."

known-devices.csv: The comma separated file in which the install.sh uses to check against. In this particular example, the prod/vend usb ID is the first field, the friendly name is in the second field, and the installation command is in the third field.

USB ID   | USB NAME     | DRIVER INSTALL COMMAND(S)                            |
0ace:1215, IOGear GWU523, lib/zd1211/zd1211rw_install.sh
0b05:1723, Asus WL-167g , lib/rt73/rt73_install.sh

rt73_install.sh: This is the script that is called by the known-devices.csv file above. If there was a simple one-line command, you could specify it in the third field of the known-devices.csv file.


   insmod rt73.ko > /dev/null 2> /dev/null
   if [ $? -eq 0 ] ; then
      echo "OKAY!"
        # These are optional permanent install commands that will be ran upon
        # successful completion of previous command.
        mkdir -p lib/modules/`uname -r`/kernel/drivers/net/wireless/rt73/
        cp rt73.ko lib/modules/`uname -r`/kernel/drivers/net/wireless/rt73/rt73.ko
     )  > /dev/null 2> /dev/null
      echo "FAILED!"
      echo -n "Error: Driver already installed, wasn't found, or failed to insert."
      echo "Check 'lsmod' and 'dmesg' for debugging information."

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:

There's 0 Comment So Far

Share your thoughts, leave a comment!