[Ilugc] simple file I/O in lua

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Thu, 3 Jan 2013 05:26:55 +0530

$ cat fileio.lua
while 1 do
        local s=io.read()
        if s == nil then break end
        print(s)
end

You can run like this:

$ lua fileio.lua  </etc/passwd

I will explain.

The outer loop is a simple while(1) loop which we have seen in C.

The local keyword is just an access specifier. You can omit it.

Lua is a scripting language, so you don't have to declare variables
and you don't
 have to use the same type also.

s=io.read() reads the STDIN into the s buffer. You can manipulate the data in s
using the string library or the other facilities.

The if s== nil line is used to look for end of file(EOF) marker.

The print(s) line prints the lines with newline marker.

The io.write(s) which I used did not print newlines.

$ lua fileio.lua </etc/passwd > /tmp/pass

$ sha1 /etc/passwd /tmp/pass

Do you see?

-Girish

-- 
Gayatri Hitech
http://gayatri-hitech.com

Other related posts:

  • » [Ilugc] simple file I/O in lua - Girish Venkatachalam