User Tools

Site Tools


notes:fall2024:projects:dsr0

This is an old revision of the document!


dsr0

powers of two base math

Base 2^1 is 2 2^2 is 4 2^3 is 8 and so on and so forth.

powers of two base conversions

checking time

By using “date +%r” in the command line you can get the current time which you can use later.

state file: keeping score across sessions

The best way to keep track of a users score across sessions is to use a file. For simplicity let's call it “info.txt” and assume we are doing this in BASH.

You can either create this file manually for every new user or have the program do it for you! Here is an example of the program doing it:

if [[ ! -f "info.txt" ]] ; then
    touch info.txt
fi

So that may be a lot at once to take in, so let's break it down. This line checks if the file exists, and when combined with an if statement it will do what is in the if statement if the file DOES NOT EXIST*.

    ! -f "info.txt"

This line will make the file

    touch info.txt

Too make our future selves happy lets set up where the data will be put in the file. Things we need:

  • An access time for the next session
  • Amount played
  • Amount right
  • Amount wrong

You can make the code do this by “echoing” into your text file. Here is an example of how to do that (tip: put this in your previous if statement):

    echo "Access Time:" >> info.txt
    echo "Amount Played:" >> info.txt
    echo "Amount Right:" >> info.txt
    echo "Amount Wrong:" >> info.txt

Again let's break that down. Here we are redirecting the output of “echo” to our file info.txt with the «.

    echo "Access Time:" >> info.txt

Inside your program you can use commands like grep, sed, cat, cut, tr, and head to name a few in order to write to and replace the text in info.txt. And don't forget that in BASH you can use | to pipe/connect commands together!

notes/fall2024/projects/dsr0.1726880505.txt.gz · Last modified: 2024/09/21 01:01 by cmazzara