mirror of
https://github.com/foo-dogsquared/wiki.git
synced 2025-02-07 15:19:04 +00:00
1 line
30 KiB
JSON
1 line
30 KiB
JSON
{"pageProps":{"metadata":{"date":"\"2021-05-09 16:40:50 +08:00\"","date_modified":"\"2022-04-16 20:18:16 +08:00\"","language":"en","source":""},"title":"Oil shell language","hast":{"type":"root","children":[{"type":"element","tagName":"nav","properties":{"className":"toc"},"children":[{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-1"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"what-is-oil-shell"},"children":[{"type":"text","value":"What is Oil shell?"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/lang.oil#what-is-oil-shell"},"children":[{"type":"text","value":"What is Oil shell?"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"overview-of-the-oil-language"},"children":[{"type":"text","value":"Overview of the Oil language"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/lang.oil#overview-of-the-oil-language"},"children":[{"type":"text","value":"Overview of the Oil language"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"strings"},"children":[{"type":"text","value":"Strings"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/lang.oil#strings"},"children":[{"type":"text","value":"Strings"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"arrays"},"children":[{"type":"text","value":"Arrays"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/lang.oil#arrays"},"children":[{"type":"text","value":"Arrays"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"conditions"},"children":[{"type":"text","value":"Conditions"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/lang.oil#conditions"},"children":[{"type":"text","value":"Conditions"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"associative-arrays"},"children":[{"type":"text","value":"Associative arrays"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/lang.oil#associative-arrays"},"children":[{"type":"text","value":"Associative arrays"}]}]}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"expression-and-command-mode"},"children":[{"type":"text","value":"Expression and command mode"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/lang.oil#expression-and-command-mode"},"children":[{"type":"text","value":"Expression and command mode"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"quoted-string-notation-qsn"},"children":[{"type":"text","value":"Quoted string notation (QSN)"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/lang.oil#quoted-string-notation-qsn"},"children":[{"type":"text","value":"Quoted string notation (QSN)"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"structured-data"},"children":[{"type":"text","value":"Structured data"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/lang.oil#structured-data"},"children":[{"type":"text","value":"Structured data"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"tips-and-tricks"},"children":[{"type":"text","value":"Tips and tricks"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/lang.oil#tips-and-tricks"},"children":[{"type":"text","value":"Tips and tricks"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For future references, this note mainly notes Oil v0.8.11 and later versions.\nAlso, we'll be comparing to Bash shell (src"},{"type":"element","tagName":"sub","properties":{},"children":[{"type":"text","value":"bash"}]},{"type":"text","value":"[:eval yes :results output]{bash --version | head -n 1} {{{results("},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"GNU bash\\, version 4.4.23(1)-release (x86_64-unknown-linux-gnu)"}]},{"type":"text","value":")}}}) as it is the most popular shell on the Unix world.\n"}]},{"type":"element","tagName":"h1","properties":{"id":"what-is-oil-shell"},"children":[{"type":"text","value":"What is Oil shell?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ripping off from the "},{"type":"element","tagName":"a","properties":{"href":"https://www.oilshell.org/blog/2020/01/simplest-explanation.html"},"children":[{"type":"text","value":"explanation page"}]},{"type":"text","value":":\n"}]},{"type":"element","tagName":"blockquote","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Oil is also aimed at people who know say Python or JavaScript, but purposely avoid shell.\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A modern shell attempting the replace "},{"type":"element","tagName":"a","properties":{"href":"id:dd9d3ffa-03ff-42a1-8c5d-55dc9fcc70fe"},"children":[{"type":"text","value":"GNU Bash"}]},{"type":"text","value":" slowly.\nThe project has an ambitious goal with a wide scope.\nIt is known for its "},{"type":"element","tagName":"a","properties":{"href":"https://oilshell.org/blog"},"children":[{"type":"text","value":"shell-oriented blog"}]},{"type":"text","value":" and the developer is very responsive and active with shell-related posts.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While there are multiple components in this project, we're focusing on two: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OSH and Oil shell"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OSH is the bridge between Bash and Oil."}]},{"type":"text","value":"\n It aims to be the most Bash-compatible shell that you can run most Bash scripts just fine.\n The point of this component is improving what's under the hood, imposing sensible defaults on them, and getting a wide reach since Bash is the most popular shell.\n While it may not run every Bash script in existence, the other point is to require minimal rewrites to run it with OSH or eventually rewrite it in Oil.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Oil shell is the modern replacement of Bash"}]},{"type":"text","value":" from its syntax and behavior.\n It aims to be the shell for people familiar to Python, Ruby, JavaScript, and the like.\n This is the other side of the bridge that OSH aims to reach.\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Both OSH and Oil are referring to the same interpreter but configured differently.\n"}]},{"type":"element","tagName":"h1","properties":{"id":"overview-of-the-oil-language"},"children":[{"type":"text","value":"Overview of the Oil language"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Oil is an entirely new programming language built from scratch.\nIt adds a hint of familiarity with Bash and takes a big queue of Python.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following block should be sufficient for jogging your memory and a quick glance of what Oil is all about.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"var title = \"Catenbury's Tale\"\nsetvar title = \"MS Fnd in a Lbry\"\n\nvar keywords = %(\"metadata\" \"information overload\" \"information retrieval\")\nappend :keywords \"information organization\"\n\nvar metadata = {}\nsetvar metadata['author'] = \"Hal Draper\"\nsetvar metadata['year'] = 1961\n\n# Practically, it's a function.\n# Not necessarily a function since it doesn't return an output.\nproc kebab_case(word) {\n write -- $word | sed -E -e 's/./\\L&/g' -e 's/s+/-/g' -e 's/[^.a-z0-9-]//g' -e 's/-+/-/g'\n}\n\nvar title_slug = $(kebab_case $title)\n\n{\n var keywords = %(\"alternate universes\" \"aliens\" \"spaceships\")\n for i in @keywords { echo \"keyword: $i\" }\n}\nfor i in @keywords { echo \"keyword: $i\" }\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"strings"},"children":[{"type":"text","value":"Strings"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While strings are similar to Bash strings"},{"type":"text","value":", there are subtle differences.\nMost notably, Bash splits the string when trying to do something.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"d='echo 3'\ne='echo \"The quick brown fox jumps over the lazy dog.\"'\n\nparallel -- $d $e\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It should throw an error because "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"parallel"}]},{"type":"text","value":" interprets it as if it has 4 arguments due to the splitting — i.e., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"parallel -- echo 3 echo \"The quick brown fox jumps over the lazy dog.\""}]},{"type":"text","value":".\nThe solution here is to quote the variables in evaluation (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"parallel -- \"$d\" \"$e\""}]},{"type":"text","value":").\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Compare that to Oil...\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"var d = 'echo 3'\nvar e = 'echo \"The quick brown fox jumps over the lazy dog.\"'\n\nparallel -- $d $e\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want splitting, you could use "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" Oil function — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"@split(array_var)"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"h2","properties":{"id":"arrays"},"children":[{"type":"text","value":"Arrays"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Arrays are mostly similar to Bash arrays except you have more options.\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can create a heterogenous list containing different types of data — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"var a = ['Dogs', 24, true ]"}]},{"type":"text","value":".\n Useful for JSON compatibility.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A homogenous array is useful for data consistency.\n It can accept a list of data of the same type — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"var b = %(\"foo\" \"bar\" \"baz\")"}]},{"type":"text","value":".\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can iterate through an array with a loop.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"for i in @a { echo \"word: $i\" }\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also add an item to the array with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" keyword.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"append :a \"biz\"\n\n# The expression mode equivalent\n# You can also append associative arrays due to the expressiveness of the mode\n_ a.append(\"biz\")\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"conditions"},"children":[{"type":"text","value":"Conditions"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Conditions in Oil look like this...\nNote that the parenthesis is a part of it.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"if (4 == 5) {\n echo \"Alright, this is true.\"\n} elif (1 > 5) {\n echo \"Another condition?\"\n} else {\n echo \"It seems you got me.\"\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also make ternary conditionals.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"echo $['dogs' if dogs == 'cute' else 'cats']\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While Oil expressions are usually used for conditions, you can also use command mode lines (see "},{"type":"element","tagName":"a","properties":{"href":"/Expression%20and%20command%20mode"},"children":[{"type":"text","value":"Expression and command mode"}]},{"type":"text","value":").\nSimilar to Bash conditions, it will pass when the exit code is zero.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"if test -d /usr/home {\n echo \"OK, you have an unusual home.\"\n} elif test -d /home {\n echo \"Alrighty, home does exist.\"\n} else {\n echo \"Homeless.\"\n}\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"associative-arrays"},"children":[{"type":"text","value":"Associative arrays"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"similar to Bash associative arrays or Python dictionaries (practically like Python dictionaries)\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"when interacting with Oil, an associative array is exactly that, an array\n"}]}]}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"const conditions = {}\nsetvar conditions['sunny'] = 80\nsetvar conditions['cloudy'] = 30\nsetvar conditions['rainy'] = -20\n\nfor key in @conditions {\n write -- $key $[conditions[key]]\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can check for a member in an associative with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" keyword.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"if ('rainy' in conditions) {\n write -- \"We have rainy days over here.\"\n}\n"}]}]},{"type":"element","tagName":"h1","properties":{"id":"expression-and-command-mode"},"children":[{"type":"text","value":"Expression and command mode"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are different ways "},{"type":"element","tagName":"a","properties":{"href":"https://www.oilshell.org/release/latest/doc/syntactic-concepts.html"},"children":[{"type":"text","value":"how Oil can create an expressive language with the shell"}]},{"type":"text","value":".\n It can parse different sublanguages with different lexer modes.\n But there are dominantly two modes to keep in mind: expression and command mode.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"simply put:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"command mode is similar to Bash expressions"}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"expression mode is akin to Python expressions"}]}]}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Expression mode even has Python familiarisms — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"if (\"item\" in array)"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"if (\"key\" in object)"}]},{"type":"text","value":".\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The addition of a Python-like expressiveness is how Oil can make a rich scripting exprience.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command mode is what you see most of the time\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"expression mode is activated when:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"right-hand side of "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"var a = 234"}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"_"}]},{"type":"text","value":" keyword where output will be ignored — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"_ a.append(b)"}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" command where it will print the results — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"= 53"}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"you can interpolate expression mode expressions with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"$[]"}]},{"type":"text","value":" — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"echo $[4 + 43 + a]"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"echo $[len(ARGV)]"}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"in "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statements — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"if (true) { echo \"WHOA\" }"}]}]}]}]}]}]},{"type":"element","tagName":"h1","properties":{"id":"quoted-string-notation-qsn"},"children":[{"type":"text","value":"Quoted string notation (QSN)"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"a data format that can represent any byte sequence\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"adapted from Rust's string syntax\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Oil can print non-ASCII stuff through "},{"type":"element","tagName":"a","properties":{"href":"https://www.oilshell.org/release/latest/doc/qsn.html"},"children":[{"type":"text","value":"QSN"}]}]}]}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"write -- $'\\u044f\\u043c\\u0443' $'\\u3bc'\n"}]}]},{"type":"element","tagName":"h1","properties":{"id":"structured-data"},"children":[{"type":"text","value":"Structured data"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While Bash have support for structured data such as arrays and associative arrays, it does not go any further such as not letting you assign arrays in an item.\nAs of v0.8.9, Oil also comes with the same problem but it seems "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/oilshell/oil/issues/741"},"children":[{"type":"text","value":"the developer is also interested in solving that"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Though, you can still declare and assign variables with nested data structures.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"const author = {\n \"name\": \"John Doe\",\n \"birthdate\": \"1992-04-04\",\n \"portfolio\": [\n { \"title\": \"Philistine: A Jon Doe story\", \"isbn\": \"392-423-2113-123\" },\n { \"title\": \"Whoa there!\", \"isbn\": \"241-123-35241-123\" }\n ]\n \"has_criminal_record\": false\n}\n\nwrite -- $[author['name']] $[author['portfolio'][0]['title']]\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Oil has a built-in JSON support with the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"json"}]},{"type":"text","value":" keyword.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-oil"]},"children":[{"type":"text","value":"const author = {\n \"name\": \"John Doe\",\n \"birthdate\": \"1992-04-04\",\n \"portfolio\": [\n { \"title\": \"Philistine: A Jon Doe story\", \"isbn\": \"392-423-2113-123\" },\n { \"title\": \"Whoa there!\", \"isbn\": \"241-123-35241-123\" }\n ]\n \"has_criminal_record\": false\n}\n\njson write :author\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is handy as most tools has an option to print JSON data — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"systemctl"}]},{"type":"text","value":", Ripgrep, "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"buku"}]},{"type":"text","value":", "},{"type":"element","tagName":"a","properties":{"href":"/cli.borg"},"children":[{"type":"text","value":"BorgBackup"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"h1","properties":{"id":"tips-and-tricks"},"children":[{"type":"text","value":"Tips and tricks"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Oil seems to evaluate in normal order, evaluating only when the conditions passed — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"echo $['' + null if null else 'EEEEHHH']"}]},{"type":"text","value":" should print "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"EEEEHHH"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Just like most modern mainstream languages... nice.\n"}]}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two operands of different types are considered unequal — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"'4' == 4"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use Python-like type conversions like "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Int"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Bool"}]},{"type":"text","value":", and "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Str"}]},{"type":"text","value":" — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"Int('4') == 4"}]},{"type":"text","value":".\n"}]}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"By default, errexit is disabled (e.g., the script will not exit on error).\n You can have fine control over it with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"run"}]},{"type":"text","value":".\n"}]}]}]}]},"backlinks":[]},"__N_SSG":true} |