Solved

How do I add numbers in Unix Script?

Posted on 2004-09-03
10
5,782 Views
Last Modified: 2013-12-26

I have the following script that is supposed to copy the given file to a timestamped file using the date only in the filename. If a second copy is done on the same day, then it appends "_1", "_2", "_3" to the end of the timestamp...

The problem is that I don't know how to "add' numbers in Unix script so my filename ends up as "filename.20040903_1" and then "filename.20040903_1+1" , "filename.20040903_1+1+1"  instead of "filename.20040903_1" and then "filename.20040903_2"...

if [ $# = 0 ]; then
       exit
fi
filename="$1.`date +'%Y%m%d'`"
echo File: $filename
cnt=1
while [ -a $filename ]
do
        filename="$1.`date +'%Y%m%d'`_$cnt"
        cnt=$cnt+1
        echo File: $filename
done;
cp -p $1 "$filename"

-- Bubba
-- PS: is anything else wrong with the script?
0
Comment
Question by:bganoush
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 18

Accepted Solution

by:
liddler earned 125 total points
ID: 11973857
cnt=`expr $cnt +1`
(back inverted comma ` not forward ')
0
 
LVL 11

Expert Comment

by:avizit
ID: 11973963
okay sorry i dont have the solution but guess I might learn something from you

what does "-a" test for ?  I haven't found that check .

I know for "expr1 –a expr2 = True if both expr1 and expr2 are true."

but just

while [ -a $filename ]  ??

0
 
LVL 11

Expert Comment

by:avizit
ID: 11974020
and oh btw try changing

cnt=$cnt+1

to

cnt=`expr $cnt + 1`


note that its backquotes `   and there is space around the '+'

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 11

Expert Comment

by:avizit
ID: 11974034
heh liddler has already answered :)
sorry
0
 

Author Comment

by:bganoush
ID: 11974725


Um... The -a I think checks if the file exists...

Thanks liddler for the quicker response... :)

BTW: is anything wrong with this part:

if [ $# = 0 ]; then
       exit
fi

-- Bubba
0
 
LVL 11

Expert Comment

by:avizit
ID: 11974754
umm in that case you might be wrong

 -e checks if the file exists

http://www.geocities.com/tipsforlinux/articles2/043.html
0
 

Author Comment

by:bganoush
ID: 11974808

ummm no..

-e checks for two things, one that the file exists and two that you have read access to it....

Your article is outdated or erroneous.

-- Bubba
0
 
LVL 18

Expert Comment

by:liddler
ID: 11977978
$# is the number of arguments passed to the script, so if none it exits
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11986211
= is for non-numeric comparisons, although it will still work in this case.

It would be more correct to do

[ $# -eq 0 ] && exit 1

Note that I've done an exit 1 as you generally want a non-zero exit code if the script failed or has some error condition like incorrect number of parameters.
Also note, you should echo an appropriate usage message if the number of parameters is incorrect.

Assuming you have ksh/bash on your system, the following shell script will do want you want without the inefficient loop and calls to the date command.

#!/bin/ksh
if [ $# -ne 1 ]
then
   echo "USAGE: `basename $0` [filename]" >&2
   exit 1
fi

date=`date +%Y%m%d`
file=$1

if [ ! -f $file ]
then
   echo "$file does not exist" >&2
   exit 1
fi

let count=1

exists=`ls -1 $file.$date* 2>/dev/null | wc -l`
let count+=exists

cp -p $file $file.${date}_$count


If you want a pure bourne shell solution, change

let count=1

to

count=1

and

let count+=exists

to

count=`expr $count + $exists`
0
 

Expert Comment

by:abhishek_2707
ID: 12154221
use "expr" command followed by variables addressed using $ sign, and operator seperated by commas....everything enclosed in BACKTICKS like ( ` ).
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
move a line in eclipse 3 103
twoTwo  challenge 35 103
unix example issues 18 90
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question