====== Other Languages on the Command Line ======
===== Ruby =====
sudo apt install ruby-full # system ruby, fine for scripts
ruby -v; irb # repl
gem install bundler --user-install
System gems need sudo (''sudo gem install x''): prefer ''gem install --user-install''
or rbenv if you need a newer ruby:
# rbenv route (only if system ruby is too old)
sudo apt install rbenv ruby-build
rbenv install -l | tail # list versions
rbenv install 3.3.0 && rbenv global 3.3.0
Ruby one-liners:
ruby -e 'puts File.readlines("f").grep(/error/).size' # count matches
ruby -pe 'sub(/foo/, "bar")' -i file.txt # sed-ish in-place
ruby -rjson -e 'j=JSON.parse(STDIN.read); puts j["key"]' < f.json
Script skeleton:
#!/usr/bin/env ruby
abort "usage: #{$0} FILE" if ARGV.empty?
File.readlines(ARGV[0], chomp: true).each { |l| puts l.upcase }
===== Node.js =====
sudo apt install nodejs npm # distro version (older but fine)
node -v; node script.js # run
node # repl
For a modern Node (nvm is the usual route):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
nvm install --lts
npm init -y # package.json
npm install # local dep
npx # run without installing
Node skeleton:
#!/usr/bin/env node
const fs = require("fs");
const [,, file] = process.argv;
if (!file) { console.error("usage: script FILE"); process.exit(1); }
console.log(fs.readFileSync(file, "utf8"));
===== Lua (outside Conky) =====
sudo apt install lua5.4
lua5.4 -i script.lua # run then repl
#!/usr/bin/env lua5.4
for line in io.lines(arg[1]) do print(line:upper()) end
===== Perl (always installed) =====
perl -pi -e 's/old/new/g' *.txt # in-place replace across files
perl -ne 'print if /error/' log.txt # grep
perl -lane 'print $F[2]' data.txt # awk-ish column extract
perl -MJSON::PP -0777 -ne 'print decode_json($_)->{key}' f.json
===== Choosing a Language =====
* **bash**: glue, system tasks, anything <100 lines
* **python**: default for anything with logic, parsing, APIs
* **ruby**: if you're fluent; good one-liners and text munging
* **node**: JS-heavy work, npx tooling, web-shaped data
* **lua**: conky config; rarely a general scripting pick here
* **perl**: superior one-liner when sed/awk get awkward
===== See Also =====
* [[terminal:scripting:start|Scripting Index]]
* [[terminal:tools:start|CLI Tools]]
* [[terminal:start|Return to terminal]]
* [[start|Return to wiki home]]