dotfiles

My PC Dotfiles & Website

scripts/commands.sh (1596B)


 1#! /usr/bin/env nix
 2#! nix shell nixpkgs#bash nixpkgs#bzip2 nixpkgs#coreutils nixpkgs#git nixpkgs#gnupg nixpkgs#gnutar nixpkgs#gzip nixpkgs#p7zip nixpkgs#unzip -c bash
 3
 4# Useful Commands #
 5
 6temp(){
 7  TEMP="${XDG_RUNTIME_DIR:-/tmp}/$1"
 8  case "${2:-}" in
 9    1) mkdir -p "$TEMP" ;;
10    2) rm -rf "$TEMP" ;;
11    3) ;;
12    *) rm -rf "$TEMP"; mkdir -p "$TEMP" ;;
13  esac
14}
15
16extract() {
17  if [ -f "$1" ]
18  then
19    case $1 in
20    *.tar.bz2) tar xvjf "$1";;
21    *.tar.gz) tar xvzf "$1";;
22    *.tar.xz) tar Jxvf "$1";;
23    *.bz2) bunzip2 "$1";;
24    *.rar) rar x "$1";;
25    *.gz) gunzip "$1";;
26    *.tar) tar xvf "$1";;
27    *.tbz2) tar xvjf "$1";;
28    *.tgz) tar xvzf "$1";;
29    *.zip) unzip -d "${1//\.zip/}" "$1";;
30    *.Z) uncompress "$1";;
31    *.7z) 7z x "$1";;
32    *) error "Can't Extract '$1'" ;;
33    esac
34  else
35    error "File '$1' is invalid"
36  fi
37}
38
39up() {
40  LIMIT=$1
41  P=$PWD/..
42  for ((i=1; i < LIMIT; i++))
43  do
44    P=$P/..
45  done
46  cd "$P" || exit
47}
48
49internet() {
50  if ! nc -zw1 google.com 443 2> /dev/null
51  then
52    error "You are Offline, Please Connect to the Internet"
53  fi
54}
55
56restart() {
57  read -rp "Do you want to Reboot the System? (Y/*): " choice
58    case $choice in
59      [Yy]*) reboot;;
60      *) exit;;
61    esac
62}
63
64# Print
65
66newline() {
67  echo -e "\n";
68}
69
70error() {
71  echo -e "\n\033[0;31merror:\033[0m $1"
72  if [ -n "${2:-}" ]
73  then
74    echo -e "\n$2"
75  fi
76  exit 7
77}
78
79info() {
80  echo -e "\n\033[0;35minfo:\033[0m $1"
81  if [ -n "${2:-}" ]
82  then
83    echo -e "\n$2"
84  fi
85}
86
87warn() {
88  echo -e "\n\033[0;35mwarning:\033[0m $1"
89  if [ -n "${2:-}" ]
90  then
91    echo -e "\n$2"
92  fi
93}