From eb4c07a64fe9366ba4de62ccb4a4c6f994889b67 Mon Sep 17 00:00:00 2001 From: zakary Date: Tue, 23 Jul 2024 08:11:41 -0500 Subject: [PATCH] fix(shellcheck): problems with env and script syntax --- backup_config.sh | 19 +++++++------------ dupe.sh | 20 ++++++++++++++++---- f2b-dump.sh | 3 ++- merge_folders.py | 9 +++++++-- pic-update.sh | 3 ++- qbm-qbit.sh | 18 ++++++++++++++++-- xseed.sh | 5 +++-- xseed_qbit_cat_filter.sh | 23 ++++++++++------------- zfsburn.sh | 21 ++++++++++++++++----- 9 files changed, 79 insertions(+), 42 deletions(-) diff --git a/backup_config.sh b/backup_config.sh index 5c55976..564aa35 100755 --- a/backup_config.sh +++ b/backup_config.sh @@ -20,23 +20,18 @@ EXCLUDE_PATTERNS=( '--exclude=plexmediaserver/*' ) -# Create the archive in /tmp -tar -czvf "$TEMP_BACKUP_DIR/$ARCHIVE_NAME" "${EXCLUDE_PATTERNS[@]}" -C "$SOURCE_DIR" . > "$LOG_FILE" 2>&1 +# Create the archive in /tmp and check if the archive was created successfully +if tar -czvf "$TEMP_BACKUP_DIR/$ARCHIVE_NAME" "${EXCLUDE_PATTERNS[@]}" -C "$SOURCE_DIR" . >"$LOG_FILE" 2>&1; then + echo "Archive created successfully: $TEMP_BACKUP_DIR/$ARCHIVE_NAME" >>"$LOG_FILE" -# Check if the archive was created successfully -if [[ $? -eq 0 ]]; then - echo "Archive created successfully: $TEMP_BACKUP_DIR/$ARCHIVE_NAME" >> "$LOG_FILE" - # Sync the archive to the remote backup directory - rsync -av "$TEMP_BACKUP_DIR/$ARCHIVE_NAME" "$REMOTE_BACKUP_DIR" >> "$LOG_FILE" 2>&1 - - if [[ $? -eq 0 ]]; then - echo "Backup synced successfully: $REMOTE_BACKUP_DIR/$ARCHIVE_NAME" >> "$LOG_FILE" + if rsync -av "$TEMP_BACKUP_DIR/$ARCHIVE_NAME" "$REMOTE_BACKUP_DIR" >>"$LOG_FILE" 2>&1; then + echo "Backup synced successfully: $REMOTE_BACKUP_DIR/$ARCHIVE_NAME" >>"$LOG_FILE" # Optionally, remove the local archive after successful sync rm "$TEMP_BACKUP_DIR/$ARCHIVE_NAME" else - echo "Failed to sync the backup to the remote server" >> "$LOG_FILE" + echo "Failed to sync the backup to the remote server" >>"$LOG_FILE" fi else - echo "Failed to create the archive" >> "$LOG_FILE" + echo "Failed to create the archive" >>"$LOG_FILE" fi diff --git a/dupe.sh b/dupe.sh index 0da6d92..d0a6c57 100755 --- a/dupe.sh +++ b/dupe.sh @@ -1,12 +1,24 @@ #!/bin/bash -# Load environment variables from .env file # Load environment variables from .env file if it exists -if [ -f ".env" ]; then +# in the same directory as this bash script + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ENV_PATH="$SCRIPT_DIR/.env" +if [ -f "$ENV_PATH" ]; then # shellcheck source=.env - source ".env" - echo "using .env file" + echo "Loading environment variables from $ENV_PATH file" + # shellcheck disable=SC1090 # shellcheck sucks + if source "$ENV_PATH"; then + echo "Environment variables loaded successfully" + else + echo "Error loading environment variables" >&2 + exit 1 + fi +else + echo ".env file not found in script directory ($ENV_PATH)" fi + # Variables JDUPES_OUTPUT_LOG=${JDUPES_OUTPUT_LOG:-"/mnt/data/jdupes.log"} JDUPES_SOURCE_DIR=${JDUPES_SOURCE_DIR:-"/mnt/data/media/"} diff --git a/f2b-dump.sh b/f2b-dump.sh index 4281237..a1cfc59 100755 --- a/f2b-dump.sh +++ b/f2b-dump.sh @@ -5,6 +5,7 @@ temp_file=$(mktemp) # Function to add content to the temporary file add_content() { + # shellcheck disable=SC2129 echo -e "\n$1\n" >>"$temp_file" cat "$2" >>"$temp_file" 2>/dev/null echo -e "\n" >>"$temp_file" @@ -34,7 +35,7 @@ done # Upload to termbin echo "Uploading to Termbin..." >>"$temp_file" -cat "$temp_file" | nc termbin.com 9999 +nc termbin.com 9999 <"$temp_file" # Cleanup rm "$temp_file" diff --git a/merge_folders.py b/merge_folders.py index 489d62c..1bd2748 100644 --- a/merge_folders.py +++ b/merge_folders.py @@ -3,7 +3,10 @@ import logging # Setup logging -logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') +logging.basicConfig( + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" +) + def merge_directories(src, dst): """ @@ -12,7 +15,7 @@ def merge_directories(src, dst): for item in os.listdir(src): src_path = os.path.join(src, item) dst_path = os.path.join(dst, item) - + if os.path.isdir(src_path): # If it's a directory, recurse into it if not os.path.exists(dst_path): @@ -28,6 +31,7 @@ def merge_directories(src, dst): else: logging.info(f"File skipped (already exists): {dst_path}") + def atomic_moves(source_directories, target_directory): """ Handles atomic moving from multiple source directories to a single target directory. @@ -40,6 +44,7 @@ def atomic_moves(source_directories, target_directory): except Exception as e: logging.error(f"Error during moving process from {src}: {e}") + # Example use case (commented out for safety - remove "# " to uncomment): # source_dirs = ['/mnt/data/media/tv-slade', '/mnt/data/media/tv-tmp'] # target_dir = '/mnt/data/media/tv' diff --git a/pic-update.sh b/pic-update.sh index a0c5095..b75b479 100755 --- a/pic-update.sh +++ b/pic-update.sh @@ -16,7 +16,8 @@ CURRENT_UID=$(id -u) # Check if Plex-Image-Cleanup is installed and the current user owns it check_pic_installation() { if [ -d "$PIC_PATH" ]; then - local pic_repo_owner=$(stat -c '%u' "$PIC_PATH") + local pic_repo_owner + pic_repo_owner=$(stat -c '%u' "$PIC_PATH") if [ "$pic_repo_owner" != "$CURRENT_UID" ]; then echo "You do not own the Plex-Image-Cleanup repo. Please run this script as the user that owns the repo [$pic_repo_owner]." exit 1 diff --git a/qbm-qbit.sh b/qbm-qbit.sh index bca82ad..795b896 100755 --- a/qbm-qbit.sh +++ b/qbm-qbit.sh @@ -7,8 +7,22 @@ if ! command -v lockfile &>/dev/null; then fi # Load environment variables from .env file if it exists -if [ -f ".env" ]; then - source ".env" +# in the same directory as this bash script + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ENV_PATH="$SCRIPT_DIR/.env" +if [ -f "$ENV_PATH" ]; then + # shellcheck source=.env + echo "Loading environment variables from $ENV_PATH file" + # shellcheck disable=SC1090 # shellcheck sucks + if source "$ENV_PATH"; then + echo "Environment variables loaded successfully" + else + echo "Error loading environment variables" >&2 + exit 1 + fi +else + echo ".env file not found in script directory ($ENV_PATH)" fi # Use environment variables with descriptive default values diff --git a/xseed.sh b/xseed.sh index 6e24e4e..b4b8fbe 100644 --- a/xseed.sh +++ b/xseed.sh @@ -14,8 +14,8 @@ ENV_PATH="$SCRIPT_DIR/.env" if [ -f "$ENV_PATH" ]; then # shellcheck source=.env echo "Loading environment variables from $ENV_PATH file" - source "$ENV_PATH" - if [ $? -eq 0 ]; then + # shellcheck disable=SC1090 # shellcheck sucks + if source "$ENV_PATH"; then echo "Environment variables loaded successfully" else echo "Error loading environment variables" >&2 @@ -31,6 +31,7 @@ USENET_CLIENT_NAME=${USENET_CLIENT_NAME:-SABnzbd} XSEED_HOST=${XSEED_HOST:-crossseed} XSEED_PORT=${XSEED_PORT:-8080} LOG_FILE=${LOG_FILE:-/config/xseed.log} +# shellcheck disable=SC2269 XSEED_APIKEY=${XSEED_APIKEY} # Function to log messages diff --git a/xseed_qbit_cat_filter.sh b/xseed_qbit_cat_filter.sh index f638ec1..1d219c1 100644 --- a/xseed_qbit_cat_filter.sh +++ b/xseed_qbit_cat_filter.sh @@ -29,7 +29,6 @@ FILTERED_CAT="CAT1|CAT2|CAT3" # Ex) tracker.announce.com|tracker.announce2.com FILTERED_TRACKER="tracker1.announce.com|tracker2.announce.com" - log() { echo -e "${0##*/}: $1" } @@ -39,13 +38,13 @@ log_err() { } cross_seed_request() { - local data="$1" - local headers=(-X POST "$XSEED_URL" --data-urlencode "$data") - if [ -n "$XSEED_API_KEY" ]; then - headers+=(-H "X-Api-Key: $XSEED_API_KEY") - fi - response=$(curl --silent --output /dev/null --write-out "%{http_code}" "${headers[@]}") - echo "$response" + local data="$1" + local headers=(-X POST "$XSEED_URL" --data-urlencode "$data") + if [ -n "$XSEED_API_KEY" ]; then + headers+=(-H "X-Api-Key: $XSEED_API_KEY") + fi + response=$(curl --silent --output /dev/null --write-out "%{http_code}" "${headers[@]}") + echo "$response" } if [[ -z "$TORRENT_PATH" ]]; then @@ -56,14 +55,12 @@ elif [[ -z "$TORRENT_CAT" ]]; then log_err "Category not specified for $TORRENT_PATH" fi - - if [[ -n "$FILTERED_CAT" ]] && [[ "$TORRENT_CAT" =~ ^($FILTERED_CAT)$ ]]; then log "[\033[1m$TORRENT_NAME\033[0m] [$TORRENT_CAT]" - xseed_resp=$(cross_seed_request "infoHash=$TORRENT_INFOHASH"); - [ "$xseed_resp" != "204" ] && sleep 30 && xseed_resp=$(cross_seed_request "path=$TORRENT_PATH") + xseed_resp=$(cross_seed_request "infoHash=$TORRENT_INFOHASH") + [ "$xseed_resp" != "204" ] && sleep 30 && xseed_resp=$(cross_seed_request "path=$TORRENT_PATH") elif [[ -n "$FILTERED_TRACKER" ]] && [[ "$TORRENT_TRACKER" =~ ($FILTERED_TRACKER) ]]; then log "[\033[1m$TORRENT_NAME\033[0m] [$TORRENT_TRACKER]" - xseed_resp=$(cross_seed_request "infoHash=$TORRENT_INFOHASH"); + xseed_resp=$(cross_seed_request "infoHash=$TORRENT_INFOHASH") [ "$xseed_resp" != "204" ] && sleep 30 && xseed_resp=$(cross_seed_request "path=$TORRENT_PATH") fi diff --git a/zfsburn.sh b/zfsburn.sh index 807e3c4..365e90b 100755 --- a/zfsburn.sh +++ b/zfsburn.sh @@ -1,12 +1,23 @@ #!/bin/bash -# Load .env file -set -o allexport -if [ -f ".env" ]; then +# Load environment variables from .env file if it exists +# in the same directory as this bash script + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ENV_PATH="$SCRIPT_DIR/.env" +if [ -f "$ENV_PATH" ]; then # shellcheck source=.env - source ".env" + echo "Loading environment variables from $ENV_PATH file" + # shellcheck disable=SC1090 # shellcheck sucks + if source "$ENV_PATH"; then + echo "Environment variables loaded successfully" + else + echo "Error loading environment variables" >&2 + exit 1 + fi +else + echo ".env file not found in script directory ($ENV_PATH)" fi -set +o allexport0 VERBOSE=${VERBOSE:-1} MAX_FREQ=${MAX_FREQ:-4}