Skip to content

Instantly share code, notes, and snippets.

@ThaiDat
Last active July 31, 2023 10:36
Show Gist options
  • Select an option

  • Save ThaiDat/1f7107376b0f95507808165df965782b to your computer and use it in GitHub Desktop.

Select an option

Save ThaiDat/1f7107376b0f95507808165df965782b to your computer and use it in GitHub Desktop.

Revisions

  1. ThaiDat revised this gist Jul 31, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions clean.sh
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ if [ -z ${CL_TARGET_DIR+x} ]; then
    CL_TARGET_DIR="${1:-$(pwd)}"
    fi

    # CL_VERBOSE: Set to 1 to print log
    if [ -z ${CL_VERBOSE+x} ]; then
    CL_VERBOSE=0
    fi
    @@ -32,6 +33,7 @@ done

    for cl_file in $(find "$CL_TARGET_DIR" "${CL_PRUNE_DIRS[@]}" "${CL_INCLUDE_FILES[@]}" -false); do

    # Verbose
    if [ ${CL_VERBOSE} == 1 ]; then
    echo "${cl_file}"
    fi
  2. ThaiDat revised this gist Jul 31, 2023. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions clean.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # CL_FILE_EXTENSIONS: The script will apply on files with given extensions
    if [ -z ${CL_FILE_EXTENSIONS+x} ]; then
    CL_FILE_EXTENSIONS=(txt md json xml yaml yml sh bat inf)
    CL_FILE_EXTENSIONS=(txt md json xml yaml yml sh bat)
    fi

    # CL_IGNORED_SUB_DIR: The script will ignore files in given directories
    @@ -13,6 +13,10 @@ if [ -z ${CL_TARGET_DIR+x} ]; then
    CL_TARGET_DIR="${1:-$(pwd)}"
    fi

    if [ -z ${CL_VERBOSE+x} ]; then
    CL_VERBOSE=0
    fi

    # Prepare parameters for find command to ignore directories in CL_IGNORED_SUB_DIR
    CL_PRUNE_DIRS=()
    for cl_dir in "${CL_IGNORED_SUB_DIR[@]}"; do
    @@ -27,8 +31,11 @@ done


    for cl_file in $(find "$CL_TARGET_DIR" "${CL_PRUNE_DIRS[@]}" "${CL_INCLUDE_FILES[@]}" -false); do
    #echo "${cl_file}"

    if [ ${CL_VERBOSE} == 1 ]; then
    echo "${cl_file}"
    fi

    # Remove trailing whitespaces and tab characters
    sed -i 's/[ \t]*$//' "$cl_file"

    @@ -37,4 +44,4 @@ for cl_file in $(find "$CL_TARGET_DIR" "${CL_PRUNE_DIRS[@]}" "${CL_INCLUDE_FILES

    # Remove empty lines at the end
    sed -i -Ez '$ s/\n+$//' "$cl_file"
    done
    done
  3. ThaiDat revised this gist Jul 14, 2023. 1 changed file with 20 additions and 13 deletions.
    33 changes: 20 additions & 13 deletions clean.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # CL_FILE_EXTENSIONS: The script will apply on files with given extensions
    if [ -z ${CL_FILE_EXTENSIONS+x} ]; then
    CL_FILE_EXTENSIONS=(txt md json xml yaml yml sh bat)
    CL_FILE_EXTENSIONS=(txt md json xml yaml yml sh bat inf)
    fi

    # CL_IGNORED_SUB_DIR: The script will ignore files in given directories
    @@ -10,24 +10,31 @@ fi

    # CL_TARGET_DIR: The directory that the scripts will work on.
    if [ -z ${CL_TARGET_DIR+x} ]; then
    CL_TARGET_DIR="${1:-$(pwd)}"
    CL_TARGET_DIR="${1:-$(pwd)}"
    fi

    # Prepare parameters for find command to ignore directories in CL_IGNORED_SUB_DIR
    CL_PRUNE_DIRS=()
    for cl_dir in "${CL_IGNORED_SUB_DIR[@]}"; do
    CL_PRUNE_DIRS+=("-name" "$cl_dir" "-prune" "-o")
    CL_PRUNE_DIRS+=("-type" "f" "-name" "$cl_dir" "-prune" "-o")
    done

    # Prepare parameters for find command to include given extensions in CL_FILE_EXTENSIONS
    CL_INCLUDE_FILES=()
    for cl_ext in "${CL_FILE_EXTENSIONS[@]}"; do
    CL_INCLUDE_FILES+=("-type" "f" "-name" "*.${cl_ext}" "-o")
    done

    for cl_file in $(find "$CL_TARGET_DIR" "${CL_PRUNE_DIRS[@]}" -type f); do
    if [[ "${CL_FILE_EXTENSIONS[@]}" =~ "${cl_file##*.}" ]]; then
    # Remove trailing whitespaces and tab characters
    sed -i 's/[ \t]*$//' "$cl_file"

    # Remove empty lines at the beginning
    sed -i '/./,$!d' "$cl_file"
    for cl_file in $(find "$CL_TARGET_DIR" "${CL_PRUNE_DIRS[@]}" "${CL_INCLUDE_FILES[@]}" -false); do
    #echo "${cl_file}"

    # Remove trailing whitespaces and tab characters
    sed -i 's/[ \t]*$//' "$cl_file"

    # Remove empty lines at the end
    sed -i -Ez '$ s/\n+$//' "$cl_file"
    fi
    done
    # Remove empty lines at the beginning
    sed -i '/./,$!d' "$cl_file"

    # Remove empty lines at the end
    sed -i -Ez '$ s/\n+$//' "$cl_file"
    done
  4. ThaiDat created this gist May 11, 2023.
    33 changes: 33 additions & 0 deletions clean.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # CL_FILE_EXTENSIONS: The script will apply on files with given extensions
    if [ -z ${CL_FILE_EXTENSIONS+x} ]; then
    CL_FILE_EXTENSIONS=(txt md json xml yaml yml sh bat)
    fi

    # CL_IGNORED_SUB_DIR: The script will ignore files in given directories
    if [ -z ${CL_IGNORED_SUB_DIR+x} ]; then
    CL_IGNORED_SUB_DIR=(.git)
    fi

    # CL_TARGET_DIR: The directory that the scripts will work on.
    if [ -z ${CL_TARGET_DIR+x} ]; then
    CL_TARGET_DIR="${1:-$(pwd)}"
    fi

    # Prepare parameters for find command to ignore directories in CL_IGNORED_SUB_DIR
    CL_PRUNE_DIRS=()
    for cl_dir in "${CL_IGNORED_SUB_DIR[@]}"; do
    CL_PRUNE_DIRS+=("-name" "$cl_dir" "-prune" "-o")
    done

    for cl_file in $(find "$CL_TARGET_DIR" "${CL_PRUNE_DIRS[@]}" -type f); do
    if [[ "${CL_FILE_EXTENSIONS[@]}" =~ "${cl_file##*.}" ]]; then
    # Remove trailing whitespaces and tab characters
    sed -i 's/[ \t]*$//' "$cl_file"

    # Remove empty lines at the beginning
    sed -i '/./,$!d' "$cl_file"

    # Remove empty lines at the end
    sed -i -Ez '$ s/\n+$//' "$cl_file"
    fi
    done