Better HN
topnewbestaskshowjobs
Log inSign up

gr4viton

1 karma · joined Jan 14, 2019 (7y ago) · 3 submissions

Submissions

comment 7y ago · link · parent

Plus `ask_yes` if you want to ask if user is sure.

   ask_yes () {
    read -r -p "$1 = Are you sure? [y/N] " response
    if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
    then
        return 0  # true = 0
    else
        return 1  # false = 1
    fi
   }
Plus `countdown timer` to make user think about what he has done, before he does it. (Code is a mess cause i used some snippet then created another from it)

  countdown_str () {
    # countdown "00:01:00" # = hh:mm:ss
    IFS=:
    set -- $*
    secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} ))
    while [ $secs -gt 0 ]
    do
      sleep 1 &
      printf "\r%02d:%02d:%02d" $((secs/3600)) $(( (secs/60)%60)) $((secs%60))
      secs=$(( $secs - 1 ))
      wait
    done
    echo
  }

  countdown_5 () {
    countdown 5 2>/dev/null
  }

  countdown () {
    secs=${1:-"00"}
    mins=${2:-"00"}
    hours=${3:-"00"}
    chars=$(printf "%02d:%02d:%02d" $hours $mins $secs)
    echo "Countdown from $chars"
    countdown_str $chars 2>/dev/null
  }

comment 7y ago · link · parent

Well the most used, and first thing i add when i get to a new system is:

   rcbash='~/.bashrc' 
   alias src="source $rcbash"
   alias vrc="vim $rcbash"
   alias vimo="vim -O"  # vsplit files

And then, a little snippet which made my "bashrc" less repetitive when it comes to folder aliases.

   gr4_folderize() {
    # creates aliases and variables for a path
    #
    # $1 = used abbreviation
    # $2 = folder path
    #
    # gr4_folderize "esp32" "/srv/dd/esp32"
    # creates aliases `cdesp32`, `lsesp32` and `diresp32` env

    # ${param:?word} writes `word` to stdout when param is unset or null
    local abbrev="${1:?No abbreviation alias.}"
    local folder="${2:?No directory to folderize.}"

    export dir${abbrev}="$folder"
    which > /dev/null 2>&1 cd${abbrev} || alias cd${abbrev}="cd $folder"
    which > /dev/null 2>&1 ls${abbrev} || alias lll${abbrev}o="lla $folder"
   }

comment 7y ago · link · parent

for the `mkae` you should really use `thefuck` cli anti-typo tool ;) https://github.com/nvbn/thefuck