09と11を比較する方法

09と11を比較する方法

したがって、日付形式のファイル名の一部を取得し、その数が11より大きいことを確認するコードがあります。すべてのファイルは同じ命名原則に従い、名前と日付のみが異なります。 -> 例:

Huistaak1-HelloWorld_Jonas.De Preter.s.ua_poging_2019-11-12 (注: このファイル名はディレクトリです)

以下は、最後の2つの数字を取得して11と比較し、数字が大きい場合にディレクトリを作成するコードです。

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

私が経験している問題は、日付が10未満のときに09と11を比較することです。これによりエラーが発生します。

09> 11 --> 'utf-8'コーデックは位置679のバイト0xc8をデコードできません:無効な連続バイト

ベストアンサー1

sedを使って簡単に修正しました。

#the string which I'm working with:
#Huistaak1-HelloWorld_Jolien.Peters.s.ua_poging_2019-11-12

for d in ./*/*/; do
  char=${d: -3} #:Variable to get the last 2 numbers in this string(12/)
  x=${char%/} #:Variable to remove the invisible "/"
  y=$(echo $x | sed 's/^0*//')#:Incase there are leading zeros remove them
  echo $y
  (( "$y" > 11 )) && #Compare the numbers and if $y is bigger then make new directory
  mkdir -p "$d"late_inzending
done

おすすめ記事