[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: here is a good one :)




> Is there anyway under bash to take a filename, like text, and then get the
> numeric ascii value of each character in the name?
> man ascii shows 
> 
> 
> 	164145170164
> 
> Is there anyway to do this?

Chuck, you owe me.

Ok, bash doesn't have anything like ASC() like BASIC does, so I made my
own asc function in C. You'll need it to get the bash script to work.

Here's the code for asc:


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   char c[] = {atoi(argv[1]), '\0'};

   printf("%s\n", c);

   return 0;
}


Compile that, put it in /usr/bin (or wherever), and then here's the
script. Note that I only check in the ASCII range of 43-173. If you go
outside of that, you'll get the "unary operator expected" errors.


#!/bin/bash

PLACE=1

while [ $PLACE -le ${#1} ]; do
   LETTER=$(echo $1 | cut -c $PLACE)
   COUNT=43
   while [ $COUNT -le 173 ]; do
      ASC=$(asc $COUNT)
      if [ $ASC = $LETTER ]; then
         echo -n $COUNT
      fi
      COUNT=$((COUNT+1))
   done
   PLACE=$((PLACE+1))
done

echo


-
To unsubscribe, send email to majordomo@luci.org with
"unsubscribe luci-discuss" in the body.