functions: funciones para ksh93

This commit is contained in:
Tuxliban Torvalds 2023-05-05 18:24:23 -06:00
parent 809157337b
commit 93d6e98e6c
13 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,9 @@
# Añadir esta configuración en /root/.profile para personalizar el prompt para root
function admin_prompt
{
[[ $PWD == $HOME ]] && dir="~" || dir="${PWD#*/}"
print "${YELLOW}[$dir] ${GREEN}#${RED} "
}
PS1="\$(admin_prompt) "

11
functions_korn/count Normal file
View File

@ -0,0 +1,11 @@
# Contar archivos o directorios en el directorio
# Esto funciona pasando la salida del glob a la función y luego contando el número de argumentos.
function count
{
# Uso:
# count /path/to/dir/* -> Total (archivos y directorios)
# count /path/to/dir/*/ -> Total de directorios
[[ -e $1 ]] && print "$#" || print 0
}

View File

@ -0,0 +1,13 @@
# Obtener el número de líneas de un fichero
function count_lines
{
lines=0
while IFS= read -r line || [[ -n $line ]]; do
# lines=$((lines+1)) is slower than ((lines=lines+1))
((lines=lines+1))
done < "$1"
printf '%s\n' "$lines"
}

29
functions_korn/extraer Normal file
View File

@ -0,0 +1,29 @@
# Extrar archivos comprimidos y/o empaquetados
function ex
{
if [ -f "$1" ]; then
case $1 in
*.tar.bz2)
bsdtar -xvjf "$1"
;;
*.tar.gz)
bsdtar -xvzf "$1"
;;
*.rar)
bsdtar -xf "$1"
;;
*.tar.xz)
bsdtar -xvjf "$1"
;;
*.zip)
bsdtar -xf "$1"
;;
*.7z)
bsdtar -xf "$1"
;;
*)
print "El archivo $1 no puede ser extraído. Formato desconocido"
esac || return 1
fi
}

11
functions_korn/get_branch Normal file
View File

@ -0,0 +1,11 @@
# Mostrar rama actual en directorios de git
function get_branch
{
BRANCH=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [[ ! -z ${BRANCH} ]]; then
print "${BRANCH}"
fi
unset BRANCH
}

6
functions_korn/get_dir Normal file
View File

@ -0,0 +1,6 @@
# Mostrar directorio de trabajo actual
function get_dir
{
printf '%s\n' ${PWD/$HOME/\~}
}

12
functions_korn/ll Normal file
View File

@ -0,0 +1,12 @@
# Función personalizada para ls
# Dependiendo del número de entradas, mostrará el contenido ajustándolo a la pantalla
function ll
{
local __lstmp=$(command ls -X -1 --color=always "$@")
if [ ! $( print "$__lstmp" | wc -l) -gt $LINES ]; then
print "$__lstmp"
else
command ls -C --color=always "$@"
fi
}

13
functions_korn/man Normal file
View File

@ -0,0 +1,13 @@
# Mostrar los manuales de los programa con color
function man
{
LESS_TERMCAP_mb=$(printf %b "\033[1;31m") \
LESS_TERMCAP_md=$(printf %b "\033[1;31m") \
LESS_TERMCAP_me=$(printf %b "\033[0m") \
LESS_TERMCAP_se=$(printf %b "\033[0m") \
LESS_TERMCAP_so=$(printf %b "\033[1;44;33m") \
LESS_TERMCAP_ue=$(printf %b "\033[0m") \
LESS_TERMCAP_us=$(printf %b "\033[1;32m") \
command man -a "$@"
}

7
functions_korn/mkcd Normal file
View File

@ -0,0 +1,7 @@
# Crear un directorio y cambiar a él
function mkcd
{
mkdir -p -- "$1" || return
cd -P -- "$1"
}

9
functions_korn/prompt Normal file
View File

@ -0,0 +1,9 @@
# Prompt para usuario normal
function prompt
{
print "${GREEN}${PWD/$HOME/\~} ${CYAN}$(get_branch)"
print "${RED}-> ${YELLOW}$ ${NORM}"
}
PS1="\$(prompt) "

11
functions_korn/rman Normal file
View File

@ -0,0 +1,11 @@
# Consultar manuales de programa remotos
rman() {
if command -v lynx >/dev/null; then
lynx https://man.voidlinux.org/x86_64/"$@"
return 0
else
print "Instale el programa lynx"
return 1
fi
}

5
functions_korn/ver Normal file
View File

@ -0,0 +1,5 @@
# Consultar qué versión de shell korn se tiene en el sistema
ver() {
printf '%s\n' "Korn Shell: ${KSH_VERSION:-desconocida}"
}

13
functions_korn/which_sudo Normal file
View File

@ -0,0 +1,13 @@
# Función que se encarga de determinar qué programa usar para escalar
# permisos de administrador
function which_sudo
{
if command -v sudo >/dev/null && sudo -l | grep -q -e ' ALL$' -e xbps-install; then
print sudo
elif command -v doas >/dev/null && [ -f /etc/doas.conf ]; then
print doas
elif [[ $(id -u) != 0 ]]; then
print su
fi
}