Search results for: “bash snippet”

  • Bash Snippet: Luhn Algorithm

    Bash Snippet: Luhn Algorithm

    I like Bash, but it isn’t well suited for some tasks. For fun I sometimes ignore that. Occasionally people seem to find this useful. In that spirit, here is my implementation of the popular Luhn / mod10 algorithm used in credit card, IMEI, and other number sequences. download [luhn.zip] # Returns Luhn checksum for supplied…

  • Bash Snippet: Decimal to Binary Conversion

    Bash Snippet: Decimal to Binary Conversion

    Convert a decimal value into the binary representation and vice versa in Bash using only built-ins. If you know a better way, please let me know. To ensure a properly formatted expression for the arithmetic expansion in bin2dec, the dec2bin function prefixes zeros as needed to pad to a character count evenly divisible by 8.…

  • Bash Snippet: Trim Function

    Bash Snippet: Trim Function

    Occasionally I find myself wanting to removing leading and/or trailing occurrences of a character or string from a larger string in bash. A couple common uses are removing quotes, or stripping an unknown number of spaces leading into the data of interest. This can be done pretty easily with other methods such as awk or…

  • Bash Snippet: URL Encoding

    Bash Snippet: URL Encoding

    One approach would be to encode everything, but the approach I took was to just encode things I thought might be problematic to pass over the query string. URL (a.k.a percent) encoding of a string in bash: urlencode () {         tab="`echo -en "\x9"`"         i="$@"      …

  • Bash Snippet: Calculating the Distance Between 2 Coordinates

    Bash Snippet: Calculating the Distance Between 2 Coordinates

    I have a tendency to do things in bash that I’d probably be better off doing in perl or python. Although bash may have super powers, math is not one of them, and so like my last post this script also requires bc. I’ll try and keep these code snippet posts short and sweet, and…

  • Bash Snippet: HTML &#code; decoder

    Bash Snippet: HTML &#code; decoder

    A short and simple way to decode HTML decimal (and hex) character codes in bash. html_decode () {     html_encoded="$1"     html_encoded=${html_encoded//&#/ }     html_encoded=(`echo $html_encoded`)     for html_dec in ${html_encoded[@]}     do         html_dec="${html_dec//X/x}"         html_dec="${html_dec//;/}"         if [ "${html_dec:0:1}"…