How does one change part of a file name in a shell script?

How does one change part of a file name in a shell script?

Post by Juha Lai » Sat, 16 Jan 1993 04:08:26




Quote:>So is there any way to take a file name in a shell script, chop off
>it's suffix and add a new one?

Try the 'basename' command. I think it's what you're looking for.

f.ex:
basename foo.bar .bar           produces
foo

..Wolf

 
 
 

How does one change part of a file name in a shell script?

Post by Richard Schi » Fri, 15 Jan 1993 17:24:57


I want a simple shell script that takes a file, runs it through TeX
then dvips and prints the resulting postscript file, i.e. the command
``tex-print myfile.tex'' should run the following commands:

tex myfile.tex
dvips myfile.dvi
lpr myfile.ps

The problem I have is that if I pass ``myfile.tex'' to a shell script
``tex-print'' as a command line variable then $1 = ``myfile.tex'' so a
simple script like

tex $1
dvips $1
lpr $1

won't work.  Also, the program that calles ``tex-print'' passes the
full file name ``myfile.tex'' and can't be configured to just pass
``myfile'' without the suffix.

So is there any way to take a file name in a shell script, chop off
it's suffix and add a new one?

Thanks for your help

Rich Schiek


 
 
 

How does one change part of a file name in a shell script?

Post by George Fa » Mon, 18 Jan 1993 01:15:28



>I want a simple shell script that takes a file, runs it through TeX
>then dvips and prints the resulting postscript file, i.e. the command
>``tex-print myfile.tex'' should run the following commands:
>tex myfile.tex
>dvips myfile.dvi
>lpr myfile.ps
>The problem I have is that if I pass ``myfile.tex'' to a shell script
>``tex-print'' as a command line variable then $1 = ``myfile.tex'' so a
>simple script like
>tex $1
>dvips $1
>lpr $1

With the input argument  myfile
You can try :   tex ${1}.tex
                dvips ${1}.dvi
                lpr ${1}.ps

>won't work.  Also, the program that calles ``tex-print'' passes the
>full file name ``myfile.tex'' and can't be configured to just pass
>``myfile'' without the suffix.
>So is there any way to take a file name in a shell script, chop off
>it's suffix and add a new one?
>Thanks for your help
>Rich Schiek


Cheers,
George.

---------------

Home Phone: (825)356-1084
    .------------------------------------------------.
\   | No one born alive can know everything ,        |                       /(
||=:| All you want might be got through your effort. |:====================||||
/   | Hard-working can help yet .                    |                       \(
    | Don't give it up , guy .                       |
    `------------------------------------------------'
---------------
--
---------------

Home Phone: (825)356-1084
    .------------------------------------------------.

 
 
 

How does one change part of a file name in a shell script?

Post by Graham Ru » Tue, 19 Jan 1993 03:17:56



>So is there any way to take a file name in a shell script, chop off
>it's suffix and add a new one?

You dont say what system you use but on SunOS 4.1.2 I use "basename".
(There is a Gnu version available as well.)

#!/bin/sh
PATH=/usr/bin:$PATH
ARG="myfile.tex"
BASE=`basename $ARG .tex`
#  BASE is now 'myfile'
DVI=$BASE.dvi
PS=$BASE.ps

echo $ARG $BASE $DVI $PS

Will output:

myfile.tex myfile myfile.dvi myfile.ps

Graham Rule
-------------------------------------------------------------------------------

Edinburgh University,     JCMB,    Phone:  +44 31 650 4989         O=Edinburgh;
Kings Buildings, Mayfield Road,    Fax:    +44 31 662 4712         P=uk.ac;
Edinburgh EH9 3JZ,    Scotland                                     A= ; C=GB

 
 
 

How does one change part of a file name in a shell script?

Post by Christopher Chan-N » Sat, 23 Jan 1993 11:24:41



: >So is there any way to take a file name in a shell script, chop off
: >it's suffix and add a new one?
: >
: Try the 'basename' command. I think it's what you're looking for.

: f.ex:
: basename foo.bar .bar         produces
: foo

Or if you have a shell that supports it (csh, tcsh, and my favorite zsh) you
could use Environment Variable Modifiers.  For example:

#!/bin/zsh
NAME=$1:r
tex ${NAME}.tex
dvips ${NAME}.dvi
lpr ${NAME}.ps
rm ${NAME}.ps

You'd probably want to do some error checking or something though (so it
doesn't continue when tex fails with millions of errors. :)

Hope this helps.

---
Christopher Chan-Nui    | The higher the "higher-ups" are who've come to see  

#include <disclaimer.h> | successful one.