### Basic Chain Loading Example Source: https://github.com/skarnet/execline/blob/main/doc/grammar.html Illustrates a simple Unix program execution using chain loading, similar to how execline scripts operate. This example shows 'nice' modifying process priority before executing 'echo'. ```shell nice -10 echo blah ``` -------------------------------- ### Typical execline Script Start with elgetpositionals Source: https://github.com/skarnet/execline/blob/main/doc/elgetpositionals.html This snippet shows a common pattern for starting an argument-taking execline script. It first uses elgetopt to parse options, then elgetpositionals to set up positional parameters, followed by the main program execution. ```execline #!/command/execlineb elgetopt _optstring_ elgetpositionals _prog..._ ``` -------------------------------- ### Case with subexpression matching example Source: https://github.com/skarnet/execline/blob/main/doc/case.html This example demonstrates how to use the -N option with case to capture subexpressions from a matched value. The captured subexpressions are then available in the environment for subsequent commands. ```execline #!/bin/execlineb -S1 emptyenv case -N -- $1 { "([fo]+)bar(baz)" { /usr/bin/env } } ``` -------------------------------- ### Execline Quine - Initial Setup and Data Definition Source: https://github.com/skarnet/execline/blob/main/doc/quine-dam.txt This snippet shows the initial shebang, comments, and the definition of the 'lns' variable which holds the data for the quine. It also includes the first set of 'foreground printf' commands to print the header comments and the initial part of the code. ```execline #! /command/execlineb -P # Public Domain. # See comments below. # (Search for "HERE".) # define -sCd "\n" lns " ${p} ${bubble} is the end of the quine's data. ${p} They represent the following code, with various quotations: ${p} ${b} (backslash) is represented as ${d}${ob}b${cb} ${p} ${q} (double quote) is represented as ${d}${ob}q${cb} ${p} ${p} (sharp/pound/shibboleth/whatever) is represented as ${d}${ob}p${cb} ${p} ${ob} (open brace) is represented as ${d}${ob}ob${cb} ${p} ${cb} (closed brace) is represented as ${d}${ob}cb${cb} ${p} ${d} (dollar) is represented as ${d}${ob}d${cb} ${p} ${bubble} (the magic word) is represented as ${d}${ob}bubble${cb} ${p} (The point of the magic word is to allow the reader ${p} to conveniently skip over the large data section.) ${p} ${p} Now we have the quine's code! ${p} ${p} First, print the lines that come before the data. foreground ${ob} printf %s ${b}${p}${b}!${q} ${q} ${cb} foreground ${ob} printf %s${b}${b}n ${q}/command/execlineb -P${q} ${cb} foreground ${ob} printf %s${b}${b}n ${b}${p}${q} Public Domain.${q} ${cb} foreground ${ob} printf %s${b}${b}n ${b}${p}${q} See comments below.${q} ${cb} foreground ${ob} printf %s ${b}${p}${q} (Search for ${q} ${cb} foreground ${ob} printf %s${b}${b}n ${b}${q}${bubble}${b}${q}.) ${cb} foreground ${ob} printf %s${b}${b}n ${b}${p} ${cb} foreground ${ob} printf %s ${q}define -sCd ${b}${q}${b}${b}n${b}${q} lns ${b}${q}${q} ${cb} ${p} Next, print the data themselves, as data. foreground ${ob} forx -E lin ${ob} ${d}${ob}lns${cb} ${cb} multisubstitute ${ob} define b ${d}${ob}b${cb} define q ${d}${ob}q${cb} define p ${d}${ob}p${cb} define ob ${d}${ob}ob${cb} define cb ${d}${ob}cb${cb} define d ${d}${ob}d${cb} define bubble ${d}${ob}bubble${cb} define intron ${d}${ob}intron${cb} ${cb} printf ${b}${b}n%s ${d}${ob}lin${cb} ${cb} foreground ${ob} printf %s${b}${b}n ${b}${q} ${cb} ${p} Finally, use the data to print the code! forx -E lin ${ob} ${d}${ob}lns${cb} ${cb} multisubstitute ${ob} define b ${b}${b} define q ${b}${q} define p ${b}${p} define ob ${b}${ob} define cb ${b}${cb} define d ${d} define bubble ${bubble} define intron ${q}${intron}${q} ${cb} printf %s${b}${b}n ${d}${ob}lin${cb} ${p} That's all, folks! - Well, that wasn't so hard, was it? ${p} (This quine was written by - see ${p} ${p} for more information on quines and how to write them.) ``` -------------------------------- ### Shell matching example Source: https://github.com/skarnet/execline/blob/main/doc/case.html This example illustrates the use of the -s option for shell-style pattern matching. In this mode, regular expressions are treated as shell glob patterns, and subexpression matching is disabled. ```execline case -s -- $1 { "f*bar?az" { echo "Match found!" } } ``` -------------------------------- ### Parallel Substitution Example (Correct) Source: https://github.com/skarnet/execline/blob/main/doc/multisubstitute.html Illustrates how multisubstitute corrects the issue seen in serial substitution. By placing 'define B ${A}' and 'importas A A' within the multisubstitute block, both substitutions happen in parallel, yielding the desired output. ```execline #!/command/execlineb export A wrong multisubstitute { define B ${A} importas A A } echo ${B} ``` -------------------------------- ### execlineb Script with Block Source: https://github.com/skarnet/execline/blob/main/doc/el_semicolon.html An example of an execlineb script where 'echo 1' is executed from a block, followed by 'echo 2'. ```execlineb #!/command/execlineb foreground { echo 1 } echo 2 ``` -------------------------------- ### Execlineb -S_n_ option for positional parameter substitution Source: https://github.com/skarnet/execline/blob/main/doc/el_pushenv.html This example shows how to use the '-S_n_' option with execlineb for direct positional parameter substitution without modifying the environment stack. This is equivalent to using 'elgetpositionals -P_n_' followed by 'emptyenv -P'. ```execline #!/command/execlineb -S_n_ _foobar... ``` -------------------------------- ### Basic File Readability Test with eltest Source: https://github.com/skarnet/execline/blob/main/doc/eltest.html A common use case for eltest is to check if a file is readable. This example demonstrates a simple readability test. ```bash eltest -r ${file} ``` -------------------------------- ### Retrieving PID and Signal in trap directive Source: https://github.com/skarnet/execline/blob/main/doc/trap.html This example shows how to use 'importas' within a trap directive to capture the PID of the spawned program and the signal number that was trapped. This is useful for more complex signal handling logic. ```execline importas ! ! importas SIGNAL SIGNAL ``` -------------------------------- ### Serial Substitution Example (Incorrect) Source: https://github.com/skarnet/execline/blob/main/doc/multisubstitute.html Demonstrates a scenario where serial substitution leads to an incorrect result. The 'define B ${A}' occurs before 'importas A A', causing 'B' to be defined with the initial value of 'A' rather than the imported one. ```execline #!/command/execlineb export A wrong define B ${A} importas A A echo ${B} ``` -------------------------------- ### Quoted String Example Source: https://github.com/skarnet/execline/blob/main/doc/execlineb.html Demonstrates handling of double quotes and escaped characters within a quoted string in an execlineb script. ```execline "This is a \"quoted\" string." ``` -------------------------------- ### Secure Block Quoting with Substitution Source: https://github.com/skarnet/execline/blob/main/doc/el_semicolon.html Illustrates secure block quoting in an argv command involving substitution. This example prevents unwanted script modification by properly quoting all arguments, including those resulting from substitution. ```sh $ define FOO '' foreground ' echo' ' ${FOO}' ' rm' ' -rf' ' /' '' echo blah ``` -------------------------------- ### Insecure Block Quoting with Substitution Source: https://github.com/skarnet/execline/blob/main/doc/el_semicolon.html Demonstrates an insecure way to handle block quoting with substitution, which can lead to unintended and dangerous script execution. This example highlights the security risk of unquoted arguments within blocks. ```sh $ define FOO '' foreground echo '${FOO}' rm -rf / '' echo blah ``` -------------------------------- ### argv Representation of Blocks Source: https://github.com/skarnet/execline/blob/main/doc/el_semicolon.html Demonstrates how to use the 'foreground' command from the shell to sequence 'echo 1' and 'echo 2', showing the argv syntax for blocks using quoted arguments and empty words. ```sh $ foreground ' echo' ' 1' '' echo 2 ``` -------------------------------- ### Decoding Netstrings with Substitution Command Source: https://github.com/skarnet/execline/blob/main/doc/el_transform.html Demonstrates how to use the substitution command with an empty delimiter to interpret input as a sequence of netstrings. This is useful for safely splitting strings containing arbitrary characters. ```execline define -s -d "" A '1:a,2:bb,0:,7:xyz 123,1: ,' echo '$A' ``` -------------------------------- ### Basic Script Structure and Command Execution Source: https://github.com/skarnet/execline/blob/main/doc/componentsb.txt Demonstrates the basic structure of an execlineb script, including foreground execution, block grouping with braces, and simple command execution like 'sleep'. ```execlineb #!/command/execlineb -P # This execlineb script will sleep for 1 second, then print some # silly things on the standard output. foreground # an unquoted string, evaluated to: foreground { sleep 1 # Two unquoted strings, evaluated to " sleep" and " 1" # (without the quotation marks). } ``` -------------------------------- ### Execline Quine - Alternative Syntax (Comments) Source: https://github.com/skarnet/execline/blob/main/doc/quine-dam.txt This snippet presents an alternative, more readable syntax for the same Execline quine logic, using comments to explain the sections. It demonstrates the same core functionality of printing the header, data, and code. ```execline # HERE is the end of the quine's data. # They represent the following code, with various quotations: # \ (backslash) is represented as ${b} # " (double quote) is represented as ${q} # # (sharp/pound/shibboleth/whatever) is represented as ${p} # { (open brace) is represented as ${ob} # } (closed brace) is represented as ${cb} # $ (dollar) is represented as ${d} # HERE (the magic word) is represented as ${bubble} # (The point of the magic word is to allow the reader # to conveniently skip over the large data section.) # # Now we have the quine's code! # # First, print the lines that come before the data. foreground { printf %s #\! " " } foreground { printf %s\n "/command/execlineb -P" } foreground { printf %s\n #" Public Domain." } foreground { printf %s\n #" See comments below." } foreground { printf %s #" (Search for " } foreground { printf %s\n "HERE".) } foreground { printf %s\n # } foreground { printf %s "define -sCd \"\n\" lns \"" } # Next, print the data themselves, as data. foreground { forx -E lin { ${lns} } multisubstitute { define b ${b} define q ${q} define p ${p} define ob ${ob} define cb ${cb} define d ${d} define bubble ${bubble} define intron ${intron} } printf \n%s ${lin} } foreground { printf %s\n " # Finally, use the data to print the code! forx -E lin { ${lns} } multisubstitute { define b \ define q " define p # define ob { define cb } define d $ define bubble HERE define intron "NOTICE HOW THIS SENTENCE APPEARS ONLY ONCE IN THIS QUINE?" } printf %s\n ${lin} # That's all, folks! - Well, that wasn't so hard, was it? # (This quine was written by - see # # for more information on quines and how to write them.) ``` -------------------------------- ### Create a pipe using fdreserve Source: https://github.com/skarnet/execline/blob/main/doc/fdreserve.html This snippet demonstrates how to use fdreserve to reserve two file descriptors for creating a pipe. The reserved descriptors are then imported into variables and used with the piperw command. ```execline #!/command/execlineb fdreserve 2 multisubstitute { importas fdr FD0 importas fdw FD1 } piperw $fdr $fdw _prog... ``` -------------------------------- ### Execline script using emptyenv -P for clean environment Source: https://github.com/skarnet/execline/blob/main/doc/el_pushenv.html This script demonstrates how to run a program with a clean environment by first printing the script's arguments and then using 'emptyenv -P' to remove the current positional parameters before executing the program. ```execline #!/command/execlineb elgetpositionals foreground { echo $0 $@ } emptyenv -P prog $@ ``` -------------------------------- ### Basic Substitution with Escaping Source: https://github.com/skarnet/execline/blob/main/doc/el_substitute.html Illustrates basic variable substitution and how backslashes affect literal output. Shows substitution of defined variables and undefined variables. ```execline #!/command/execlineb define A val foreground { echo $A \\$A \\\\$A \\\\\\$A \\\\\\\\$A \\\\\\\\\\$A } echo $B \\$B \\\\$B \\\\\\$B \\\\\\\\$B \\\\\\\\\\$B ``` -------------------------------- ### execline Script for Basic Chain Loading Source: https://github.com/skarnet/execline/blob/main/doc/grammar.html A simple execline script that replicates the 'nice -10 echo blah' functionality. It uses execlineb to parse the script into an argv and execute the commands sequentially. ```execline #!/command/execlineb -P nice -10 echo blah ``` -------------------------------- ### Basic Execline Script with Macro Substitution Source: https://github.com/skarnet/execline/blob/main/doc/quine-prj-2.txt Demonstrates a simple execline script utilizing macro substitution to define and echo variables. This is useful for basic variable management and string manipulation within execline scripts. ```execline #!/command/execlineb define e "#!/command/execlineb define e ${q}${E}${q} multisubstitute { define q ${b}${q} define b ${b}${b} define E $e } echo $e" multisubstitute { define q " define b \ define E $e } echo $e ``` -------------------------------- ### Implementing IfElse with runblock Source: https://github.com/skarnet/execline/blob/main/doc/runblock.html This snippet shows how to implement the 'ifelse' command using execline's 'runblock' and 'ifte' commands. It demonstrates selective execution of code blocks based on conditions. ```execline #!/command/execlineb ifte { runblock 2 } { runblock -r 2 } runblock 1 ``` -------------------------------- ### Potential fdreserve descriptor reuse issue Source: https://github.com/skarnet/execline/blob/main/doc/fdreserve.html This example illustrates a scenario where calling fdreserve multiple times without allocating the reserved descriptors can lead to descriptor reuse, potentially causing issues. The second call to fdreserve might reuse a descriptor that was intended to be kept open. ```execline #!/command/execlineb fdreserve 3 multisubstitute { importas fdr FD0 importas fdw FD1 } piperw $fdr $fdw fdreserve 1 multisubstitute { importas oldfd FD2 importas newfd FD0 } _prog... ``` -------------------------------- ### Execute Script from File Source: https://github.com/skarnet/execline/blob/main/doc/execlineb.html Provide the script file path as an argument to execlineb to execute its content. ```bash #!/command/execlineb /path/to/your/script.txt ``` -------------------------------- ### Recursive Substitution with Multiple Keys Source: https://github.com/skarnet/execline/blob/main/doc/el_substitute.html Illustrates parallel substitutions of two different variables (A and B) within a single word. The output shows all combinations generated by the parallel substitutions. ```execline #!/command/execlineb multisubstitute { define -s A "a b c d" define -s B "1 2 3" } echo ${A}x${B} ``` -------------------------------- ### Basic multisubstitute Script Structure Source: https://github.com/skarnet/execline/blob/main/doc/multisubstitute.html This shows the general syntax for using multisubstitute within an execlineb script. It outlines the structure for defining substitution commands within a block before specifying the program to execute. ```execline multisubstitute { # Substitution commands go here # e.g., define, importas, elglob } _prog..._ ``` -------------------------------- ### Looping over positional parameters with netstrings Source: https://github.com/skarnet/execline/blob/main/doc/dollarat.html Use dollarat with forbacktickx to iterate over script arguments, handling spaces, newlines, and other special characters reliably. ```execline #!/command/execlineb forbacktickx -d "" ARG { dollarat -d "" } dosomething $ARG ``` -------------------------------- ### Recursive Substitution with Parallel Processing Source: https://github.com/skarnet/execline/blob/main/doc/el_substitute.html Shows how substitutions are performed recursively when multiple keys appear in a word and their values are split. Parallel substitutions are processed from left to right. ```execline #!/command/execlineb define -s B "1 2 3" echo ${B}x${B} ``` -------------------------------- ### Basic Execline Script Structure Source: https://github.com/skarnet/execline/blob/main/doc/quine-prj.txt This snippet demonstrates the fundamental structure of an Execline script, including the shebang line, variable definition using 'define', environment variable setting with 'env', and outputting variables with 'echo'. ```execline #!/command/execlineb define e "#!/command/execlineb define e $q${E}$q env e=$e define q ${b}${q} define b ${b}${b} importas E e echo $e" env e=$e define q " define b \ importas E e echo $e ``` -------------------------------- ### Quoted and Escaped Strings Source: https://github.com/skarnet/execline/blob/main/doc/componentsb.txt Illustrates how quoted strings and escaped spaces are handled, showing that they result in single words. Also covers how comments are treated within quoted strings. ```execlineb "echo" # this is a quoted string. It will evaluate to the word: echo foo\ bar\ zoinx # This is one word, since the spaces are escaped "foo bar zoinx" # This is exactly the same word, written another way " # this is not a comment, since it is inside a quoted string # This is not a comment either \" # nor is this " # but this is one ``` -------------------------------- ### Basic Variable Substitution in execline Source: https://github.com/skarnet/execline/blob/main/doc/el_substitute.html Demonstrates the basic substitution of a defined variable 'FOO' with the value 'blah' and its subsequent use in an echo command. This script will print 'blah' to standard output. ```execline #!/command/execlineb define FOO blah echo $FOO ``` -------------------------------- ### Execute Script from String Source: https://github.com/skarnet/execline/blob/main/doc/execlineb.html Use the -c option to execute a script provided as a string argument. ```bash #!/command/execlineb -c echo "Hello, World!" ``` -------------------------------- ### Execline Quine Program Source: https://github.com/skarnet/execline/blob/main/doc/quine-jriou.txt This is the complete Execline script that functions as a quine. It defines variables and uses nested commands to reconstruct and print its own source code. ```execline #!/command/execlineb define A "#!/command/execlineb" define B "fine G $ foreground { echo ${C} }\necho -n foreground ${D} define C ${E}${C}${R}foreground\n${D} echo ${G}${D}A${H} ${H}${R}foreground\n${D} echo define A ${G}${D}C${H}${G}${D}A${H}${G}${D}C${H} ${H}${R}echo\n-n define B ${G}${D}C${H} ${H}${R}foreground\n${D} echo -n ${G}${D}B${H} ${H}${R}foreground\n${D} multisubstitute ${D} define C ${E}${C} define D ${E}${D}${R}define\nE ${E}${E}${R}define\nH ${E}${H} define R ${C}${R}${C} ${H} de } echo ${B}" foreground { define C " foreground { echo ${A} } foreground { echo define A ${C}${A}${C} } echo -n define B ${C} } foreground { echo -n ${B} } foreground { multisubstitute { define C \" define D {\ndefine E \\ndefine H } define R \"\" } define G $ foreground { echo ${C} }\necho -n foreground ${D} define C ${E}${C}${R}foreground\n${D} echo ${G}${D}A${H} ${H}${R}foreground\n${D} echo define A ${G}${D}C${H}${G}${D}A${H}${G}${D}C${H} ${H}${R}echo\n-n define B ${G}${D}C${H} ${H}${R}foreground\n${D} echo -n ${G}${D}B${H} ${H}${R}foreground\n${D} multisubstitute ${D} define C ${E}${C} define D ${E}${D}${R}define\nE ${E}${E}${R}define\nH ${E}${H} define R ${C}${R}${C} ${H} de } echo ${B} ``` -------------------------------- ### Execline Script with Variable Definitions Source: https://github.com/skarnet/execline/blob/main/doc/quine-prj-3.txt A basic execline script demonstrating variable definitions and exports. It defines nested variables and exports one for use. ```execline #!/command/execlineb -P define e "#!/command/execlineb -P define e ${q}${E}${q} export E $e define q ${b}${q} define b ${b}${b} importas E E echo $e" export E $e define q " define b \ importas E E echo $e ``` -------------------------------- ### execline Script with Foreground Command Source: https://github.com/skarnet/execline/blob/main/doc/grammar.html Demonstrates the use of the 'foreground' builtin command in execline. It executes a 'sleep 1' command within a block and then proceeds to the 'echo blah' instruction. ```execline #!/command/execlineb -P foreground { sleep 1 } echo blah ``` -------------------------------- ### Simplified argv Block Representation Source: https://github.com/skarnet/execline/blob/main/doc/el_semicolon.html A simpler, but less secure, way to represent blocks in argv for the 'foreground' command. This version omits quoting for arguments within the block. ```sh $ foreground echo 1 '' echo 2 ``` -------------------------------- ### Substitution of Split Values Source: https://github.com/skarnet/execline/blob/main/doc/el_substitute.html Demonstrates how a split value for a variable is substituted by multiple words. The echo command receives multiple arguments based on the split value. ```execline #!/command/execlineb define -s FOO "v1 v2 v3" echo prefix-${FOO}-postfix ``` -------------------------------- ### Basic execline script with positional parameters Source: https://github.com/skarnet/execline/blob/main/doc/el_pushenv.html A basic execline script that retrieves and prints its positional parameters, then executes a program with those parameters. This version may pollute the program's environment. ```execline #!/command/execlineb elgetpositionals foreground { echo $0 $@ } prog $@ ```