0

I was trying to write a shell script in unix environment and my requirement is if my condition is satisfied then it should note down how many times my condition is satisfied

for example in below code when the VAR value is 1 then it should note down how many times the value is 1 and keep recording the number in some specific file at certain path

VAR = /sys/ccc/sss

if [ "$VAR" -eq 1 ];

then /*need logic*/

fi

to be clear if ran above logic 100 times and my condition is true for 95 times then how to write a logic such that code should create a variable named condition_yes which has number 95 in it at end and varaible named condition_no which has number 5 in init Any idea or tip is appreciated

Requirement in nutshell My requirement is when i get a valid signal from external source a varaible in my sysfs located on my ecu at /sys/bus/drive/prd will be set to 1 and if not it will be set to 0 so my requirement is I continuosly send signal from an external source to my ecu for about 1 hr and at the end i want total count of how many times my signal is good and the varable above is set to 1

5
  • This question was so familiar to me that I had to search it. Indeed, it had been around before <askubuntu.com/questions/1212200/…>, apparently it was deleted and now reposted.
    – Quasímodo
    Commented Mar 2, 2020 at 22:34
  • yes it was me who deleted the previous question and readded as the old question is not that informative and clear. Sorry for any confusion
    – DEV
    Commented Mar 2, 2020 at 22:48
  • 1
    Instead of the formal code, please explain with words what you have and what you want to do with it. Otherwise we don't understand and we cannot help you.
    – sudodus
    Commented Mar 2, 2020 at 22:58
  • @DEV next time, just edit your question to be more clear, don't delete and recreate.
    – Thomas Ward
    Commented Mar 3, 2020 at 19:23
  • @sudodus added my requirement breifly to the question
    – DEV
    Commented Mar 3, 2020 at 22:45

1 Answer 1

0

If the value 1 or 0 is assigned to the file /sys/bus/drive/prd, you can read it with 'cat` and assign a variable to it.

var=$(cat /sys/bus/drive/prd)

and then count the zeros and ones

...
if [ "$var" == "1" ]
then
  good=$((good+1))
elif [ "$var" == "0" ]
  bad=$((bad+1))
else
  echo "unexpected value"
fi
...

You have not explained anything about the signal, so I don't write any code about that.

Instead I made a small demo shellscript, that may help you see how to make a while loop to count 'good' and 'bad' key-strokes and how to write the result when the measurement is finished.

#!/bin/bash

echo "siganl: as demo, let a key-stroke be the signal"
echo "value:  as demo, a digit key is good=1 other keys are bad=0"
echo "quit the loop with 'q'"
good=0
bad=0
re='^[0-9]+$'
while true
do
 read -sn1 ans
 if [[ "$ans" == "q" ]]
 then
  echo ""
  echo "good=$good"
  echo "bad=$bad"
  exit
 elif [[ "$ans" =~ $re ]]
 then
  echo -n '1'
  good=$((good+1))
 else
  echo -n '0'
  bad=$((bad+1))
 fi
done

Good luck with your own shellscript :-)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .