This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bash function | |
# | |
# Usage: save foo.bar | |
# - creates a file like foo.bar.d41d8cd, | |
# - overwrites previous copy if identical, | |
# - preserves other previous versions with very high probability | |
# | |
save() { | |
file=$1 | |
if [[ -f "$file" ]]; then |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Guiding principle: a shell is a domain-specific language for handling | |
# files and processes. These core features shall be served by a dedicated | |
# and already familiar syntax. Traditional programming control structures | |
# on the other hand are hard to use and limited in traditional shell | |
# languages. For this, we shall use a proper programming language, one that | |
# checks statically for basic programming mistakes, allows the use of rich | |
# data structures, and integrates well in large applications. | |
# | |
# Language properties: | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
Sample command-line argument specified using Cmdliner's author's style. | |
*) | |
open Cmdliner | |
let seed_term = | |
let doc = " | |
Use the number $(docv) to initialize the global pseudo-random number | |
generator. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
Created by Martin Jambon and placed in the Public Domain on June 1, 2019. | |
Print a tree or a DAG as tree, similarly to the 'tree' command. | |
*) | |
open Printf | |
let rec iter f = function | |
| [] -> () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# | |
# Illustrate what happens when editing a script while it's running. | |
# | |
# This happens when saving the file in emacs, for example, but not with | |
# 'sed -i' because the latter deletes the old file before writing a new one. | |
# See https://unix.stackexchange.com/a/88572/46435 for an explanation. | |
# | |
set -eu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# | |
# Run parallel commands and fail if any of them fails. | |
# | |
set -eu | |
pids=() | |
for x in 1 2 3; do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
x=0 | |
y=0 | |
read x <<EOF | |
1 | |
EOF | |
echo 1 | read y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
x=1 | |
f() { | |
echo "$x" | |
} | |
g() { | |
local x=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
f() { | |
echo "$x" | |
} | |
main() { | |
local x=42 | |
f | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
Interactive approach | |
-------------------- | |
You can copy-paste code into `utop`, provided you load the lwt.unix | |
package: | |
#use "topfind";; | |
#thread;; (* only needed in the plain `ocaml` toplevel, not in `utop`. *) | |
#require "lwt.unix";; |