RobertW
RobertW

Reputation: 156

How do I check if nested if is not equal to 0 bash

I have a PC that downloads torrents and moves completed files to a LAN server then continues seeding them. When it's done, the .parts file is removed. At this point I'd like it to move everything to the correct directories. Removing the 2 extra if statements works fine for that but I'd rather not try to run the commands if the files don't exist.

I've tried a few different things in trying to condense this script as much as I can without running useless parts if not needed. I've tried using the following (and more):

if [ "$count2" != 0 ]
if [ "$count2" -ne 0 ]
if [ "$count2" -gt 0 ]

Last working code:

#!/bin/bash

count=`ls -1 *.parts 2>/var/www/html/uploads/Videos/AutoTorrents/ | wc -l`
if [ $count == 0 ]
then
        mv /var/www/html/uploads/Videos/AutoTorrents/*.{mkv,mp4,m4v,avi,txt} /var/www/html/uploads/Videos/Movies/!New_Movies/
        mv /var/www/html/uploads/Videos/AutoTorrents/*.{srt,sub} /var/www/html/uploads/Videos/Movies/!Subtitles/
        mv /var/www/html/uploads/Videos/AutoTorrents/*.{png,jpg,jpeg} /var/www/html/images/
fi

What the non-working code I'm trying to get to work looks like currently:

#!/bin/bash

count=$(find *.parts 2>/var/www/html/uploads/Videos/AutoTorrents/ | wc -l)
count2=$(find *.{srt,sub} 2>/var/www/html/uploads/Videos/AutoTorrents/ | wc -l)
count3=$(find *.{png,jpg,jpeg} 2>/var/www/html/uploads/Videos/AutoTorrents/ | wc -l)
if [ "$count" == 0 ]
then
        mv /var/www/html/uploads/Videos/AutoTorrents/*.{mkv,mp4,m4v,avi} /var/www/html/uploads/Videos/Movies/!New_Movies/
        if [ "$count2" == 0 ]
            then
                :
            else mv /var/www/html/uploads/Videos/AutoTorrents/*.{srt,sub,txt} /var/www/html/uploads/Videos/Movies/!Subtitles/
        fi
        if [ "$count3" == 0 ]
            then
                :
            else mv /var/www/html/uploads/Videos/AutoTorrents/*.{png,jpg,jpeg} /var/www/html/images/
        fi
fi

Upvotes: 1

Views: 127

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295619

The problem here is that your find invocations are all buggy, but because you're redirecting stderr (to a directory, which isn't able to store it), you never see the error message describing the bug, and thus never find out what went wrong.

Consider the following instead:

dir=/var/www/html/uploads/Videos/AutoTorrents/
count=$(find "$dir" -name '*.parts' | wc -l)
count2=$(find "$dir" '(' -name '*.srt' -o -name '*.sub' ')' -print | wc -l)
count3=$(find "$dir" '(' -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' ')' -print | wc -l)

...or, even better, follow the advice in BashFAQ #4, and rely on nullglob to make sure globs with no matches expand to empty lists, and globstar to enable recursion. The below also uses GNU xargs to split up lists of files into lengths mv is guaranteed to be able to successfully handle (also relying on the GNU mv extension -t to put the destination before the list of files to be moved, rather than at the end):

#!/usr/bin/env bash
case $BASH_VERSION in ''|[0-3].*) echo "ERROR: Bash 4.0+ needed" >&2; exit 1;; esac

dir=/var/www/html/uploads/Videos/AutoTorrents/
movDest=/var/www/html/uploads/Videos/Movies/
imgDest=/var/www/html/images/

shopt -s nullglob globstar

parts=( "$dir"/**/*.parts )
if (( ${#parts[@]} )); then
  exit 0 # do not run when any *.parts files exist
fi

movs=( "$dir"/**/*.{mkv,mp4,m4v,avi} )
if (( ${#movs[@]} )); then
  printf '%s\0' "${movs[@]}" | xargs -0r mv -t "$movDest/!New_Movies" --
fi

subs=( "$dir"/**/*.{srt,sub} )
if (( ${#subs[@]} )); then
  printf '%s\0' "${subs[@]}" | xargs -0r mv -t "$movDest/!Subtitles" --
fi

imgs=( "$dir"/**/*.{png,jpg,jpeg} )
if (( ${#imgs[@]} )); then
  printf '%s\0' "${imgs[@]}" | xargs -0r mv -t "$imgDest" --
fi

Upvotes: 2

Related Questions