home.social

#unixshell — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #unixshell, aggregated by home.social.

  1. Wrote a #shell function without using ls inside of $( ), so my inner @mirabilos won't harass me. XD

    #slightly easier wireguard command
    function wg {
        local dir file profile profiledir= parm=${1:-} statustext
        #Find profile dir
        for dir in {,/usr/local}/etc/wireguard; do
            if [[ -d $dir ]]; then
                profiledir=$dir
                break
            fi
        done
        #Find config file
        if [[ -n $profiledir ]]; then
            for file in $profiledir/*.conf; do
                if [[ -e $file ]]; then
                    profile=${file//*\/}
                    profile=${profile/.conf}
                    break
                fi
            done
        fi
        [[ -n $profile ]] || profile=proton
        statustext="wireguard profile $profile"
        case ${parm,,} in
            up|on)      doas wg-quick  up  $profile;;
            down|off)   doas wg-quick down $profile;;
            status)     echo -en "$statustext _______\r"
                        echo -en "$statustext "
                        ifconfig |grep -q "^$profile:" && echo enabled || echo disabled;;
            *)          warn "wg usage: wg up|down|status";;
        esac
    }
    

    Hmm, seems ${foo,,} for lower case conversion is #bash-only. I wonder if I should use tr instead.

    #Unix #UnixShell #ShellScript #ShellScripting

  2. Wrote a #shell function without using ls inside of $( ), so my inner @mirabilos won't harass me. XD

    #slightly easier wireguard command
    function wg {
        local dir file profile profiledir= parm=${1:-} statustext
        #Find profile dir
        for dir in {,/usr/local}/etc/wireguard; do
            if [[ -d $dir ]]; then
                profiledir=$dir
                break
            fi
        done
        #Find config file
        if [[ -n $profiledir ]]; then
            for file in $profiledir/*.conf; do
                if [[ -e $file ]]; then
                    profile=${file//*\/}
                    profile=${profile/.conf}
                    break
                fi
            done
        fi
        [[ -n $profile ]] || profile=proton
        statustext="wireguard profile $profile"
        case ${parm,,} in
            up|on)      doas wg-quick  up  $profile;;
            down|off)   doas wg-quick down $profile;;
            status)     echo -en "$statustext _______\r"
                        echo -en "$statustext "
                        ifconfig |grep -q "^$profile:" && echo enabled || echo disabled;;
            *)          warn "wg usage: wg up|down|status";;
        esac
    }
    

    Hmm, seems ${foo,,} for lower case conversion is #bash-only. I wonder if I should use tr instead.

    #Unix #UnixShell #ShellScript #ShellScripting

  3. Wrote a #shell function without using ls inside of $( ), so my inner @mirabilos won't harass me. XD

    #slightly easier wireguard command
    function wg {
        local dir file profile profiledir= parm=${1:-} statustext
        #Find profile dir
        for dir in {,/usr/local}/etc/wireguard; do
            if [[ -d $dir ]]; then
                profiledir=$dir
                break
            fi
        done
        #Find config file
        if [[ -n $profiledir ]]; then
            for file in $profiledir/*.conf; do
                if [[ -e $file ]]; then
                    profile=${file//*\/}
                    profile=${profile/.conf}
                    break
                fi
            done
        fi
        [[ -n $profile ]] || profile=proton
        statustext="wireguard profile $profile"
        case ${parm,,} in
            up|on)      doas wg-quick  up  $profile;;
            down|off)   doas wg-quick down $profile;;
            status)     echo -en "$statustext _______\r"
                        echo -en "$statustext "
                        ifconfig |grep -q "^$profile:" && echo enabled || echo disabled;;
            *)          warn "wg usage: wg up|down|status";;
        esac
    }
    

    Hmm, seems ${foo,,} for lower case conversion is #bash-only. I wonder if I should use tr instead.

    #Unix #UnixShell #ShellScript #ShellScripting

  4. Wrote a #shell function without using ls inside of $( ), so my inner @mirabilos won't harass me. XD

    #slightly easier wireguard command
    function wg {
        local dir file profile profiledir= parm=${1:-} statustext
        #Find profile dir
        for dir in {,/usr/local}/etc/wireguard; do
            if [[ -d $dir ]]; then
                profiledir=$dir
                break
            fi
        done
        #Find config file
        if [[ -n $profiledir ]]; then
            for file in $profiledir/*.conf; do
                if [[ -e $file ]]; then
                    profile=${file//*\/}
                    profile=${profile/.conf}
                    break
                fi
            done
        fi
        [[ -n $profile ]] || profile=proton
        statustext="wireguard profile $profile"
        case ${parm,,} in
            up|on)      doas wg-quick  up  $profile;;
            down|off)   doas wg-quick down $profile;;
            status)     echo -en "$statustext _______\r"
                        echo -en "$statustext "
                        ifconfig |grep -q "^$profile:" && echo enabled || echo disabled;;
            *)          warn "wg usage: wg up|down|status";;
        esac
    }
    

    Hmm, seems ${foo,,} for lower case conversion is #bash-only. I wonder if I should use tr instead.

    #Unix #UnixShell #ShellScript #ShellScripting

  5. Wrote a #shell function without using ls inside of $( ), so my inner @mirabilos won't harass me. XD

    #slightly easier wireguard command
    function wg {
        local dir file profile profiledir= parm=${1:-} statustext
        #Find profile dir
        for dir in {,/usr/local}/etc/wireguard; do
            if [[ -d $dir ]]; then
                profiledir=$dir
                break
            fi
        done
        #Find config file
        if [[ -n $profiledir ]]; then
            for file in $profiledir/*.conf; do
                if [[ -e $file ]]; then
                    profile=${file//*\/}
                    profile=${profile/.conf}
                    break
                fi
            done
        fi
        [[ -n $profile ]] || profile=proton
        statustext="wireguard profile $profile"
        case ${parm,,} in
            up|on)      doas wg-quick  up  $profile;;
            down|off)   doas wg-quick down $profile;;
            status)     echo -en "$statustext _______\r"
                        echo -en "$statustext "
                        ifconfig |grep -q "^$profile:" && echo enabled || echo disabled;;
            *)          warn "wg usage: wg up|down|status";;
        esac
    }
    

    Hmm, seems ${foo,,} for lower case conversion is #bash-only. I wonder if I should use tr instead.

    #Unix #UnixShell #ShellScript #ShellScripting

  6. Recursion usually scares me a bit, but it worked out nicely here:

    #convert "cx"-style Esperanto notation to native accents (ĉ)
    function eaccent {
        if [[ ${1:-} ]]; then
            echo "$*" |eaccent
        else
            sed 's/cx/ĉ/g; s/gx/ĝ/g; s/hx/ĥ/g; s/jx/ĵ/g; s/sx/ŝ/g; s/ux/ŭ/g; s/C[xX]/Ĉ/g; s/G[xX]/Ĝ/g; s/H[xX]/Ĥ/g; s/J[xX]/Ĵ/g; s/S[xX]/Ŝ/g; s/U[xX]/Ŭ/g'
        fi
    }
    

    #bash #unix #shell #scripts #scripting #UnixShell #ShellScripting #Esperanto

  7. Recursion usually scares me a bit, but it worked out nicely here:

    #convert "cx"-style Esperanto notation to native accents (ĉ)
    function eaccent {
        if [[ ${1:-} ]]; then
            echo "$*" |eaccent
        else
            sed 's/cx/ĉ/g; s/gx/ĝ/g; s/hx/ĥ/g; s/jx/ĵ/g; s/sx/ŝ/g; s/ux/ŭ/g; s/C[xX]/Ĉ/g; s/G[xX]/Ĝ/g; s/H[xX]/Ĥ/g; s/J[xX]/Ĵ/g; s/S[xX]/Ŝ/g; s/U[xX]/Ŭ/g'
        fi
    }
    

    #bash #unix #shell #scripts #scripting #UnixShell #ShellScripting #Esperanto

  8. Recursion usually scares me a bit, but it worked out nicely here:

    #convert "cx"-style Esperanto notation to native accents (ĉ)
    function eaccent {
        if [[ ${1:-} ]]; then
            echo "$*" |eaccent
        else
            sed 's/cx/ĉ/g; s/gx/ĝ/g; s/hx/ĥ/g; s/jx/ĵ/g; s/sx/ŝ/g; s/ux/ŭ/g; s/C[xX]/Ĉ/g; s/G[xX]/Ĝ/g; s/H[xX]/Ĥ/g; s/J[xX]/Ĵ/g; s/S[xX]/Ŝ/g; s/U[xX]/Ŭ/g'
        fi
    }
    

    #bash #unix #shell #scripts #scripting #UnixShell #ShellScripting #Esperanto

  9. Recursion usually scares me a bit, but it worked out nicely here:

    #convert "cx"-style Esperanto notation to native accents (ĉ)
    function eaccent {
        if [[ ${1:-} ]]; then
            echo "$*" |eaccent
        else
            sed 's/cx/ĉ/g; s/gx/ĝ/g; s/hx/ĥ/g; s/jx/ĵ/g; s/sx/ŝ/g; s/ux/ŭ/g; s/C[xX]/Ĉ/g; s/G[xX]/Ĝ/g; s/H[xX]/Ĥ/g; s/J[xX]/Ĵ/g; s/S[xX]/Ŝ/g; s/U[xX]/Ŭ/g'
        fi
    }
    

    #bash #unix #shell #scripts #scripting #UnixShell #ShellScripting #Esperanto

  10. Recursion usually scares me a bit, but it worked out nicely here:

    #convert "cx"-style Esperanto notation to native accents (ĉ)
    function eaccent {
        if [[ ${1:-} ]]; then
            echo "$*" |eaccent
        else
            sed 's/cx/ĉ/g; s/gx/ĝ/g; s/hx/ĥ/g; s/jx/ĵ/g; s/sx/ŝ/g; s/ux/ŭ/g; s/C[xX]/Ĉ/g; s/G[xX]/Ĝ/g; s/H[xX]/Ĥ/g; s/J[xX]/Ĵ/g; s/S[xX]/Ŝ/g; s/U[xX]/Ŭ/g'
        fi
    }
    

    #bash #unix #shell #scripts #scripting #UnixShell #ShellScripting #Esperanto

  11. When you're dealing with plaintext data in a suspicious context, consider using `cat -v` where you'd usually use `cat`.

    ```
    -v, --show-nonprinting
    use ^ and M- notation, except for LFD and TAB
    ```

  12. P.S., the body of the parent #toot was created by a simple #shell #function:

    function apod {
        #Today's NASA Astronomy Picture of the Day info-fetcher
        curl -sL 'https://apod.nasa.gov/apod/archivepix.html' \
            |grep -m1 "[0-9][0-9]:" \
            |sed 's/^/Date: /;
                s|: *<a href="|\nURL: https://apod.nasa.gov/apod/|;
                s/">/\nTitle: /; s/<.*$//'
        echo
        echo "#NASA #Astronomy #PictureOfTheDay"
    }
    

    #bash #ksh #mksh #shellScripting #unix #UnixShell #WebScraping #Scraping #HTML

  13. In the helpful shell functions department:

    (Requires bc to be installed)

    load() {
        local load=$(uptime |sed -E "s/^.*load averages?: //; s/,.*$//")
        local uname="$(uname)"
        local cpus
        if [[ $1 == -q ]]; then
            echo "$load"
        elif [[ $1 == -i ]]; then
            echo "$load + 0.5" |bc -l |cut -f1 -d.
        else
            case "$uname" in
                Linux)  cpus=$(grep -c ^processor /proc/cpuinfo);;
                *BSD)   cpus=$(sysctl hw.ncpu |tr -dc "0-9\n");;
                *)      warn "load(): assuming 1 cpu, don't know how to get the number in \"$uname\""
                        cpus=1;;
            esac
            local loadPerCPU=$(echo "scale=3; $load / $cpus" |bc -l |sed 's/^\./0./')
            echo "$load ($loadPerCPU per processor ($cpus))"
        fi
    }
    

    #ShellScripting #UnixShell #ShellScripts #bash #ksh #sh

  14. #Poll: Curious about people's attitudes towards shell scripting.

    Two part question:

    1. Are you a DEVeloper (or working in a development-heavy role), OTHER-IT worker (such as a sysadmin, architect, anything in a non-development-heavy role), or NON-IT (accountant, doctor, whatever)
    2. Do you HATE shell scripting, are you INDIFferent towards (or ignorant of) shell scripting, or do you LOVE it?

    #Unix #UnixShell #ShellScript #ShellScripting #POSIX #PosixShell #sh #bash #zsh #csh #tcsh #ksh #pdksh #oksh #mksh

  15. I've probably tooted about this before, but I don't know why this isn't standard.

    It's just so obvious, at least to me. ;)

    ~ $ type mcd
    mcd is a function
    mcd () 
    { 
        [[ -n $1 ]] && mkdir "$1" && cd "$1"
    }
    

    #shell #UnixShell #ShellScripting #sh #bash #ksh #mksh

  16. A wild #blog post appears!

    I do some benchmarking of #oksh with nine C compilers on #OpenBSD. Nothing conclusive is discovered, but it is nonetheless a very fun journey.

    briancallahan.net/blog/2021101

    #Unix #Linux #FreeBSD #NetBSD #DragonFlyBSD #BSD #C #compile #compiler #compilers #shell #unixshell #kornshell #ksh