0

I would like to increment a variable if an execution failed.

I tested the logic by using a script like:

#!/bin/bash

if [ ! -f "/tmp/init" ]
then
    var=0
    echo $var
    export var
    touch /tmp/init
fi
if [ "$1" -eq 1 ]
then
    (( var++ ))
    export var
fi
echo $var

I obtain the result:

:~# ./script.sh 1                     
0
1
:~# ./script.sh 1
2
:~# ./script.sh 1
2

Is there an easy way to do this stuff?

3
  • 1
    You will have to store it in a temp file Commented Apr 13, 2015 at 13:35
  • It works by a tmp/file Commented Apr 13, 2015 at 13:57
  • But you're not storing your variable in the temp file. Add an echo $var > /tmp/init after your var++ line. $var disappears when you come out of your script. export makes it available to its children, not to its parent. Commented Apr 13, 2015 at 15:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.