Solved

How do I add numbers in Unix Script?

Posted on 2004-09-03
10
5,776 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now