私はこのような出力を取得しようとしています。
$ sh mod-date-pattern.sh sun
The file sun1.txt was modified on 2007-10-01 at 01:26.
The file sun2.txt was modified on 2007-10-01 at 19:10.
The file morning-sun.txt was modified on 2007-10-01 at 02:53.
The file evening-sun.txt was modified on 2007-10-01 at 02:55.
私のコードは次のとおりです。
Namefile=$1
ExDatefile=$(ls -l $Namefile*)
IFS=' ' array_Datefile=($Exdatefile)
for n in 5 14 22 30
do
m=$(($n +1))
o=$(($m +1))
p=$(($n -3))
Mounth=${array_Datefile[$n]}
Day=${array_Datefile[$m]}
Time=${array_Datefile[$o]}
Name=${array_Datefile[$p]}
echo "The file $Name was modified on $Mounth $Day $Time"
done
ちなみに$ExDatefileの出力はい。
-rwxr-xr-x@ 1 onurcanbektas staff 2026 May 29 2008 hw1_evening_sun.txt
-rwxr-xr-x@ 1 onurcanbektas staff 2687 May 29 2008 hw1_morning_sun.txt
-rwxr-xr-x@ 1 onurcanbektas staff 243128 May 29 2008 hw1_out_si_wire.txt
-rw-r--r-- 1 onurcanbektas staff 282 Jun 2 10:28 hw1_script.sh
-rw-r--r-- 1 onurcanbektas staff 68 Jun 2 11:49 hw1_script2.sh
-rwxr-xr-x@ 1 onurcanbektas staff 577 May 29 2008 hw1_sun1.txt
-rwxr-xr-x@ 1 onurcanbektas staff 6074 May 29 2008 hw1_sun2.txt
出力は次のとおりです。
$ sh hw1_script2.sh hw1
The file was modified on
The file was modified on
The file was modified on
The file was modified on
それでは、問題は何ですか?
注:提供された情報がこの質問に答えるのに十分かどうかはわかりません。もしそうなら、私に教えてください。
バッシュ3.2 OS Xエルキャピタン
編集する:
$array_Datefile[$n] を直接呼び出すと、出力は次のようになります。
[5] [6] [7] [8]
[14] [15] [16] [17]
[22] [23] [24] [25]
[30] [31] [32] [33]
なぜこれが起こるのですか?解析に問題がありますか?
ベストアンサー1
ls -l
まあ、本当に出力を解析したい場合は、次のことを試してみてください。
Namefile=$1
while read perms blocks user group size month day yearortime filename ;do
echo "The file $filename was modified on $month $day $yearortime"
done < <(ls -l $Namefile*)
...しかしfor $Namefile* ..
より良い場合:
Namefile=$1
for file in $Namefile*;do
unixtime=$(stat -c %Y "$file")
printf "The file %s was modified on %(%b %d %Y, %T)T\n" "$file" $unixtime
done