• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5805
  • Last Modified:

How do I add numbers in Unix Script?


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
bganoush
Asked:
bganoush
  • 4
  • 2
  • 2
  • +2
1 Solution
 
liddlerCommented:
cnt=`expr $cnt +1`
(back inverted comma ` not forward ')
0
 
avizitCommented:
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
 
avizitCommented:
and oh btw try changing

cnt=$cnt+1

to

cnt=`expr $cnt + 1`


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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
avizitCommented:
heh liddler has already answered :)
sorry
0
 
bganoushAuthor Commented:


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
 
avizitCommented:
umm in that case you might be wrong

 -e checks if the file exists

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

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
 
liddlerCommented:
$# is the number of arguments passed to the script, so if none it exits
0
 
TintinCommented:
= 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
 
abhishek_2707Commented:
use "expr" command followed by variables addressed using $ sign, and operator seperated by commas....everything enclosed in BACKTICKS like ( ` ).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now