Rob Berry
Rob Berry

Reputation: 315

variable from bash 'if echo grep'

I'm using the following code to sort recordings and captures from my CCTV into dated sub directories that are more than a day old.

Is there any way to reference the results of the echo grep date section of the IF code as a variable so that it can be used when creating a folder?

I could just create a variable on a separate line with another use of grep (or define the variable and reference it in the IF statement) but would prefer if it were all on one line.

File names are similar to 'MDAlarm_20160417-120925.jpg' so the contents of the variable would be 2016-04-17.

for f in ~/FI9803P_00626E5755DE/**/*;
do
    if [[ `echo $f | grep -oP '\d{8}' | date -f - +'%Y-%m-%d'` != `date +'%Y-%m-%d'` ]]; then
        mkdir -p $(dirname $f)/$varFromIF; echo "Made it" #mv $f $(dirname $f)/$varFromIF
    fi
done

Upvotes: 1

Views: 991

Answers (1)

Avihoo Mamka
Avihoo Mamka

Reputation: 4786

Yes. You can do it like this:

for f in ~/FI9803P_00626E5755DE/**/*;
do
    if [[ "${varFromIF=$(echo $f | grep -oP '\d{8}' | date -f - +'%Y-%m-%d')}" != $(date +'%Y-%m-%d') ]]; then
        mkdir -p $(dirname $f)/$varFromIF; echo "Made it" #mv $f $(dirname $f)/$varFromIF
    fi
done

Upvotes: 1

Related Questions