?
Solved

How do I add numbers in Unix Script?

Posted on 2004-09-03
10
Medium Priority
?
5,795 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 18

Accepted Solution

by:
liddler earned 500 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
Industry Leaders: 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!

 
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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. …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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