0

I am making a bash script where I need to run a command using a specific ID. But a single ID can be used only 10 times. After that the ID should change

For Example I have three IDs

ID=(abcd1
abcd2
abcd3)

echo $ID

Now when this script is executed 10 times then the value of ID should change to abcd2.. more after 10 times then abcd3

I searched on google but couldn't get anything like this

EDIT : I just thought of even getting the idea for using json in such case so I have created one more post here

2
  • I don't believe bash has a built-in to know how many times or when a variable is used. But you can create a function which "returns" the variable and every time you call that function a counter should be increased (the counter would be inside the function) Commented Jan 20, 2023 at 21:40
  • What happens if the script is executed 31 times? Start again with abcd1? Commented Jan 20, 2023 at 22:13

2 Answers 2

1

You can put counters in the script. Then have the script update them each time it's run.

#!/usr/bin/env bash

_list=(
  abc
  def
  ghi
)
_list_index=0 # autoupdate
_list_usage_counter=0 # autoupdate

max_uses=10
if [ $((_list_usage_counter)) -eq $((max_uses - 1)) ] ; then
  # reset counter
  sed -Ei \
    -e "s/^(_list_usage_counter)\=${_list_usage_counter}( # autoupdate)\$/\1=0\2/" \
    "$(readlink -f "$0")"
  _list_usage_counter=0

  # update list index
  sed -Ei \
    -e "s/^(_list_index)\=${_list_index}( # autoupdate)\$/\1=$((++_list_index))\2/" \
    "$(readlink -f "$0")"
fi

# make sure index is not out of bounds
if [ $_list_index -ge ${#_list[@]} ] ; then
  echo "no more items to use"
  exit
fi

# ... do stuff ...
echo "item: ${_list[_list_index]}"
echo "used: $((_list_usage_counter + 1))"

# update counter
sed -Ei \
  -e "s/^(_list_usage_counter)\=${_list_usage_counter}( # autoupdate)\$/\1=$((++_list_usage_counter))\2/" \
  "$(readlink -f "$0")"
4
  • what if using a json file & updating the counters over there ? Commented Jan 20, 2023 at 22:13
  • I've updated the script. Anything more than this, you'll need to figure out on your own. Commented Jan 20, 2023 at 22:21
  • You didn't mention json in your question. If using an external data source, use whatever program you normally use to read and update data. Commented Jan 20, 2023 at 22:21
  • You can try jq to process json. I haven't used it before, so can't help you with it. Commented Jan 20, 2023 at 22:38
0

You can store your "counters" to the temp files:

#!/bin/bash

ID=(abcd1 abcd2 abcd3)

#if script run for the first time create "file" counters
id_file="/tmp/$(basename ${0})_id"
us_file="/tmp/$(basename ${0})_usage"
if [[ ! -f $id_file ]]; then
   echo "0" > $id_file
fi

if [[ ! -f $us_file ]]; then
   echo "0" > $us_file
fi

#set variables
max_usage=10
id="$(cat $id_file)"
usage="$(cat $us_file)"

#if all ids used, exit script
if [[ $id -eq ${#ID[@]} ]]; then
   echo "No more ids available! Create new ones and delete '$id_file' and '$us_file' files."
   exit
fi

if [[ $usage -lt $max_usage ]]; then
   usage="$(( $(cat $us_file) + 1 ))"
   echo $usage > $us_file
   # do your stuff
   echo "Your id is: ${ID[$id]}."
else
   #reset usage and change (use next) id
   usage="1"
   echo "1" > $us_file
   id=$((id+1))
   echo "$id" > $id_file
   if [[ $id -lt ${#ID[@]} ]]; then
      echo "Your id x is: ${ID[$id]}."
   else
      echo "No more ids available! Create new ones and delete '$id_file' and '$us_file' files."
   fi
fi

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.