Wilhel

Wilhel

SHELL tools and scripts

Notes#

$_    # Previous parameter
!!    # Previous command
$?    # Exit code of the last command
$#    # Number of arguments
$@    # All arguments
$$    # PID of the shell itself
$0    # Name of the script itself

Create a folder and enter the folder

# mcd.sh
mcd(){
	mkdir -p "$1"
	cd "$1"
}

source mcd.sh
mcd test
# example.sh

#!/bin/bash
  
echo "Start program at $(date)" # Date will be substituted
 
echo "Running program $0 with $# arguments with pid $$"
 
for file in "$@";do
        grep foobar "$file" > /dev/null 2> /dev/null
        # When pattern is not found,grep has exit status
        # We redirect STDOUT and STDERR to a null register ..
        if [[ "$?" -ne 0 ]]; then
                echo "File $file does not have any foobar, adding one"
                echo "# foobar" >> "$file"
        fi      
done

How to use the commands:

  • tldr for more straightforward and concise explanations
sudo apt-get install tldr
mkdir -p ~/.local/share/tldr
tldr -u

Find files

  • fd
sudo apt-get install fd-find
sudo ln -s $(which fdfind) /usr/bin/fd

Search code

  • riggrep(rg)
sudo apt-get install riggrep

# Find all files that use the requests library
rg -t py 'import requests'
# Find all files without a shebang (including hidden files)
rg -u --files-without-match "^#!"
# Find all lines with the string "foo" and print the next 5 lines
rg foo -A 5
# Print matching statistics (number of matching lines and files)
rg --stats PATTERN

General fuzzy search tool

  • fzf
sudo apt-get install fzf

ps aux | fzf
fzf --query "sh"

Folder navigation

  • fasd
  • autojump
  • tree
  • broot
  • nnn
  • ranger

Exercises#

Save the current location

#!/bin/bash
marco(){
	echo $(pwd) > ~/dev/class/missing-semester/marco_history.log
	echo "save pwd $(pwd)"
}
polo(){
	cd "$(cat ~/dev/class/missing-semester/marco_history.log)"
}

#!/bin/bash
marco() {
	export MARCO=$(pwd)
}
polo() {
	cd "$MARCO"
}

Debug errors and capture output

# buggy.sh

#!/usr/bin/env bash

n=$(( RANDOM % 100 ))

if [[ n -eq 42 ]]; then
        echo "Something went wrong"
        >&2 echo "The error was using magic numbers"
        exit 1
fi

echo "Everything went according to plan"

# debug_while.sh
#!/usr/bin/env bash
count=1

while true
do
        ./buggy.sh &>> out.log
        if [[ $? -ne 0 ]]; then
                # cat out.log
                echo "failed after $count times"
                break
        fi
        ((count++))
done

# debug_for.sh
#!/usr/bin/env bash
for ((count=1;;count++))
do
        ./buggy.sh &>> out.log
        if [[ $? -ne 0 ]]; then
                # cat out.log
                echo "failed after $count times"
                break
        fi
done

# debug_until.sh
#!/usr/bin/env bash
count=0
until [[ "$?" -ne 0 ]];
do
        count=$((count+1))
        ./buggy.sh &>> out.log
done
# cat out.log
echo "found error after $count runs"

Recursively find all HTML files in a folder and compress them into a zip file

mkdir -p html_root/html
touch html_root/{1..10}.html
touch html_root/html/xxxx.html
fd --extension html | xargs -d '\n' tar -cvzf html.zip

Write a command or script to recursively find the most recently used file in a folder

find -type f -print0 | xargs -0 ls -lt | head -1
# find -print0 appends a NULL character after each result of the query, replacing the default newline character
# xargs -0 uses NULL as the delimiter for xargs command
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.