76 lines
2.4 KiB
Plaintext
76 lines
2.4 KiB
Plaintext
#!/usr/bin/env luash
|
|
-- Quick Examples: Bite-sized demos showing luash vs bash
|
|
|
|
print("=== Luash vs Bash: Quick Examples ===\n")
|
|
|
|
print("1. Environment Variables")
|
|
print(" Bash: echo $USER")
|
|
print(" Luash: print($USER)")
|
|
print(" → " .. $USER)
|
|
|
|
print("\n2. Variable Assignment")
|
|
print(" Bash: MY_VAR=\"hello\"")
|
|
print(" Luash: $MY_VAR = \"hello\"")
|
|
$MY_VAR = "hello"
|
|
print(" → " .. $MY_VAR)
|
|
|
|
print("\n3. Command Substitution")
|
|
print(" Bash: current_dir=$(pwd)")
|
|
print(" Luash: current_dir = `pwd`")
|
|
current_dir = `pwd`
|
|
print(" → " .. current_dir)
|
|
|
|
print("\n4. Conditional Logic")
|
|
print(" Bash: if [ -f \"README.md\" ]; then echo \"exists\"; fi")
|
|
print(" Luash: if `test -f README.md` == \"\" then print(\"missing\") else print(\"exists\") end")
|
|
if `test -f README.md && echo "yes"` == "yes" then
|
|
print(" → exists")
|
|
else
|
|
print(" → missing")
|
|
end
|
|
|
|
print("\n5. Loops with Arrays")
|
|
print(" Bash: for file in *.lua; do echo $file; done")
|
|
print(" Luash: files = `ls *.lua`; for file in files:gmatch(\"[^\\n]+\") do print(file) end")
|
|
files = `ls *.luash`
|
|
print(" → Files found:")
|
|
for file in files:gmatch("[^\n]+") do
|
|
print(" " .. file)
|
|
end
|
|
|
|
print("\n6. String Processing")
|
|
print(" Bash: echo \"hello world\" | tr '[:lower:]' '[:upper:]'")
|
|
print(" Luash: string.upper(\"hello world\")")
|
|
print(" → " .. string.upper("hello world"))
|
|
|
|
print("\n7. Math Operations")
|
|
print(" Bash: result=$((5 + 3))")
|
|
print(" Luash: result = 5 + 3")
|
|
result = 5 + 3
|
|
print(" → " .. result)
|
|
|
|
print("\n8. File Operations")
|
|
print(" Bash: cat file.txt | wc -l")
|
|
print(" Luash: line_count = `cat luash | wc -l`")
|
|
line_count = `cat luash | wc -l`
|
|
print(" → " .. line_count .. " lines in luash script")
|
|
|
|
print("\n9. Error Handling")
|
|
print(" Bash: command || echo \"failed\"")
|
|
print(" Luash: if `command 2>/dev/null` == \"\" then print(\"failed\") end")
|
|
if `nonexistent_command 2>/dev/null` == "" then
|
|
print(" → command failed (as expected)")
|
|
end
|
|
|
|
print("\n10. Complex Data Structures")
|
|
print(" Bash: (limited array support)")
|
|
print(" Luash: Full Lua tables!")
|
|
data = {
|
|
users = {"alice", "bob", "charlie"},
|
|
scores = {alice = 95, bob = 87, charlie = 92}
|
|
}
|
|
print(" → Users: " .. table.concat(data.users, ", "))
|
|
print(" → Alice's score: " .. data.scores.alice)
|
|
|
|
print("\n✨ Luash combines the best of shell scripting with Lua's power!")
|