Use tr to transform a text string
April 20th, 2004
Problem:
In a shell script to back up a directory, you want to save the directory as a tar file whose name includes the full original path (just in case you have multiple directories, in separate locations, with the same name). So you want to save a directory with the path /dev/fs/D/mydir to a tar file called BACKUP_.dev.fs.D.mydir.
Solution:
Use tr to transform the path string. For example, the path to the directory you are backing up (/dev/fs/D/mydir) is stored in the variable $DIRECTORY. Use the following commands to create a variable called $NAME containing BACKUP_.dev.fs.D.mydir.
NAME = `echo $DIRECTORY | tr '/' '.'`
NAME = BACKUP_`echo $NAME | tr '\' '.'`
Note: don't mix up the backticks and the single quotes.
Potentially similar posts
- Scan for available IP addresses – August 2011
- My first (useful) Ruby program – June 2011
- Watch out for PHP shorthand – November 2010
- Perl basics for beginners (on Windows) – August 2010
- Gotchas with running a Perl script as a cron job – August 2010