blob: 53ee6e83a3894c71039a76528ad9129528b9d522 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
--[[
Copyright (c) 2024, Lance Borden
All rights reserved.
This software is licensed under the BSD 3-Clause License.
You may obtain a copy of the license at:
https://opensource.org/licenses/BSD-3-Clause
Redistribution and use in source and binary forms, with or without
modification, are permitted under the conditions stated in the BSD 3-Clause
License.
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
]]
-- This file is a demo of all of the commands Lunar Shell has in its Lua API
-- the Lua file itself is the executable unit so all code is run line by line
-- All Lua specific tools still work here too. Functions, loops, conditionals,
-- tables, Lua's standard library, etc are all available to integrate with Lunar Shell
print("Welcome to Lunar Shell scripting")
-- command line args can be passed to lua programs
-- done like so: example.lua arg1 arg2 ...
-- args can be read using the global args tables
if args ~= nil then
print("Printing args:")
for i = 1, #args do
print(args[i])
end
end
-- exec can be used to run any command line prompt
-- this method is much more native than using os.execute and is the recommended function
-- it also returns a boolean on if the command executed successfully
if lush.exec('echo "hello world"\n') then
print("echo worked properly")
end
-- debug mode can be used to log execution of commands
lush.debug(true) -- enters debug
lush.exec('echo "echo in debug mode"')
lush.debug(false) -- exits debug
-- getcwd returns the current working directory
local cwd = lush.getcwd()
print(cwd)
-- exec also supports piping
-- this comment will get grepped since it says hello
lush.cd("~/.lush/scripts")
lush.exec('cat "example.lua" | grep "hello" | sort | uniq')
lush.cd(cwd)
-- exists allows you to check if a file or directory exists
if lush.exists("~/.lush/scripts/example.lua") then
print("example.lua exists")
end
-- isFile and isDir check if a path points to a file or a directory
if lush.isFile("~/.lush/scripts/example.lua") then
print("example.lua is a file")
end
if not lush.isDirectory("~/.lush/scripts/example.lua") then
print("example.lua is not a directory")
end
-- you can also check if a file is readable/writeable
if lush.isReadable("~/.lush/scripts/example.lua") then
print("example.lua is readable")
end
if lush.isWriteable("~/.lush/scripts/example.lua") then
print("example.lua is writeable")
end
-- you can fetch the most recently executed command in history
print("Most recent history: " .. lush.lastHistory())
-- you can also fetch history at a certain index in the past (1 being most recent)
print("Most recent history indexed: " .. lush.getHistory(1))
|