test the string is char or integer

How will test the string contains numeric character or alphabet, is there any script to test ?

If the string is somewhat fix, you can test it with case/esac, or should it be generic?

it is generic.

I had the problem not this way yet nor could I find a nice tool to test it. So I come up with this ugly but working solution:

echo "$1" |\
sed 's/./&\n/g' |\
awk '
        /[a-zA-Z]/ {a+=1}
        /[0-9]/    {n+=1}
        END{
                sum=a+n
                print sum,a,n
                if ( a == 0 && sum == NR-1 ) { print "numeric" }
                if ( n == 0 && sum == NR-1 ) { print "alpha" }
                else { print "something else" }
        }'

If you are using phksh, ksh88 or ksh93, here is an example of testing an input to determine if it is all numeric, letters or a mix of numbers and letters:

#!/bin/ksh

INPUT="ABC"

case $INPUT in
   ( +([0-9])       ) echo "$INPUT is all numbers" ;;
   ( +([a-zA-Z])    ) echo "$INPUT is all characters" ;;
   ( +([0-9a-zA-Z]) ) echo "$INPUT is alphanumeric" ;; 
   (               *) echo "$INPUT is something unknown" ;;
esac

exit 0
> cat test_alpha 
sample1="abcdef"
echo $sample1 - checking alpha
if [ $sample1 = `echo $sample1 | tr -d [0-9]` ]; then  echo "same"; fi

sample2="abc123def"
echo $sample2 - checking alpha
if [ $sample2 = `echo $sample2 | tr -d [0-9]` ]; then  echo "same"; fi

sample3="123456"
echo $sample3 - checking numeric
if [ $sample3 = `echo $sample3 | tr -d [:alpha:]` ]; then  echo "same"; fi

sample4="123abc456"
echo $sample4 - checking numeric
if [ $sample4 = `echo $sample4 | tr -d [:alpha:]` ]; then  echo "same"; fi

and its execution; showing the first is alpha (same) and the third is all numeric (same)

> test_alpha 
abcdef - checking alpha
same
abc123def - checking alpha
123456 - checking numeric
same
123abc456 - checking numeric

Why not use plain old case?

case $string in
  *[!0-9A-Za-z]*) echo "Not pure alpha + numbers" ;;
  *[!0-9]*) echo "Pure alpha";;
  '') echo "Empty string (duh)" ;;
  *) echo "Pure numbers" ;;
esac

I thought of case 1st too, but I was not able to put the expressions together so it would work correctly. Since I was nosy I tried your case script, and it does not work for me either like my former tries with case (just changed $string to $1):

root@isau02:/data/tmp/testfeld> ./era.ksh ab1cde
Pure alpha

You are correct of course; the matching is greedy, and tries its darndest to find a match, so using a negation between wildcards will find a match if there is a way to match it, regardless of the other characters. So the label "pure alpha" is wrong; it should be "alpha + possibly numbers", or the logic should be changed to do additional cases within that case statement.

case $string in
  *[!0-9A-Za-z]*) echo "Not pure alpha + numbers" ;;
  '') echo "Empty string (duh)" ;;
  *[0-9]*[!0-9]*|*[!0-9]*[0-9]*) echo "Mixed alpha + numbers" ;;
  *[!0-9]*) echo "Pure alpha";;
  *) echo "Pure numbers" ;;
esac

How greedy! :wink: :smiley:
I guess we found, thanks to the OP, that we are lacking a fine small binary standard tool to check stuff like this! :slight_smile: (though I never needed that yet). But aren't there tools about for everything?! :smiley:

It is probably time we started using POSIX character classes in our examples more often!

#!/bin/bash

shopt -s extglob

INPUT="123"

case $INPUT in
   ( +([[:digit:]])  ) echo "$INPUT is all numbers" ;;
   ( +([[:alpha:]])  ) echo "$INPUT is all characters" ;;
   ( +([[:alnum:]])  ) echo "$INPUT is alphanumeric" ;;
   (                *) echo "$INPUT is empty or something unknown" ;;
esac

exit 0