• 19 Posts
  • 18 Comments
Joined 3M ago
cake
Cake day: Jun 19, 2023

help-circle
rss

It’s also traditional to eat raw meat, but we discovered fire at some point.


You also don’t need the dash for the short options.

True, but I refuse to entertain such a non-standard option format. It’s already enough to tolerate find’s.



Custom, interoperable info and man commands
[Source code](https://gist.github.com/sebastiancarlos/179f6cb68b3a6ac00f8e8c7e22291100) ``` #!/usr/bin/env bash # get the texinfo node for a command # - takes a command name, like 'cat' # - if it belongs to a texinfo node, returns the node name, like 'coreutils' # - otherwise returns empty string function __get_texinfo_node_for_command () { local coreutils_commands=( 'cat' 'tac' 'nl' 'od' 'base32' 'base64' 'base64' 'basenc' 'fmt' 'pr' 'fold' 'head' 'tail' 'split' 'csplit' 'wc' 'sum' 'cksum' 'b2sum' 'md5sum' 'sha1sum' 'sha224sum' 'sha256sum' 'sha384sum' 'sha512sum' 'sort' 'shuf' 'uniq' 'comm' 'ptx' 'tsort' 'ptx' 'cut' 'paste' 'join' 'tr' 'expand' 'unexpand' 'ls' 'dir' 'vdir' 'dircolors' 'cp' 'dd' 'install' 'mv' 'rm' 'shred' 'link' 'ln' 'mkdir' 'mkfifo' 'mknod' 'readlink' 'rmdir' 'unlink' 'chown' 'chgrp' 'chmod' 'touch' 'df' 'du' 'stat' 'sync' 'truncate' 'echo' 'printf' 'yes' 'false' 'true' 'test' 'expr' 'tee' 'basename' 'dirname' 'pathchk' 'mktemp' 'realpath' 'pwd' 'stty' 'printenv' 'tty' 'id' 'logname' 'whoami' 'groups' 'users' 'who' 'arch' 'date' 'nping' 'uname' 'hostname' 'hostid' 'uptime' 'chcon' 'runcon' 'chroot' 'env' 'nice' 'nohup' 'stdbuf' 'timeout' 'kill' 'sleep' 'factor' 'numfmt' 'seq' ) local bash_builtins=( '.' ':' '[' 'alias' 'bg' 'bind' 'break' 'builtin' 'caller' 'cd' 'command' 'compgen' 'complete' 'compopt' 'continue' 'declare' 'dirs' 'disown' 'echo' 'enable' 'eval' 'exec' 'exit' 'export' 'false' 'fc' 'fg' 'getopts' 'hash' 'help' 'history' 'jobs' 'kill' 'let' 'local' 'logout' 'mapfile' 'popd' 'printf' 'pushd' 'pwd' 'read' 'readarray' 'readonly' 'return' 'set' 'shift' 'shopt' 'source' 'suspend' 'test' 'times' 'trap' 'type' 'typeset' 'ulimit' 'umask' 'unalias' 'unset' 'wait' # and some reserved words too: '!' # this represents pipelines '[[' '{' 'case' 'if' 'for' 'function' 'select' 'time' 'until' 'while' ) for cmd in "${bash_builtins[@]}"; do if [[ "$1" == "$cmd" ]]; then echo 'bash' return 0 fi done for cmd in "${coreutils_commands[@]}"; do if [[ "$1" == "$cmd" ]]; then echo 'coreutils' return 0 fi done echo '' return 0 } # custom info # - prepend 'coreutils' if argument is a coreutils command # - prepend 'bash' if argument is a bash builtin # - add some default options function info () { # if there is only one non-option argument, and it matches a coreutils or # bash builtin, prepend the corresponding name local option_args=() local non_option_args=() for arg in "$@"; do if [[ ! "$arg" =~ ^- ]]; then non_option_args+=("$arg") else option_args+=("$arg") fi done if [[ "${#non_option_args[@]}" -eq 1 ]]; then info_node="$(__get_texinfo_node_for_command "${non_option_args[0]}")" if [[ -n "$info_node" ]]; then non_option_args=("${info_node}" "${non_option_args[@]}") fi fi # add default options and run option_args+=('--vi-keys' '--init-file' "${XDG_CONFIG_HOME}/infokey") command info "${option_args[@]}" "${non_option_args[@]}" } alias i="info" # custom man # - if receiving a command with info page, run info instead function man () { if [[ "$#" -eq 1 && $(__get_texinfo_node_for_command "$1") != '' ]]; then info "$1" else command man "$1" fi } alias m="man" ```
fedilink

Diff like a boss! (custom Bash commands)
[Source code](https://gist.github.com/sebastiancarlos/2f822a1d1f8e7aad816baded6eb437a4) ``` #!/usr/bin/env bash # diffc - diff commands # - allows to call as: diffc 'command one' 'command two' # instead of: diff <(command one) <(command two) # (Just to save typing a few characters. Lol I'm a lazy programmer) function diffc () { if [[ "$#" != "2" ]]; then echo "diffc requires two arguments" return 1 fi local command=$(printf 'diff <( %s ) <( %s )' "$1" "$2") echo $command eval $command } # diffh - diff history # - make a diff of the output of the last two commands in the shell history function diffh () { # first one is 2nd to last. second is last # remove preceeding spaces local first=$(fc -ln -2 -2) local second=$(fc -ln -1 -1) # print and run diff local command=$(printf 'diff <( %s ) <( %s )' "${first}" "${second}") echo $command eval $command local error_code=$? # replace this 'diffh' entry in history with the command history -s "$(echo $command)" return $error_code } ```
fedilink




GCC embraces web technologies with one (1) line of CSS.
Among the 15~ million lines of codes in the GCC source code, there is a single CSS line. Who dares to call CSS bloat now? More like GSS eh. ``` $ cloc gcc-13.2.0 github.com/AlDanial/cloc v 1.98 T=113.87 s (1029.7 files/s, 143776.9 lines/s) --------------------------------------------------------------------------------------- Language files blank comment code --------------------------------------------------------------------------------------- C++ 31144 610803 701522 2961450 C 50773 410175 635758 2033424 PO File 44 332336 443572 916826 Ada 6610 290082 396614 831278 Go 5347 102068 191016 769730 ... Brainf*** 1 3 4 10 Lisp 1 4 12 7 DOS Batch 2 0 0 4 CSS 1 0 0 1 --------------------------------------------------------------------------------------- SUM: 117256 2322548 2988111 11061312 --------------------------------------------------------------------------------------- ```
fedilink

What happens when you create a Linux user?
Source code: https://gist.github.com/sebastiancarlos/1a5b04099b750f5a15c77c8cb4f03fc8 ``` ### Sample output: # # CREATE_HOME no # CREATE_MAIL_SPOOL no # LOG_INIT yes # USERGROUPS_ENAB yes # HOME /home # MAIL_DIR /var/spool/mail # MAIL_FILE N/A # SHELL /bin/bash # SKEL /etc/skel # EXPIRE N/A # GROUP 984 (users) # INACTIVE -1 # PASS_MAX_DAYS 99999 # PASS_MIN_DAYS 0 # PASS_WARN_AGE 7 # UMASK 077 # ------------------------------------------------------------------------------------- # CREATE_HOME: Create a home directory if it does not exist. # CREATE_MAIL_SPOOL: Create a mailbox file for the user. # LOG_INIT: Add the user to the lastlog and faillog databases. # USERGROUPS_ENAB: If set to yes, useradd will create a group with the name of the # user, and userdel will remove the user's group if it contains no # more members. # HOME: The user's name will be affixed to form the new home. # MAIL_DIR: The mail spool file will be created using this value. # MAIL_FILE: Same, but relative to the user's home directory. # SHELL: The user's login shell. # SKEL: The skeleton directory, which contains files and directories to # be copied into the user's home directory. # EXPIRE: The date on which the user account will be disabled, if any. # GROUP: If USERGROUPS_ENAB is set to no, or if the flag -N/--no-user-group # is passed, this will be the new user's group. # INACTIVE: The number of days after a password has expired in which the # password is still be accepted. -1 disables the inactive period. # PASS_MAX_DAYS: The number of days a password may be used. If is older, a password # change will be forced. # PASS_MIN_DAYS: The minimum number of days allowed between password changes. Any # attempt sooner will be rejected. # PASS_WARN_AGE: Warn user N days befere password expiration. # UMASK: The file mode creation mask is initialized to this value. ```
fedilink

Thanks! I guess yas-bdsm is more of a minimal, “bring your own compiler” project. You end up with a folder for your dotfiles, and it’s up to you to decide how to put stuff there.


When you say immutable what do you mean? Surely dotfiles are meant to change over time? Where would you like to edit them?




"fdisk" and "free" with custom colors
Source code: - fdisk: https://gist.github.com/sebastiancarlos/27c96cf209919d4ad2d8b193945bc6f9 - free: https://gist.github.com/sebastiancarlos/28db5f0579374ff3643040313a7aac19
fedilink

Unfortunately wttr.in produces incorrect weather reports in many cities around the world




It's the "less" week of the "Record Your Dotfiles" contest. Share yours!
https://gist.github.com/sebastiancarlos/9dd95a2c3d5957257e51d623972173f7
fedilink


Opening links in your Bash terminal with the keyboard
[Source code](https://gist.github.com/sebastiancarlos/f20908e58ba9831045f9bddd57bec19a)
fedilink

I thought about it but there are two complications here: The code is recursive, and it calls an external shell script. I’m sure there are ways to do that in awk, but I’m not too much of an awk guru, so I decided to leave my awking to self-contained smaller scripts. Also, considering I need to spawn subprocesses anyway for this, Bash wouldn’t be a bad choice since it’s built for that.



A custom bash "type" command with recursive alias and symlink resolution
[Source code](https://gist.github.com/sebastiancarlos/eadb4dc087b521ce706eb991545e5c95)
fedilink

Isn't it weird that we put credentials in the environment?
Almost every program that we run has access to the environment, so nothing stops them from curling our credentials to some nefarious server. Why don't we put credentials in files and then pass them to the programs that need them? Maybe coupled with some mechanism that prevents executables from reading any random file except those approved.
fedilink

Hey, just wondering, how using a terminal multiplexer adds “more bandwidth” to your ssh session? What do you mean by more bandwidth?



You mean the line containing the licence? Or the user who commented some improvement.suggestions? In any case, I agree :)


Show Notifications in your Terminal
[Source code](https://gist.github.com/sebastiancarlos/ca7124e33d4af1afcf3f29b01f5deadd)
fedilink

Add Vim to your Bash prompt (video)
[source code](https://gist.github.com/sebastiancarlos/b3668160d793d7cb3552cef57000755a)
fedilink

Auto "cd" in Bash with silent output
[source code](https://gist.github.com/sebastiancarlos/efe3fede207232315c5a895f1be19712)
fedilink




That’s a good point. If I need something like a bash script I tend to stick to bash features as much as possible.


just trying to get a good mental model of when it’s reasonable to use tools like awk instead of simpler unix tools. also further confirming that sed is almost never the best tool except for substitutions.