Home > Mobile >  Loop to compare numbers within file name
Loop to compare numbers within file name

Time:11-02

So I've written a code to compare if a certain number within the file name is bigger than 11 and if it is than it should make a directory.

-->Mainfolder
   -->Jan
      -->Huistaak1-HelloWorld_Jonas.De Preter.s.ua_poging_2019-11-12
   -->Feremans
      -->Huistaak1-HelloWorld_Len.Feremans.s.ua_poging_2019-11-10
   ...

The code needs to get the day of the provided date and if it's above 11 it creates a directory "late_inzending" So it should look like this

-->Mainfolder
   -->Jan
      -->Huistaak1-HelloWorld_Jonas.De Preter.s.ua_poging_2019-11-12
      -->late_inzending
   ...

My code doesn't seem to work

for dir in */
do
    cut1=$(echo "$dir" | cut -f4 -d '_')
    cut2=$(echo "$cut1" | cut -f3 -d '-')
    declare -i x="$cut2"
    declare -i y=11
    if (( x > y))
    then
        mkdir late_inzending
    fi    
done

CodePudding user response:

Something like.

#!/usr/bin/env bash

for d in ./*/*/; do
  [[ ! -d "$d" ]] && continue
  end="${d##*-}"
  (( ${end%/} > 11 )) &&
  echo mkdir -p "$d"late_inzending
done

  • Execute from within main

  • Remove the echo if you're ok with the ouput.

  • Related