Skip to content

Instantly share code, notes, and snippets.

@Bugaddr
Created May 26, 2024 17:25
Show Gist options
  • Save Bugaddr/39218fe02fcb860d0a34729fb9d8a364 to your computer and use it in GitHub Desktop.
Save Bugaddr/39218fe02fcb860d0a34729fb9d8a364 to your computer and use it in GitHub Desktop.
Shell script for printing whatis for executables in /usr/bin folder randomly, just to make you curious and maybe you will find some new command :)
#!/usr/bin/env bash
# File to store previously printed commands
printed_commands_file=~/.local/share/daily-whatis/databases/printed_commands_db.txt
# Check if the file exists, if not create it
if [ ! -f "$printed_commands_file" ]; then
mkdir -p "$(dirname "$printed_commands_file")" # Ensure directory exists
touch "$printed_commands_file" # Create the file
fi
# Function to select a random command from /usr/bin folder
select_random_command() {
local commands=("/usr/bin"/*)
if [ ${#commands[@]} -eq 1 ]; then
echo "Error: No commands found in /usr/bin directory."
exit 1
fi
local random_index=$((RANDOM % ${#commands[@]}))
echo "${commands[$random_index]}"
}
# Function to get the whatis entry for a command
get_whatis_entry() {
local command="$1"
whatis "$(basename "$command")"
}
# Read the file into an array
readarray -t printed_commands < "$printed_commands_file"
# Select a random command and ensure it's not printed before
while true; do
random_command=$(select_random_command)
# Check if the command is not already printed
if [[ ! " ${printed_commands[@]} " =~ " ${random_command} " ]]; then
whatis_entry=$(get_whatis_entry "$random_command")
# Print the whatis entry
echo "Todays whatis: $whatis_entry"
# Add the command to printed commands list
echo "$random_command" >> "$printed_commands_file"
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment