Fs.lua: Difference between revisions

From Alpine Linux
(Category:Lua)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
=== is_dir ===
Filesystem Specific functions
 
 
== is_dir ==
'''INPUT:'''<BR>
'''INPUT:'''<BR>
This library function required the following inputs/parameters.
This library function required the following inputs/parameters.
Line 16: Line 19:
  directory
  directory
In directory is missing 'liboutput' would contain '''''nil''''' (nothing).
In directory is missing 'liboutput' would contain '''''nil''''' (nothing).
=== is_file ===
== is_file ==
'''INPUT:'''<BR>
'''INPUT:'''<BR>
This library function required the following inputs/parameters.
This library function required the following inputs/parameters.
Line 35: Line 38:




=== is_link ===
== is_link ==
'''INPUT:'''<BR>
'''INPUT:'''<BR>
This library function required the following inputs/parameters.
This library function required the following inputs/parameters.
Line 53: Line 56:
In file is missing 'liboutput' would contain '''''nil''''' (nothing).
In file is missing 'liboutput' would contain '''''nil''''' (nothing).


=== read_file / read_file_as_array ===
== read_file / read_file_as_array ==
'''INPUT:'''<BR>
'''INPUT:'''<BR>
This library function required the following inputs/parameters.
This library function required the following inputs/parameters.
Line 76: Line 79:
  1 Nov  6 13:17:01 inspiron /USR/SBIN/CRON[21751]: (root) CMD (  cd / && run-parts --report /etc/cron.hourly)
  1 Nov  6 13:17:01 inspiron /USR/SBIN/CRON[21751]: (root) CMD (  cd / && run-parts --report /etc/cron.hourly)
  2 Nov  6 13:43:31 inspiron -- MARK --
  2 Nov  6 13:43:31 inspiron -- MARK --
===dostounix ===
'''INPUT:'''<BR>
This library function required the following inputs/parameters.
* file contents
** Example: c = file:read("*a")
* String with EOL
'''OUTPUT:'''<BR>
This library function deliverers the following output/parameters.
* Output is returned minus the \r characters
'''CODING EXAMPLE:'''
-- Include/Call for this library
require("fs")
-- file test.txt is from dos/windows
require "fs"
f = io.open("test.txt")
c = f:read("*a")
file = fs.dostounix(c)
--file now contains the contents of test.txt without the \r char
===remove_blanks_comments ===
'''INPUT:'''<BR>
This library function required the following inputs/parameters.
* Path
** Example: /root/mynotes
'''OUTPUT:'''<BR>
This library function deliverers the following output/parameters.
* Returns a table to iterate over minus the blank and commented lines
'''CODING EXAMPLE:'''
-- Include/Call for this library
require("fs")
t = fs.remove_blanks_comments("/etc/shorewall/rules")
-- t is a table with only pertinent lines


===search_replace ===
== write_file ==
'''INPUT:'''<BR>
This library function required the following inputs/parameters.
* Path to file
** Example: /root/mynotes
* Search
** What to find
*Replace
**What to replace the found items with
'''OUTPUT:'''<BR>
This library function deliverers the following output/parameters.
* Table
'''CODING EXAMPLE:'''
-- Include/Call for this library
require("fs")
t = fs.search_replace("/etc/shorewall/interfaces", "eth0", "eth1")
--t will be a table that contains /etc/shorewall/interfaces with eth0 replaced as eth1
 
===ipairs_string ===
'''INPUT:'''<BR>
This library function required the following inputs/parameters.
* Table
'''OUTPUT:'''<BR>
This library function deliverers the following output/parameters.
* String
'''CODING EXAMPLE:'''
-- Include/Call for this library
require("fs")
t = { "eth0", "eth1", "lo"}
fs.ipairs_string(t)
--will give you a table that can be writing one per line for a file.
 
=== write_file ===
'''INPUT:'''<BR>
'''INPUT:'''<BR>
This library function required the following inputs/parameters.
This library function required the following inputs/parameters.
Line 159: Line 98:
  fs.write_file("/var/log/messages", "Hello World!")
  fs.write_file("/var/log/messages", "Hello World!")


=== find ===
== find ==
Lets you search for dir entries matching filespec.<BR>
Lets you search for dir entries matching filespec.<BR>
<span style="color:red">Could be compared to 'find / -type d | grep tmp' '''''(not sure)'''''</span><BR>
<span style="color:red">Could be compared to 'find / -type d | grep tmp' '''''(not sure)'''''</span><BR>
Line 184: Line 123:
  2 /usr/tmp/apk_add
  2 /usr/tmp/apk_add


 
