Skip to content

Instantly share code, notes, and snippets.

@olegon
Created February 9, 2023 01:25
Show Gist options
  • Save olegon/eb2964225dedad096a0823bac3dc4dc1 to your computer and use it in GitHub Desktop.
Save olegon/eb2964225dedad096a0823bac3dc4dc1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
: <<'END_COMMENT'
Read man ls and write an ls command that lists files in the following manner
Includes all files, including hidden files
Sizes are listed in human readable format (e.g. 454M instead of 454279954)
Files are ordered by recency
Output is colorized
END_COMMENT
ls -alht --color /tmp
#!/usr/bin/env bash
: <<'END_COMMENT'
Write bash functions marco and polo that do the following. Whenever you execute marco the
current working directory should be saved in some manner, then when you execute polo, no
matter what directory you are in, polo should cd you back to the directory where you
executed marco.
For ease of debugging you can write the code in a file marco.sh and (re)load the definitions
to your shell by executing source marco.sh.
END_COMMENT
marco() {
OG_MARCO_DIRECTORY=$(pwd)
echo "Use polo command to enter $OG_MARCO_DIRECTORY directory."
}
polo() {
if [ -n "$OG_MARCO_DIRECTORY" ]; then
echo "Entering $OG_MARCO_DIRECTORY directory."
cd "$OG_MARCO_DIRECTORY" || exit
else
>&2 echo "OG_MARCO_DIRECTORY environment variable not found."
fi
}
#!/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"
#!/usr/bin/env bash
: <<'END_COMMENT'
Say you have a command that fails rarely. In order to debug it you need to capture its output
but it can be time consuming to get a failure run. Write a bash script that runs the following
script until it fails and captures its standard output and error streams to files and prints
everything at the end. Bonus points if you can also report how many runs it took for the script
to fail.
END_COMMENT
COMMAND="$1"
STDOUT_FILE="/tmp/og-$$-stdout.txt"
STDERR_FILE="/tmp/og-$$-stderr.txt"
echo "Executing $COMMAND until it fails."
while "$COMMAND" > "$STDOUT_FILE" 2> "$STDERR_FILE"; do :; done;
echo "stdout = $(cat $STDOUT_FILE)"
echo "stderr = $(cat $STDERR_FILE)"
#!/usr/bin/env bash
: <<'END_COMMENT'
As we covered in the lecture find’s -exec can be very powerful for performing operations over
the files we are searching for. However, what if we want to do something with all the files,
like creating a zip file? As you have seen so far commands will take input from both arguments
and STDIN. When piping commands, we are connecting STDOUT to STDIN, but some commands like tar
take inputs from arguments. To bridge this disconnect there’s the xargs command which will
execute a command using STDIN as arguments. For example ls | xargs rm will delete the files in
the current directory.
Your task is to write a command that recursively finds all HTML files in the folder and makes
a zip with them. Note that your command should work even if the files have spaces (hint: check
-d flag for xargs).
If you’re on macOS, note that the default BSD find is different from the one included in GNU
coreutils. You can use -print0 on find and the -0 flag on xargs. As a macOS user, you should be
aware that command-line utilities shipped with macOS may differ from the GNU counterparts; you can
install the GNU versions if you like by using brew.
END_COMMENT
# find => procura os arquivos e joga para STDOUT
# -print0 => usa o caractere 0 (NULL) para separar o nome dos arquivos
# xargs => pega o STDIN e usa como argumentos do próximo comando, necessário neste caso porque comando zip trabalha com argumentos
# -0 => usa NULL como separador do nome dos arquivos
# -print0 e -0 são necessários para trabalhar corretamente com arquivos que possuem espaço no nome
find . -iname '*.html' -type f -print0 | xargs -0 zip all-html-files.zip
#!/usr/bin/env bash
: <<'END_COMMENT'
(Advanced) Write a command or script to recursively find the most recently modified
file in a directory. More generally, can you list all files by recency?
END_COMMENT
find "$1" -type d -exec echo '{}' \; | while read -r dir; do
MOST_RECENT_FILE=$(ls -t "$dir" | head -n 1)
echo "$dir: $MOST_RECENT_FILE"
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment