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

Re: [Sheflug] Easy bash question



José Luis Gómez Dans wrote:

Hi all,
I have heaps and heaps of files in directories I need to convert
(they are eps files that need to be converted into Fig's, using
pstoedit). I have used a simple loop to do this:
for i in $(ls *.ps); do pstoedit -f xfig <blah> $i > $i.fig ; done

This is fine, but my_file.eps turns out to be my_file.eps.fig,
which is not aesthetically pleasing (and obviously a source of much
annoyance :D). So I was wondering, what's the best way of trimming the
extension from my $i loop variable? Do I have to use sed for this? Do
I actually need to read the manual?
From O'Reilly's Learning Bash:

Graphics file conversion utilities are quite common because of the plethora of different graphics formats and file types. They allow you to specify an input file, usually from a range of different formats, and convert it to an output file of a different format. In this case, we want to take a PCX file, which can't be displayed with a Web browser, and convert it to a GIF which can be displayed by nearly all browsers. Part of this process is taking the filename of the PCX file, which ends in .pcx, and changing it to one ending in .gif for the output file. In essence, you want to take the original filename and strip off the .pcx, then append .gif. A single shell statement will do this:

outfile=${filename%.pcx}.gif

The shell takes the filename and looks for .pcx on the end of the string. If it is found, .pcx is stripped off and the rest of the string is returned. For example, if filename had the value alice.pcx, the expression ${filename%.pcx} would return alice. The .gif is appended to form the desired alice.gif, which is then stored in the variable outfile.

If filename had an inappropriate value (without the .pcx) such as alice.jpg, the above expression would evaluate to alice.jpg.gif: since there was no match, nothing is deleted from the value of filename, and .gif is appended anyway. Note, however, that if filename contained more than one dot (e.g., if it were alice.1.pcx—the expression would still produce the desired value alice.1.gif).

Regards,

Jonathan


___________________________________________________________________

Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html

GNU the choice of a complete generation.