3 ms·
Shell functions can also accomplish the same purpose as tiny little scripts. For example, a 2 line shell script can be wrapped in a function instead: mymo
by chubot 6y ago
Shell functions can also accomplish the same purpose as tiny little scripts. For example, a 2 line shell script can be wrapped in a function instead:
mymove() {
cp --verbose $1 $2
rm --verbose $1
}
Shell has good abstraction capacities! It even has some that other languages don't have:
Shell Has a Forth-like Quality http://www.oilshell.org/blog/2017/01/13.html http://www.oilshell.org/blog/2017/01/13.html
Pipelines Support Vectorized, Point-Free, and Imperative Style http://www.oilshell.org/blog/2017/01/15.html http://www.oilshell.org/blog/2017/01/15.html
http://www.oilshell.org/blog/tags.html?tag=shell-the-good-parts#shell-the-good-parts http://www.oilshell.org/blog/tags.html?tag=shell-the-good-pa...
- jcynix 6y agoYes, shell functions are cool. I use them to augment standard commands too, e.g. make head(1) or tail(1) output more lines than usual, depending on the terminal's number of lines: head () { if [[ $# -eq 0 ]] then /usr/bin/head -$[(LINES-1)/2] elif [[ -f "$1" ]] then case $# in (1) /usr/bin/head -$[(LINES-1)/2] $* ;; (2) /usr/bin/head -$[LINES*5/12] $* ;; (3) /usr/bin/head -$[(LINES-1)/3] $* ;; (*) /usr/bin/head $* ;; esac else /usr/bin/head $* fi }
- brendyn 6y agowith that, I think if the cp fails, the rm will still be deleted. maybe 'set -euo pipefail' will fix it. Do the $1 and $2 need to be quoted too incase there are special characters?
- chubot 6y agoYes all that is true -- it was just a quick demonstration. By the way: - Oil has set -euo pipefail on by default. (And also nullglob) - Oil doesn't split words by default, so you don't need the quotes: https://www.oilshell.org/release/0.8.0/doc/simple-word-eval.html https://www.oilshell.org/release/0.8.0/doc/simple-word-eval.... Those are all "opt in" -- OSH still runs POSIX shell scripts, if you want all the extra ceremony :)