Skip to content

Latest commit

 

History

History
174 lines (143 loc) · 3.17 KB

functions.md

File metadata and controls

174 lines (143 loc) · 3.17 KB

Bashew functions

Input/Output functions

IO:alert()

IO:alert "This file should normally not be empty"
# print alert (to stderr)

IO:announce()

IO:announce "Now starting conversion ..."
# print announcement and wait 1 second

IO:confirm()

IO:confirm "Delete intermediate file?"
# ask confirmation, default = N

IO:debug()

IO:debug "Saving to cache ./cache/file.113.txt"
# print debug text (only if -v/--verbose was specified)

IO:die()

IO:die "Input is not correctly formatted"
# print error message and stop script

IO:log()

IO:log "Operation ended after $SECONDS seconds"
# log text to default log file

IO:print()

IO:print "output: ./export/names.220315.csv"
# show text (except if -q/--quiet was specified)

IO:progress()

IO:progress "Now processing file #13 ..."
# show text but stay on same line (so next print statement overwrites it)

IO:question()

IO:question "What is your name?" "(unknown)"
# ask a question and return the answer
name=$(IO:question "What is your name?" "(unknown)")

IO:success()

IO:success "Some text"
# print text with a success checkmark

OS functions

Os:beep()

Os:beep 
# make a sound

Os:busy()

do_something_slow_in_background &
Os:busy $!  "Doing the slow thing"
# show an animated spinner while process is running

Os:folder()

Os:folder "/some/folder" 30
# prepare and cleanup a folder: create it if necessary and delete all files older than 30 days

Os:follow_link()

Os:follow_link "./do_this"
# return actual path of a file, following all symbolic links 

Os:notify()

Os:notify "Start downloading ..."
# or
Os:notify "Start compiling ..." "Library Compiler"
# send notification to desktop 

Os:require()

Os:require "ffmpeg"
Os:require "convert" "imagemagick"
Os:require "setver" "basher install pforret/setver"
# quit script when a required program is not installed yet

Os:tempfile()

Os:tempfile txt
# create filename for temp file that will be deleted when scripts ends

String functions

Str:ascii()

Str:ascii "Some text"
# remove accents and diacritics from text

Str:digest()

<<< "some text" Tool:digest <length:6>
< "some_file.txt" Tool:digest
# calculate hash/digest using MD5 and cut to N chars

Str:lower()

Str:lower "Some Text"
# convert text to lower case: "INTÉRNÀTIONAL" -> "intérnàtional"

Str:slugify()

Str:slugify "Some text"
# reduce text to an ASCII filename-ready text

Str:title()

Str:title "some text"
# convert to title case: "SOME TEXT" -> "Some Text"

Str:trim()

Str:trim "   some text    "
# trim space from beginning & end of text

Str:upper()

Str:upper "Some text"
< "some_text.txt" Str:upper
# convert to upper case: "intérnàtional" -> "INTÉRNÀTIONAL"

Tools

Tool:calc()

Tool:calc "( 10 / 7 ) * 25"
# calculate

Tool:time()

Tool:time
# return time as # seconds since 1 jan 1970 with microseconds (e.g.: 1651391010.385)