4

Best of .bashrc

 3 years ago
source link: https://www.datagubbe.se/bestofbash/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Best of .bashrc

Or, actually, .bash_aliases - but that's not as fun to say

On this page I've gathered some selected bits and pieces from my .bash_aliases file. Apart from the selection of hacks and scripts available on this site, these are the homemade contraptions I use most frequently when farting around with my computer.

Quick links:

Putting the computer to sleep

# suspend (sleep)
alias zzz="systemctl suspend"

A nifty little piece I believe to be stolen from the *BSD world. On a systemd Linux installation, this will put the computer to sleep.

Getting system information

# system health information
alias inf="uname -sr && uptime| sed 's/ //' && sensors|grep Pack && \
           lscpu|grep 'CPU MHz:' && acpi && \
           echo -n 'Memory in use: ' && free -m|grep Mem|\
           awk '{print \$3+\$5\" megs\"}'"

This will show the uptime, CPU temperature, current CPU frequency, battery status and RAM usage. The various commands used will differ somewhat between platforms and distributions. Example output:

Linux 5.4.0-72-generic
00:05:42 up 3 days,  9:36,  4 users,  load average: 0,05, 0,10, 0,09
Package id 0:  +39.0°C  (high = +105.0°C, crit = +105.0°C)
CPU MHz:             1152.108
Battery 0: Not charging, 100%
Memory in use: 947 megs

Listing directories

alias lsd="ls -d */"

This lists just the directories in the current working directory. Coming from the Amiga side of things, it's preposterous how unintuitive this is. dir dirs, anyone? But you know how the saying goes: "There are two major products to come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence." Now I have managed to combine them!

Working with archives

# archives
alias ltar="tar -ztvf"
alias untar="tar -zxvf"
alias atar="tar -cvzf"

Lists, unpacks and creates .tgz archives. Why atar? I don't remember, but now I'm used to it.

Calculations with decimals

# bc calc shorthand
function cl {
  echo "scale=5; $@" | bc
}

POSIX reference documentation

# show posix command reference
function posix {
  if [ -z "$1" ]; then
    (dillo "$HOME/docs/posix-2018/idx/utilities.html" >/dev/null &)
  else
    HTMLFILE="$HOME/docs/posix-2018/utilities/$1.html"
    if [ -s "$HTMLFILE" ]; then
      (dillo "$HTMLFILE" >/dev/null &)
    else
      echo "No matching file for '$1'"
    fi
  fi
}

Use Dillo to view either a list of all POSIX shell commands, or the POSIX documentation for a single command. I've downloaded the official documentation from here (it's free for personal use) and keep it unpacked in my home directory, so I can just use posix awk to read a very comprehensive description of awk. This documentation is often much better than man pages and contains actually useful examples.

Taking notes

# take a note
function note {
  if [ -z "$1" ]; then
    echo "No note entered."
  else
    echo "$@" >> "$HOME/docs/_line_notes"
  fi
}

# display notes
function notes {
  FN="$HOME/docs/_line_notes"
  if [ ! -f "$FN" ]; then
    echo "No notes (file missing)."
    return
  fi

  if [ -z "$1" ]; then
    LINECOUNT="10"
  else
    LINECOUNT="$1"
  fi
  LNUM=$(wc -l "$FN"|cut -d " " -f1)
  echo "Showing max $LINECOUNT of $LNUM notes."
  tail "$FN" -n "$LINECOUNT"
}

# clear all notes
function clearnotes {
  rm "$HOME/docs/_line_notes"
  touch "$HOME/docs/_line_notes"
}

Using note, I can make a quick note like so:
$ note hjalfi asm 1:20
which will indicate that I've watched one hour and twenty minutes of Hjalfi writes an assembler and emulator for a spaceship, so that I know where to continue watching next time by simply typing:
$ notes
Pretty nifty!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK