updated project 2

This commit is contained in:
Hugh Ratsch
2025-02-09 03:49:04 -06:00
parent 7f1c10a5a8
commit 7835d488ee
+109 -47
View File
@@ -13,6 +13,15 @@ CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
# Challenge definitions
declare -A CHALLENGES=(
["1.1"]="Find all .conf files modified in the last 24 hours and save the list to recent_configs.txt|Try using find with -mtime|Find command with time filters"
["1.2"]="Generate a report showing count of files by extension|Use find and awk together|File type counting"
["1.3"]="Create directories for different file types and sort files|Look into file command|File organization"
["1.4"]="Find and list all empty files and zero-byte files|Use find with size option|File size operations"
["1.5"]="Find and handle duplicate files using md5sum|Consider sort and uniq|Duplicate handling"
)
# Error handling
set -o errexit # Exit on error
set -o nounset # Exit on undefined variable
@@ -173,6 +182,53 @@ validate_challenge_id() {
fi
}
# Function to get challenge description
get_challenge_description() {
local challenge_id=$1
if [[ -n "${CHALLENGES[$challenge_id]:-}" ]]; then
echo "${CHALLENGES[$challenge_id]%%|*}"
else
echo "Challenge not found"
fi
}
# Function to get challenge hint
get_challenge_hint() {
local challenge_id=$1
if [[ -n "${CHALLENGES[$challenge_id]:-}" ]]; then
local hint=$(echo "${CHALLENGES[$challenge_id]}" | cut -d'|' -f2)
echo -e "${YELLOW}Hint: ${hint}${NC}"
fi
}
# Function to verify challenge
verify_challenge() {
local challenge_id=$1
case "$challenge_id" in
"1.1")
if [[ -f "$CHALLENGE_DIR/recent_configs.txt" ]]; then
local count=$(wc -l < "$CHALLENGE_DIR/recent_configs.txt")
if [[ $count -gt 0 ]]; then
echo -e "${GREEN}Success! You found $count configuration files.${NC}"
return 0
fi
fi
echo -e "${RED}Not quite right. Make sure to save the list to recent_configs.txt${NC}"
;;
"1.2")
if [[ -f "$CHALLENGE_DIR/file_report.txt" ]]; then
echo -e "${GREEN}Report generated successfully!${NC}"
return 0
fi
echo -e "${RED}No report file found. Create file_report.txt with the counts${NC}"
;;
*)
echo -e "${RED}Verification not implemented for this challenge yet${NC}"
;;
esac
return 1
}
# Function to handle commands with improved feedback
handle_command() {
local cmd="$1"
@@ -181,19 +237,17 @@ handle_command() {
case "$cmd" in
"verify"|"verify-challenge"|"v")
echo -e "${CYAN}Verifying your solution...${NC}"
"$CHALLENGE_DIR/.tracking/verify-challenge.sh" "$@"
verify_challenge "$CURRENT_CHALLENGE_ID"
;;
"hint"|"show-hint"|"h")
echo -e "${CYAN}Getting hint...${NC}"
"$CHALLENGE_DIR/.tracking/show-hint.sh" "$@"
get_challenge_hint "$CURRENT_CHALLENGE_ID"
;;
"status"|"show-status"|"s")
echo -e "${CYAN}Showing status...${NC}"
"$CHALLENGE_DIR/.tracking/show-status.sh" "$@"
;;
"progress"|"check-progress"|"p")
echo -e "${CYAN}Checking progress...${NC}"
"$CHALLENGE_DIR/.tracking/check-progress.sh" "$@"
echo -e "${CYAN}Current Challenge Status:${NC}"
echo -e "Challenge: $CURRENT_CHALLENGE_ID"
echo -e "Task: $(get_challenge_description "$CURRENT_CHALLENGE_ID")"
echo -e "Working Directory: $CHALLENGE_DIR"
;;
"clear"|"cls"|"c")
clear
@@ -209,12 +263,20 @@ handle_command() {
"help"|"?")
print_help_menu
;;
"ls"|"pwd"|"cd"|"find"|"grep"|"awk"|"sed"|"cat"|"touch"|"mkdir"|"rm"|"mv"|"cp")
# Execute actual command
$cmd "$@"
;;
"")
# Empty command, just continue
;;
*)
if command -v "$cmd" >/dev/null 2>&1; then
$cmd "$@"
else
echo -e "${RED}Unknown command: $cmd${NC}"
echo -e "Type ${YELLOW}help${NC} or ${YELLOW}?${NC} for available commands"
fi
;;
esac
return 0
@@ -226,7 +288,6 @@ print_help_menu() {
echo -e " ${YELLOW}verify, v${NC} - Check your solution"
echo -e " ${YELLOW}hint, h${NC} - Get a hint"
echo -e " ${YELLOW}status, s${NC} - Show challenge status"
echo -e " ${YELLOW}progress, p${NC} - Check overall progress"
echo -e " ${YELLOW}clear, c${NC} - Clear screen"
echo -e " ${YELLOW}reset, r${NC} - Reset current challenge"
echo -e " ${YELLOW}help, ?${NC} - Show this help message"
@@ -237,17 +298,6 @@ print_help_menu() {
echo -e "- Use arrow keys for command history"
}
# Function to get challenge description
get_challenge_description() {
local challenge_id=$1
if [[ -n "${CHALLENGES[$challenge_id]:-}" ]]; then
IFS='|' read -r _ _ _ _ task _ _ _ <<< "${CHALLENGES[$challenge_id]}"
echo "$task"
else
echo "Challenge not found"
fi
}
# Function to print challenge header
print_challenge_header() {
local challenge_id=$1
@@ -282,7 +332,30 @@ setup_challenge_env() {
done
done
;;
# Add more challenge setups as needed
"1.3")
# Create mixed files for organization
local files=("doc1.txt" "script.py" "data.json" "config.yml" "log.log" "notes.md")
for file in "${files[@]}"; do
echo "content" > "$CHALLENGE_DIR/files/$file"
done
;;
"1.4")
# Create mix of empty and non-empty files
for i in {1..10}; do
if ((i % 2 == 0)); then
touch "$CHALLENGE_DIR/files/empty$i.txt"
else
echo "content" > "$CHALLENGE_DIR/files/file$i.txt"
fi
done
;;
"1.5")
# Create some duplicate files
for i in {1..5}; do
echo "unique content $i" > "$CHALLENGE_DIR/files/file$i.txt"
cp "$CHALLENGE_DIR/files/file$i.txt" "$CHALLENGE_DIR/files/file$i.copy.txt"
done
;;
esac
echo -e "${GREEN}Challenge environment ready!${NC}"
@@ -312,12 +385,9 @@ start_challenge() {
export CURRENT_CHALLENGE_ID=$challenge_id
# Validate challenge ID
if ! validate_challenge_id "$challenge_id"; then
return 1
fi
# Source required scripts with error checking
if ! source_required_scripts; then
if [[ ! $challenge_id =~ ^[1-9]\.[1-5]$ ]]; then
echo -e "${RED}Error: Invalid challenge ID format${NC}"
echo -e "${YELLOW}Tip: Challenge ID should be in format 'level.number' (e.g., 1.1)${NC}"
return 1
fi
@@ -329,17 +399,13 @@ start_challenge() {
return 1
fi
if ! get_challenge_details "$challenge_id" 2>/dev/null; then
if [[ -z "${CHALLENGES[$challenge_id]:-}" ]]; then
echo -e "${RED}Error: Challenge $challenge_id not found${NC}"
echo -e "${YELLOW}Available challenges:${NC}"
list_challenges
return 1
fi
# Record challenge start
local start_time=$(date +%s)
echo "$challenge_id,$start_time" > "$CHALLENGE_DIR/.tracking/current_challenge"
print_challenge_header "$challenge_id"
# Enable command history and tab completion
@@ -357,7 +423,7 @@ start_challenge() {
while true; do
cmd_count=$((cmd_count + 1))
echo -ne "\n${GREEN}challenge${BLUE}[$challenge_id]${GREEN}>${NC} "
read -e -r cmd args || true # Add || true to prevent exit on Ctrl+D
read -e -r cmd args || true
# Handle Ctrl+D (EOF)
if [ $? -ne 0 ]; then
@@ -374,16 +440,19 @@ start_challenge() {
fi
done
# Record challenge end
local end_time=$(date +%s)
local duration=$((end_time - start_time))
echo "$challenge_id,$duration" >> "$CHALLENGE_DIR/.tracking/data/time.csv"
echo -e "\n${GREEN}Challenge session ended!${NC}"
echo -e "Time spent: $(printf '%dm %ds' $((duration/60)) $((duration%60)))"
echo -e "Commands used: $cmd_count"
}
# Function to list available challenges
list_challenges() {
echo -e "\n${BLUE}Available Challenges:${NC}"
for challenge_id in "${!CHALLENGES[@]}"; do
echo -e "\n${YELLOW}Challenge $challenge_id${NC}"
echo "Task: $(get_challenge_description "$challenge_id")"
done
}
# Function to show help
show_help() {
cat << EOF
@@ -427,13 +496,6 @@ main() {
start_challenge "$2"
;;
"list")
if [[ ! -f "$CHALLENGE_DIR/.tracking/challenges.sh" ]]; then
setup_environment
fi
if ! source "$CHALLENGE_DIR/.tracking/challenges.sh" 2>/dev/null; then
echo -e "${RED}Error: Failed to load challenges${NC}"
return 1
fi
list_challenges
;;
"help"|"--help"|"-h")
@@ -448,7 +510,7 @@ main() {
}
# Run main with error handling
if ! main "${@:-}"; then
if ! main "$@"; then
echo -e "${RED}Command failed. See errors above.${NC}"
exit 1
fi