Kristian
Kristian

Reputation: 1389

Writing a shell conditional for extensions

I'm writing a quick shell script to build and execute my programs in one fell swoop.

I've gotten that part down, but I'd like to include a little if/else to catch bad extensions - if it's not an .adb (it's an Ada script), it won't let the rest of the program execute.

My two-part question is:

Thanks!

Upvotes: 1

Views: 276

Answers (4)

Jonathan Leffler
Jonathan Leffler

Reputation: 753970

There are multiple ways to do it. Which is best depends in part on what the subsequent operations will be.

Given a variable $file, you might want to test what the extension is. In that case, you probably do best with:

extn=${file##*.}

This deletes everything up to the last dot in the name, slashes and all, leaving you with adb if the file name was adafile.adb.

If, on the other hand, you want to do different things depending on the extension, you might use:

case "$file" in
(*.adb)  ...do things with .adb files;;
(*.pqr)  ...do things with .pqr files;;
(*)      ...cover the rest - maybe an error;;
esac

If you want the name without the extension, you can do things the more traditional way with:

base=$(basename $file .adb)
path=$(dirname $file)

The basename command gives you the last component of the file name with the extension .adb stripped off. The dirname command gives you the path leading to the last component of the file name, defaulting to . (the current directory) if there is no specified path.

The more recent way to do those last two operations is:

base=${file##/}
path=${file%/*}

The advantage of these is that they are built-in operations that do not invoke a separate executable, so they are quicker. The disadvantage of the built-ins is that if you have a name that ends with a slash, the built-in treats it as significant but the command does not (and the command is probably giving you the more desirable behaviour, unless you want to argue GIGO).

There are other techniques available too. The expr command is an old, rather heavy-weight mechanism that would not normally be used (but it is very standard). There may be other techniques using the (( ... )), $(( ... )) and [[ ... ]] operators to evaluate various sorts of expression.

Upvotes: 3

Zsolt Botykai
Zsolt Botykai

Reputation: 51613

Extension:

fileext=`echo $filename | sed 's_.*\.__'`

Test

if [[ x"${fileext}" = "xadb" ]] ; then
  #do something
fi

Upvotes: 0

choroba
choroba

Reputation: 241908

To get just the extension from the file path and name, use parameter expansion:

${filename##*.} # deletes everything to the last dot

To compare it with the string adb, just do

if [[ ${filename##*.} != adb ]] ; then
    echo Invalid extension at "$filename".
    exit 1
fi

or, using 'else`:

if [[ ${filename##*.} != adb ]] ; then
    echo Invalid extension at "$filename".
else
    # Run the script...
fi

Upvotes: 0

ruakh
ruakh

Reputation: 183341

There are ways to extract the extension, but you don't really need to:

if [[ $filename == *.adb ]] ; then
    . . . # this code is run if $filename ends in .adb
else
    . . . # this code is run otherwise
fi

(The trouble with extracting the extension is that you'd have to define what you mean by "extension". What is the extension of a file named foo? How about a file named report.2012.01.29? So general-purpose extension-extracting code is tricky, and not worth it if your goal is just to confirm that file has a specific extension.)

Upvotes: 3

Related Questions