~~stoggle_buttons~~ ====== Trucos ====== * ''stty sane'' -> cuando te peta la terminal y no puedes escribir * ''xkill'' -> kill seleccionando gráficamente la ventana ===== Links ===== * [[https://tldr.sh/|tldr]] man pages with practical examples. * [[https://devhints.io/bash]] * [[https://github.com/LeCoupa/awesome-cheatsheets/blob/master/languages/bash.sh]] * [[https://tldp.org/LDP/abs/html/]] * [[https://github.com/awesome-lists/awesome-bash]] * [[https://toroid.org/unix-pipe-implementation|How are Unix pipes implemented?]] incluye links a código muy antiguo (hasta 1970) de UNIX * [[https://opensource.com/article/20/6/bash-trap|bash trap]] exception handling ===== Sin clasificar ===== * nl para añadir números de línea ''%%echo -e "one\ntwo\nthree" | nl%%'' * flock para crear locks en scripts de bash * [[https://www.shellcheck.net/]] -> finds bugs in your shell scripts (linting) ===== find ===== ==== Eliminar archivos por extensión ==== list=`find . -name *.pyc` readarray -t goodlist <<<"$list" for ((i=0; i<= ${#goodlist}; i++)); do rm "${goodlist[i]}"; done ==== Ejecutar cosas contra los matches find ==== find -iname "*sync-conflict*" -exec sh -c 'rm {}' \; ==== hash de una carpeta ==== find /path/to/folder/ -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum ==== Permisos recursivos de una carpeta ==== find folder/ -type f -exec bash -c "chmod 644 \"{}\"" \; chmod 664 -R folder/ # Suele ser más rápido que lo anterior find orgzly/ -type d -exec bash -c "chmod 755 \"{}\"" \; # Para poder acceder a los directorios ===== progreso al copiar un disco entero ===== while (true); do echo "scale=4; $(df /dev/dm-3 | tail -n 1 | awk '{print $3}')*100.0/469969564.0" | bc; sleep 5; clear; done # Donde 469969564.0 es el tamaño en bloques usados del dispositivo de la copia Disclaimer: como es un método un poco chapucero, es posible que se quede por debajo o por encima del 100% en algunos casos ===== fzf ===== ==== fuzzy search mostrando info de la selección actual ==== apt-cache search . | fzf --multi --preview 'apt-cache policy {1}' | awk '{print $1}' | xargs -ro apt-get install -s ===== herramientas ===== * [[https://en.wikipedia.org/wiki/agrep|agrep]] Fuzzy grep * ''sort, unique, head, tail'' * Regex con sed y con grep siempre usar -E (extended) para que haga cosas normales y esperables y no tener que poner 4 backslashes * ''seq start step end'' * ''wc'' wordcount para contar líneas, caracteres, ... * ''tee'' se usa mucho pero ni idea cómo va ==== sed ==== ===== Sin categoría ===== stty stop ^P # Ponerlo en ~/.bashrc para que Ctrl+S sirva para buscar hacia delante (Ctrl+R hacia atrás) tail -s -f # follows the log refreshing each n seconds ====== Resumen ====== ===== Atajos de teclado ===== ''%%stty --all%%'' Ctrl+C -> Stop Current Comand (SIGINT) Ctrl+D -> End of input (exit shell) Ctrl+\ -> SIGQUIT, mas estricto que SIGINT Ctrl+Z -> SIGTSTP, detiene el proceso Ctrl+S -> Halt output to screen Ctrl+Q -> Restart output to screen Ctrl+J -> New line Ctrl+L -> Clear Esc+Enter -> New line without executing command [[https://ss64.com/bash/syntax-keyboard.html]] Autocompletado TAB -> General Esc+? -> Todas las posibilidades Esc+/ -> Archivo Ctrl+X/ -> Todos los archivos Esc+~ -> Usuario Ctrl+X~ -> Todos los usuarios Esc+$ -> Variable Ctrl+X$ -> Todas las variables Esc+@ -> Host Ctrl_X@ -> Todos los hosts ===== Procesos ===== * ''$ '' -> ''Ctrl+Z'' lo detiene -> ''$ bg'' lo ejecuta en segundo plano, ''$ fg'' lo ejecuta en primer plano * ''comando &'' -> correr en segundo plano ==== double forking ==== ''(setsid command &)'' * This uses ( &) to fork to background * setsid to detach from the controlling tty. [[https://superuser.com/a/172476]] ==== Ejecutar un proceso redirigiendo logs ==== export MY_VAR="foobar"; run_my_command > "log_`date -Iseconds`.txt" 2>&1 ===== [[ ]] ===== * Diferencia entre ''%%[[%%'' (CONDITIONAL EXPRESSIONS) y ''['', operadores numéricos y strings * [[https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html]] * [[https://unix.stackexchange.com/a/306115]] ===== (( )) ===== * Aritmética de enteros ===== Aritmética de coma flotante ===== bc <<< 'scale=2; 100/3' echo "scale=2; 100/3" | bc ===== arrays, variables ${var#pattern} ===== # Dado un $file, sacar la extensión file="myfile.extension" fname="${file%"."*}" # myfile ext=${file#$fname} # .extension # Dado un filename, sacar dirname y filename dirname="${file%"/"*}" filename=${file#$dirname} filename=${filename:1} ===== variables especiales $@, $?, IFS, PS1 ===== ===== Redirecciones, stdout, stderr ===== * [[https://unix.stackexchange.com/questions/37660/order-of-redirections]] * [[https://catonmat.net/ftp/bash-redirections-cheat-sheet.pdf]] ===== UI ===== * [[https://en.wikibooks.org/wiki/Bash_Shell_Scripting/Whiptail|whiptail]]