I must make a script which will be executed as follows :
./script.sh Directory/n[0-9].car where there can be as many numbers as we may want.
I'd like to : - ignore the Directory ; Meaning I just need n[0-9].car
- Check the name of the file : has it got some Alphabetic Characters after the n? in which case it's not a proper file name.
- *Extract* the number in the name, to be able to compare it to something else after.
FILE=$( basename $1 )
CORE=$( echo $FILE | sed '/s/^n//' | sed 's/$\.car//' )
TEST=$( echo $CORE | egrep -o '[[:alpha:]]' )
if [ -n "$TEST" ]
then
echo "File name not valid!"
fi
Presuming you do not literally mean ./script.sh Directory/n[0-9].car which is a globbing expression which may expand to multiple files as input, but that you mean a single file that consists of an n followed by multiple digits (n>=1) followed by the extension .car:
If so, you could try this alternative which uses no external programs:
no_n="${1##*/n}"
nr="${no_n%.car}"
case $nr in
*[^0-9]*) echo "File name ${1##*/} invalid.."
esac
The second option does the same but is not posix compliant. If a script is posix compliant it will work with most shells. The second option uses more advanced options that are available in ksh93 and bash.