Skip to content

Instantly share code, notes, and snippets.

@DanielChuDC
Created April 7, 2024 13:51
Show Gist options
  • Select an option

  • Save DanielChuDC/7ada84db77e8f263914959d2f038e342 to your computer and use it in GitHub Desktop.

Select an option

Save DanielChuDC/7ada84db77e8f263914959d2f038e342 to your computer and use it in GitHub Desktop.

Revisions

  1. DanielChuDC created this gist Apr 7, 2024.
    64 changes: 64 additions & 0 deletions fixed_check_content.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    #!/bin/bash


    # Used an array (target_tools) to store system tools.
    # Used find command to iterate over directory contents safely, with the -mindepth 1 option to avoid processing the directory itself.
    # Properly quoted variables to prevent word splitting and globbing issues.
    # Added -d '' option to read command to handle filenames with spaces correctly.
    # Ensured find command only searches one level deep in directories.


    target_dirs="/opt/"
    target_tools=(
    "scp"
    "ssh"
    )

    function print_report() {
    local input="$1"
    local indent="$2"

    if [ -d "$input" ]; then
    echo "${indent}Contents of directory: $input"
    while IFS= read -r -d '' item; do
    if [ -d "$item" ]; then
    print_report "$item" " $indent"
    elif [ -f "$item" ]; then
    echo "${indent}File: $item"
    echo "${indent}Content:"
    local line_number=1
    while IFS= read -r line; do
    printf "%s%s: %s\n" "$indent" "$line_number" "$line"
    ((line_number++))
    done < "$item"
    echo ""
    echo ""
    fi
    done < <(find "$input" -mindepth 1 -maxdepth 1 -print0)
    elif [ -f "$input" ]; then
    echo "${indent}File: $input"
    echo "${indent}Content:"
    local line_number=1
    while IFS= read -r line; do
    printf "%s%s: %s\n" "$indent" "$line_number" "$line"
    ((line_number++))
    done < "$input"
    echo ""
    echo ""
    else
    echo "Error: Input is neither a file nor a directory"
    fi
    }

    echo "Checking for system tools in target directories..."
    echo ""

    for tool in "${target_tools[@]}"; do
    echo "Searching for tool: $tool"
    echo ""
    for directory in $target_dirs; do
    echo "Directory: $directory"
    echo ""
    print_report "$directory" " "
    done
    done