JBoy
JBoy

Reputation: 5735

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?

Upvotes: 1

Views: 5361

Answers (5)

chepner
chepner

Reputation: 530882

[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

Upvotes: 1

JBoy
JBoy

Reputation: 5735

Solved with

index=$(cat /path/counter| tail -1)
tmp=$(expr $index + 1)
printf "%06d" $tmp > /path/counter

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246744

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

Upvotes: 2

cnicutar
cnicutar

Reputation: 182619

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

Upvotes: 3

mobius
mobius

Reputation: 5174

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}

Upvotes: 1

Related Questions