Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 6 Jun 1998 20:27:18 -0400 (EDT)
From:      Adrian Filipi-Martin <adrian@nvl.virginia.edu>
To:        Slyce <slyce@onramp.net>
Cc:        FreeBSD Questions <questions@FreeBSD.ORG>
Subject:   Re: BASH script help...
Message-ID:  <Pine.HPP.3.96.980606202056.1323A-100000@huron.nvl.virginia.edu>
In-Reply-To: <3579411C.25FB6F54@onramp.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, 6 Jun 1998, Slyce wrote:

> I'm creating a script to backup some files under dos in a tarball.... I
> want to be able to save the last five backups.... I'm saving them in
> dos_backup_060198_073456_1.tar.gz format.... How can I strip the tar.gz
> and increment the 1 to 2....
> For instance, I have dos_backup_060198_073456_1.tar.gz and
> dos_backup_060298_073456_2.tar.gz and just created a new backup file,
> but need to rename the previous two by incrementing the last number by
> one, so the old dos_backup_060198_073456_1.tar.gz becomes
> dos_backup_060198_073456_2.tar.gz, the old
> dos_backup_060298_073456_3.tar.gz becomes
> dos_backup_060298_073456_3.tar.gz and the new one is
> dos_backup_060398_073456_1.tar.gz....

	Here's an example of how to split up a string in bash and
reconstruct a filename with the new version number.  See the "parameter
expansion" section of the bash man page for details on the ${foo%bar} and
${foo#bar} expansion semantics.  'let' makes it clear that you are
defining an arithmetic variable, but normally you use $((...)) to do
inline arithmetic.

	: adrian@huron; File=test_1.tar.gz  
	: adrian@huron; echo ${File}
	test_1.tar.gz
	: adrian@huron; Base=${File%%.*}
	: adrian@huron; echo ${Base}
	test_1
	: adrian@huron; let NewVersion=${Base##*_}+1
	: adrian@huron; echo ${NewVersion}
	2
	: adrian@huron; NewFile=${Base%_*}_${NewVersion}.tar.gz
	: adrian@huron; echo ${NewFile}
	test_2.tar.gz
	: adrian@huron; 

	N.B. ": adrian@huron;" is my prompt.  It's a NOP, ':' is the null
command and ';' is the command separator, so everything between are
arguments.  It's means you can paste an entire command line, including the
prompt, into a terminal window.  It makes selecting and pasting commands
simipler IMHO, i.e. just tripple click and then middle click.

	Adrian
--
adrian@virginia.edu        ---->>>>| If I were stranded on a desert island, and
System Administrator         --->>>| I could only have one OS for my computer,
Neurosurgical Visualization Lab ->>| it would be FreeBSD.  Think about it.....
http://www.nvl.virginia.edu/     ->|      http://www.freebsd.org/


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.HPP.3.96.980606202056.1323A-100000>