Por que esse código não está funcionando?

1

Estou tentando obter uma saída como essa:

$ 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.

E meu código é:

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

A propósito, a saída de $ 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

E a saída é:

$ 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   

Então, o que está errado?

Observação: não tenho certeza se as informações fornecidas são suficientes para responder a essa pergunta. Se for o caso, informe-me.

Bash 3.2 OS X El Capitan

Editar :

quando eu chamo diretamente $ array_Datefile [$ n], a saída é;

[5] [6] [7] [8]
[14] [15] [16] [17]
[22] [23] [24] [25]
[30] [31] [32] [33]

Por que é assim? Há algum problema com a análise?

    
por onurcanbektas 02.06.2016 / 13:06

4 respostas

0

Eu resolvi isso;

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

A saída é:

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
    
por 02.06.2016 / 16:15
1

Ok, se você realmente quer analisar ls -l output, você pode tentar isto:

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*)

... mas se for $Namefile* .. é melhor:

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
    
por 02.06.2016 / 16:33
0

Isso não estará completo, mas $ (ls -l xxx) produz uma única linha, não uma linha por arquivo. Isso atrapalha sua análise.

Então, faça um loop sobre $ Namefile *, lidando com um único arquivo de cada vez.

    
por 02.06.2016 / 13:26
0

Você errou os índices:

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
    
por 02.06.2016 / 13:32

Tags