Creating a counter for a shell script

I need to create a counter to use in a shell (bash) script, each time the script is called the number contained has to be increased by one and the number should be kept as six digit number, so the initial value would be 000000, then 000001, then 000002 and so on..... What i'm doing is, i create a file named 'counter' which contains a 6 digits integer on the first line. so from the script i have this code:

index= cat /path/counter | tail -1   #get the counter
tmp=`expr $index + 1`                #clone and increase the counter
echo "" >/path/counter               #empty the counter
echo ${tmp} >/path/counter           #insert clone

The problem is that its not working at the second step, probably is the first step which is actually failing, do you have an advice?

An option would be the following :

#!/bin/bash

read index < /path/counter
declare -i tmp=index+1
printf "%06d" $tmp > /path/counter

The problem is that it raises the content of the file only until 000007, after that i get:

-bash: 000008: value too great for base (error token is "000008")

Any Advice?

Answers


[UPDATE: fixed to include an explicit base marker in the text file, but Glenn Jackman beat me to it.]

You can simplify this a bit:

read index < /path/counter   # Read the first line using bash's builtin read
declare -i tmp=index+1       # Set the integer attribute on tmp to simplify the math
printf "10#%06d" $tmp > /path/counter    # No need to explicitly empty the counter; > overwrites

Or, you don't even need a temporary variable to hold the incremented value:

read index < /path/counter
printf "10#%06d" $(( index+1 )) > /path/counter

You're not reading the index right. Try:

index=$(tail -1 /path/counter)

Other things to note:

  • You don't need the cat, tail can handle things on its own
  • You could replace the backticks with tmp=$(expr ...)
  • You don't need the echo "", the > redirection truncates the file
EDIT

To make the number 6 digits wide try printf instead of echo:

printf "%06d", $index

bash has a mechanism where you can specify a number's base

#!/bin/bash
file=/path/to/counter
[[ ! -f "$file" ]] && echo 0 > $file              # create if not exist
index=$(< "$file")                                # read the contents
printf "%06d\n" "$((10#$index + 1))" > "$file"    # increment and write

This would work also:

#!/bin/bash

if [ -e /tmp/counter ]
  then
    . /tmp/counter
  fi

if [ -z "${COUNTER}" ]
  then
    COUNTER=1
  else
    COUNTER=$((COUNTER+1))
  fi

echo "COUNTER=${COUNTER}" > /tmp/counter

echo ${COUNTER}

Need Your Help

Is there a CASPOL.exe GUI command line generator?

.net code-access-security caspol

Is there a simple utility/GUI application for generating CASPOL commands?

ASP.NET SessionState TimeOut not working

c# asp.net session-state session-timeout

I want my web page to close when SessionState timeout occures. This is my code in my web config:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.