English Text file modification

7 replies
Goto Page
To the start Previous 1 Next To the start
15.04.17 11:38:28 pm
Up
AtomKuh
User
Offline Off
I need a lua script that changes the content of a text file each 10 seconds. For instance, once 10 seconds are ran out, the previous empty text file should now contain the letter a. After the next 10 seconds the letter should be changed to b. Afterwards should the letter be changed back to a.

I tried to do it myself but I failed. I don't understand this I/O Model in lua.

I need this script to determine whether my server is still running or not. As a result I can restart automatically my server with another script.
16.04.17 12:26:24 am
Up
VADemon
User
Offline Off
Code:
1
2
3
4
5
6
7
function updateUptimeFile(path)
     local path = path or "sys/lua/uptime.txt"
     local file = io.open(path, "w")

     file:write(os.date("%H:%M:%S"))
     file:close()
end
Stranded 3 development     |     Looking for volunteers for CS2D-Archive. PM me.
16.04.17 12:26:45 am
Up
TheD
User
Offline Off
Not really tested, but this is something. This should cycle every 10 seconds and update the text document. There are other ways, but this is just a quick one to get you into thinking.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
local i = 0;
local temp = "a";
addhook("second", "test")
function test()
     i = i + 1;
     if a > 100 then a = 1; end
     if i%10 == 0 then
          local file = io.open("path/to/file", "w");
          if (temp == "a") then temp = "b";
          else temp = "a";
          end
          file:write(temp);
          file:close();
     end
end
http://cs2d-rpg.sytes.net/ - nvRPG -> Coming soon!
16.04.17 01:27:33 am
Up
AtomKuh
User
Offline Off
Thanks you too! By the way is there a method to create a delayed loop without using addhooks?
16.04.17 02:37:33 am
Up
_Yank
User
Offline Off
You can use the timer function and set it to repeat forever
16.04.17 07:53:54 am
Up
MikuAuahDark
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Use locals and upvalues, it's faster
local file = io.open("sys/lua/file.txt", "rb+")
local write_a = false

function updateTextFile()
     -- Start writing on beginning of file
     file:seek("set")
     -- Write either "a" or "b"
     file:write(write_a and "a" or "b")
     -- Apply the changes
     file:flush()
     -- Flip the switch to write either "a" or "b" in next write
     write_a = not(write_a)
end

-- Finally call timer, set count to 0 so it's called infinite times.
timer(10000, "updateTextFile", "", 0)
AuahDark strikes again for one more time in Pixel Art server.
16.04.17 04:18:58 pm
Up
VADemon
User
Offline Off
local file = io.open("sys/lua/file.txt", "rb+")

should be:
local file = io.open("sys/lua/file.txt", "wb+")
Stranded 3 development     |     Looking for volunteers for CS2D-Archive. PM me.
18.04.17 12:21:58 am
Up
AtomKuh
User
Offline Off
Thanks again for your answers.

I just have one more problem with the following bash script. It determines if the text file was changed or not within 30 seconds but I dont know how to add 2 more commands:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash

# do not put tabs before paths for second file through last file
files_to_check="/path/to/first/file
/path/to/second/file
/path/to/.../file"

delay=30

while true
do
    while read path
    do
        [ ! -e "$path.sha256" ] && sha256sum "$path" > "$path.sha256"
        status=$(sha256sum --quiet -c "$path.sha256"  > /dev/null 2>&1 && echo "No change!" || (echo "File changed!" && sha256sum "$path" > "$path.sha256"))
        [ "$status" == "No change!" ] && color="\e[0m" || color="\e[38;5;208m"
        printf "$color[%s] %s: %s\e[0m\n" $(date +%Y%m%d.%H%M%S) "$path" "$status"
    done <<< "$files_to_check"
    sleep $delay
    echo "-----------"
done


If it is not changing I want to add these two commands:
Code:
1
killall screen

Code:
1
screen -d -m /home/cs2d/cs2d_dedicated &


Help me this last time, please
To the start Previous 1 Next To the start