~tardypad/dotfiles

859c03bb89aece1a6f0b3ae486191a91cdf244f1 — Damien Tardy-Panis a month ago 1add4f9
Add base shell script
M files/docs/shell/Makefile => files/docs/shell/Makefile +1 -0
@@ 7,6 7,7 @@ shell-docs:
	$(setup_file)/docs/shell/backup-contacts.scd          | scdoc > $(DESTDIR)$(DATA_DIR)/man/man1/backup-contacts.1perso
	$(setup_file)/docs/shell/backup-notes.scd             | scdoc > $(DESTDIR)$(DATA_DIR)/man/man1/backup-notes.1perso
	$(setup_file)/docs/shell/backup-time-tracker-logs.scd | scdoc > $(DESTDIR)$(DATA_DIR)/man/man1/backup-time-tracker-logs.1perso
	$(setup_file)/docs/shell/base.scd                     | scdoc > $(DESTDIR)$(DATA_DIR)/man/man1/base.1perso
	$(setup_file)/docs/shell/battery-status.scd           | scdoc > $(DESTDIR)$(DATA_DIR)/man/man1/battery-status.1perso
	$(setup_file)/docs/shell/calculate.scd                | scdoc > $(DESTDIR)$(DATA_DIR)/man/man1/calculate.1perso
	$(setup_file)/docs/shell/check-notify-low-battery.scd | scdoc > $(DESTDIR)$(DATA_DIR)/man/man1/check-notify-low-battery.1perso

A files/docs/shell/base.scd => files/docs/shell/base.scd +37 -0
@@ 0,0 1,37 @@
base(1)

# NAME

base - Convert numbers between different bases

# SYNOPSIS

base _base_in_ _base_out_

# DESCRIPTION

Convert all numbers from stdin to a different base

# OPTIONS

None

# OPERANDS

_base_in_
	Base of input numbers

_base_out_
	Base of output numbers

# ENVIRONMENT VARIABLES

None

# FILES

None

# DEPENDENCIES

None

M files/scripts/shell/Makefile => files/scripts/shell/Makefile +2 -0
@@ 7,6 7,7 @@ shell-scripts:
	$(setup_file)/scripts/shell/backup-contacts              > $(DESTDIR)$(BIN_DIR)/backup-contacts
	$(setup_file)/scripts/shell/backup-notes                 > $(DESTDIR)$(BIN_DIR)/backup-notes
	$(setup_file)/scripts/shell/backup-time-tracker-logs     > $(DESTDIR)$(BIN_DIR)/backup-time-tracker-logs
	$(setup_file)/scripts/shell/base                         > $(DESTDIR)$(BIN_DIR)/base
	$(setup_file)/scripts/shell/battery-status               > $(DESTDIR)$(BIN_DIR)/battery-status
	$(setup_file)/scripts/shell/calculate                    > $(DESTDIR)$(BIN_DIR)/calculate
	$(setup_file)/scripts/shell/check-notify-low-battery     > $(DESTDIR)$(BIN_DIR)/check-notify-low-battery


@@ 55,6 56,7 @@ shell-scripts:
	chmod +x $(DESTDIR)$(BIN_DIR)/backup-contacts
	chmod +x $(DESTDIR)$(BIN_DIR)/backup-notes
	chmod +x $(DESTDIR)$(BIN_DIR)/backup-time-tracker-logs
	chmod +x $(DESTDIR)$(BIN_DIR)/base
	chmod +x $(DESTDIR)$(BIN_DIR)/battery-status
	chmod +x $(DESTDIR)$(BIN_DIR)/calculate
	chmod +x $(DESTDIR)$(BIN_DIR)/check-notify-low-battery

A files/scripts/shell/base => files/scripts/shell/base +48 -0
@@ 0,0 1,48 @@
#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  BASE_IN=
  BASE_OUT=
}

parse_command_line() {
  BASE_IN="$1"
  BASE_OUT="$2"
}

validate_command_line() {
  if [ -z "${BASE_IN}" ]; then
    exit_error "missing input base"
  fi

  if [ -z "${BASE_OUT}" ]; then
    exit_error "missing output base"
  fi

  case "${BASE_IN}" in
    *[!0-9]*) exit_error 'input base must be an integer' ;;
  esac

  case "${BASE_OUT}" in
    *[!0-9]*) exit_error 'output base must be an integer' ;;
  esac
}

exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  exit 1
} >&2

convert() {
  while read -r NUMBER; do
    echo "obase=${BASE_OUT};ibase=${BASE_IN};${NUMBER}" | bc
  done
}

init_variables
parse_command_line "$@"
validate_command_line

convert