이 코드는 왜 작동하지 않나요?

이 코드는 왜 작동하지 않나요?

나는 이와 같은 출력을 얻으려고 노력하고 있습니다.

$ 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

답변2

이것은 완전하지 않지만 $(ls -l xxx)는 파일당 한 줄이 아닌 한 줄을 생성합니다. 그러면 분석이 엉망이 될 것입니다.

따라서 $Namefile*을 반복하여 한 번에 하나의 파일을 처리합니다.

답변3

색인이 잘못되었습니다.

for ((m = 5; m < ${#array_Datefile[@]}; m += 9))
do
    d=$((m + 1))
    t=$((m + 2))
    f=$((m + 3))
    Month=${array_Datefile[m]}
    Day=${array_Datefile[d]}
    Time=${array_Datefile[t]}
    Name=${array_Datefile[f]}
    echo "The file $Name was modified on $Month $Day $Time"
done

답변4

나는 그것을 해결했습니다.

Namefile=$1
i=-1
for n in $Namefile*
do
ExDatefile=$(ls -l $Namefile* | head $i | tail -1 )
i=$(($i -1))
IFS=' ' array_Datefile=($ExDatefile)
echo "The file ${array_Datefile[8]} was modified ${array_Datefile[5]} ${array_D$
unset ExDatefile
done

출력은 다음과 같습니다.

The file hw1_evening_sun.txt was modified May 29 2008
The file hw1_morning_sun.txt was modified May 29 2008
The file hw1_out_si_wire.txt was modified May 29 2008
The file hw1_script.sh was modified Jun 2 15:20
The file hw1_script2.sh was modified Jun 2 15:16
The file hw1_sun1.txt was modified May 29 2008
The file hw1_sun2.txt was modified May 29 2008

관련 정보