Geody Labs


# Main Index: Debian Linux Magic Spells Cheat Sheet (one liners, how to, tips and tricks)

# Shell Customization

How to customize your shell.

Edit the script executed at the shell start-up (user login shell):
# Custom Prompt and Aliases can be set here
jed ~/.bash_profile

Sample bash profile script:
(see https://labs.geody.com/systatus/ )
---

#!/bin/bash
# ~/.bash_profile

# Systatus BASH Shell Start-up
# r2020-12-27 fr2016-10-18
# by Valerio Capello - https://labs.geody.com/ - License: GPL v3.0

# Config
tshilon="\e[0;33m"; tshilof="\e[0m";
tsalerton="\e[0;31m"; tsalertof="\e[0m";

# Set Terminal Window Size
# tlins=40; tcols=160; echo -e "\e[8;$tlins;${tcols}t";

# Get Terminal Window Size
COLUMNS="$(tput cols)"; LINES="$(tput lines)";

# Prompt
export PS1="\[\e[1;32m\][\$(date +%Y-%m-%d\ %H:%M:%S\ %a)] \u@\h:\w\\\$\[\e[0m\] "

# History Date Format
export HISTTIMEFORMAT="%F %T  "

# Aliases
alias l="ls -laF --group-directories-first --color=auto"
alias d="ls -aF --group-directories-first --color=auto"

# Message
echo; clear
date "+%a %d %b %Y %H:%M:%S %Z (UTC%:z)"
echo -n "Hello "; echo -ne "$tshilon"; echo -n "$(whoami)"; echo -ne "$tshilof";
if [[ "$SSH_CONNECTION" ]]; then
echo -n " ("; echo -ne "$tshilon"; echo -n "`echo $SSH_CLIENT | awk '{print $1}'`"; echo -ne "$tshilof)";
fi
echo -n ", ";
echo -n "welcome to "; echo -ne "$tshilon"; echo -n "$(hostname)"; echo -ne "$tshilof";
echo -n " ("; echo -ne "$tshilon"; echo -n "$(hostname -i)"; echo -ne "$tshilof)";
echo ".";
echo -n "Machine ID: "; echo -n "$(cat /etc/machine-id) ";
echo -n "Boot ID: "; echo -n "$(cat /proc/sys/kernel/random/boot_id) ";
echo -n "Session ID: "; echo "$(cat /proc/self/sessionid)";
echo -n "You are ";
if [[ -n "$SSH_CONNECTION" ]]; then
echo -n "connected remotely via SSH";
elif [[ "${DISPLAY%%:0*}" != "" ]]; then
echo -n "connected remotely "; echo -ne "$tsalerton"; echo -n "NOT"; echo -ne "$tsalertof"; echo " via SSH (which is Bad)";
else
echo -n "connected locally";
fi
utty="$(tty)";
if [[ $utty == "/dev/"* ]]; then
utty=$(cut -c 6- <<< $utty);
fi
echo -n " on $utty";
echo ". Your Terminal Window Size is $COLUMNS x $LINES"
if [[ $EUID -eq 0 ]]; then
echo -ne "$tsalerton"; echo -n "You have ROOT superpowers!"; echo -e "$tsalertof";
fi
echo

# Machine
# echo -n "Vendor: "; echo "$(cat /sys/class/dmi/id/sys_vendor)";
echo -n "Machine: "; echo "$(cat /sys/class/dmi/id/product_name)";
# echo -n "Machine Type: "; echo "$MACHTYPE";
# echo -n "Board: "; echo "$(cat /sys/class/dmi/id/board_vendor) $(cat /sys/class/dmi/id/board_name)";
# echo -n "BIOS: "; echo "$(cat /sys/class/dmi/id/bios_vendor) $(cat /sys/class/dmi/id/bios_version) $(cat /sys/class/dmi/id/bios_date)";
echo -n "CPU: "; echo -n "$(grep 'model name' /proc/cpuinfo | head -1). ";
echo -n "Cores: "; grep -c 'processor' /proc/cpuinfo

echo -n "CPU average load: "; uptime | awk -F'[a-z]:' '{print $2}' | xargs | awk '{print "1 m: "$1" 5 m: "$2" 15 m: "$3}';
free -h | xargs | awk '{print "Memory: Size: "$8" Used: "$9" Free: "$10" Avail: "$13}';
df / -Th | xargs | awk '{print "FS: "$9" Type: "$10" Size: "$11" Used: "$12" ("$14") Avail: "$13" ("(100-$14)"%)"}';
echo "Last Boot / Uptime: `uptime -s` (`uptime -p`)";
echo

# Software version
lsb_release -ds
uname -a
echo "Bash version: $BASH_VERSION"
# Webserver version
echo -n "$(/usr/sbin/apache2 -v|head --lines=1) "; echo "$(/usr/sbin/apache2 -v|tail --lines=1)";
openssl version -v
php -v|head --lines=1
mysql -V
# echo
echo -n "Installed Packages: "; dpkg --get-selections | wc -l;
# echo "Last installed packages:"; grep install /var/log/dpkg.log | tail --lines=5;
echo

# Users
echo "Last logged users:"; last -n 5 | sed '/^$/d'
echo; echo "Currently logged users:"; who
echo; echo -n "Current user: "; id
echo

# Security
# Shellshock vulnerability check (reports to root only)
if [[ $EUID -eq 0 ]]; then
env x='() { :;}; echo Bash vulnerable to Shellshock' bash -c 'echo -n'
fi

---

You may want to replicate your bash profile script as an executable script (called for example status.sh ) containing information provided in the sections starting from Message and below (you'd better change echo -n "welcome to "; to echo -n "this is ";).

Customize the MOTD (Message Of The Day), message shown after logging in, before than starting the shell:
jed /etc/motd # edit the MOTD

Customize the message shown before than the log in request:
jed /etc/issue # edit the welcome message

Customize the command line:

jed ~/.inputrc

Sample .inputrc script:
---

# ~/.inputrc

# .inputrc
# r2020-11-12 fr2020-11-12
# by Valerio Capello - https://labs.geody.com/ - License: GPL v3.0

# Enable autocomplete
set disable-completion off

# Show autocomplete results in pages
set page-completions on

# Show file types for completed results (like ls -F )
set visible-stats on

# If there are too many completion results, ask to show them all
set completion-query-items 100

# Bell
# set bell-style audible

# Go to next autocomplete result with Shift-Tab
"\e[Z": menu-complete

# History search (Arrows Up and Down)
"\e[A": history-search-backward
"\e[B": history-search-forward





Please DONATE to support the development of Free and Open Source Software (PayPal, Credit Card, Bitcoin, Ether)

Page issued on 25-Sep-2022 05:46 GMT
Copyright (c) 2022 Geody - Legal notices: copyright, privacy policy, disclaimer