[[Category:Lua]]
=== table_to_string ===
'''INPUT:'''<BR>
This library required the following inputs/parameters.
* Delimiter
** Could be one or more chars.
* Array
** Data which is to be joined into a string.
'''OUTPUT:'''<BR>
This library deliverers the following output/parameters.
* String
** Could be something like "Word, Word, Word, Word"
'''CODING EXAMPLE:'''
-- Set variable/Call for this library
require "fs"
-- Create a array of data <span style="color:red">(Not sure if the next row is correct)</span>
arraytojoin = { "Bird", "Fish", "Cow", "Hammer" }
-- Process the data (note the delimiter)
liboutput = fs.table_to_string(";", arraytojoin)
Bird;Fish;Cow;Hammer
 
===string_to_table ===
'''INPUT:'''<BR>
This library required the following inputs/parameters.
* Delimiter
** Could be one or more chars.
* String or line from a file: bobo~foo~bar~baz~1
** Data is to be split into a table
'''OUTPUT:'''<BR>
This library deliverers the following output/parameters.
* Table
** { 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1}
'''CODING EXAMPLE:'''
-- Set variable/Call for this library
require "fs"
-- Grab the line from a file or input it into the funtcion
line = "bobo~foo~bar~baz~1"
-- Process the data (note the delimiter)
t = fs.string_to_table("~", line)
't' would contain:
{ 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1}
for a,b in ipairs(t) do print(a,b) end
1 bobo
2 foo
3 bar
4 baz
5 1

Latest revision as of 08:03, 12 March 2012

Filesystem Specific functions


is_dir

INPUT:
This library function required the following inputs/parameters.

  • Path to directory
    • Example: /var/log/snort

OUTPUT:
This library function deliverers the following output/parameters.

  • nil or "directory"
    • If inputstring is a existing directory, the output is "directory" (not nil)

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
-- Check if url is a folder
liboutput = fs.is_dir("/var/log/snort")

If directory exist 'liboutput' would contain string:

directory

In directory is missing 'liboutput' would contain nil (nothing).

is_file

INPUT:
This library function required the following inputs/parameters.

  • Path to file
    • Example: /var/log/messages

OUTPUT:
This library function deliverers the following output/parameters.

  • nil or "file"
    • If inputstring is a existing file, the output is "file" (not nil)

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
-- Check if url is a file
liboutput = fs.is_file("/var/log/messages")

If file exist 'liboutput' would contain string:

file

In file is missing 'liboutput' would contain nil (nothing).


is_link

INPUT:
This library function required the following inputs/parameters.

  • Path to link
    • Example: /etc/rcL.d/S50sshd

OUTPUT:
This library function deliverers the following output/parameters.

  • nil or "file"
    • If inputstring is a existing file, the output is "file" (not nil)

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
-- Check if url is a file
liboutput = fs.is_link("/etc/rcL.d/S50sshd")

If file exist 'liboutput' would contain string:

file

In file is missing 'liboutput' would contain nil (nothing).

read_file / read_file_as_array

INPUT:
This library function required the following inputs/parameters.

  • Path to file
    • Example: /var/log/messages

OUTPUT:
This library function deliverers the following output/parameters.

  • Filecontent
    • The filecontent is presented as a string (output comes as a table if 'read_file_as_array' is used)

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
-- Get filecontent
liboutput = fs.read_file("/var/log/messages")

'liboutput' could contain something like:

Nov  6 13:17:01 inspiron /USR/SBIN/CRON[21751]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Nov  6 13:43:31 inspiron -- MARK --

CODING EXAMPLE (read_file_as_array):
If 'read_file_as_array' is used, you could do something like this see the output:

for a,b in ipairs(liboutput) do print(a,b) end

The output would look something like:

1 Nov  6 13:17:01 inspiron /USR/SBIN/CRON[21751]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
2 Nov  6 13:43:31 inspiron -- MARK --

write_file

INPUT:
This library function required the following inputs/parameters.

  • Path to file
    • Example: /root/mynotes
  • String
    • Content that should be written to file

OUTPUT:
This library function deliverers the following output/parameters.

  • Output is written to file
    • The content of the inputstring is written to the file.
    • Data is not appended to file

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
-- Write to file
fs.write_file("/var/log/messages", "Hello World!")

find

Lets you search for dir entries matching filespec.
Could be compared to 'find / -type d | grep tmp' (not sure)
INPUT:
This library function required the following inputs/parameters.

  • SearchString
    • This string is what we are searching for in the dirnames.
  • Path
    • This path is where we start searching
    • Search is recursive (not sure)

OUTPUT:
This library function deliverers the following output/parameters.

  • SearchResult
    • The SearchResult as a table (array)

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
-- Search for dir
liboutput = fs.find("tmp", "/") 

You could do something like this see the output:

for a,b in ipairs(liboutput) do print(a,b) end

The output would look something like:

1 /usr/tmp
2 /usr/tmp/apk_add