1

I'm trying to take such a output like;

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

And my code is ;

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

By the way the output of $ExDatefile is ;

-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

And the output is;

$ 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   

So , what is wrong ?

Note:I am not sure whether the information given is enough for answering this question.If it is the case , please inform me.

Bash 3.2 OS X El Capitan

Edit:

when I directly call $array_Datefile[$n] , the output is;

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

Why is it like that ?Are there any problem with parsing ?

  • 3
    Don't parse ls. Use stat or find ... -printf or anything else instead. – cas Jun 2 '16 at 11:30
  • @cas I understand why I shouldn't parse but I've started to learn shell script recently ,so I couldn't understand how I should do . – onurcanbektas Jun 2 '16 at 11:51
  • 1
    If you're a beginner then now is the perfect time to avoid learning bad habits. – cas Jun 2 '16 at 11:59
  • The modification date from ls -l is not consistent with your goal. Even if you use ls -lT your script will fail if file names have spaces in them. I strongly suggest that you read the manuals stat and strftime. – fd0 Jun 2 '16 at 13:48
1

Ok, if you really want to parse ls -l output, you could try this:

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

... but if for $Namefile* .. you'd better:

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
  • With BSD stat you could just use- stat -f 'The file %N was modified on %Sm' -t '%Y-%m-%d at %R' -- "$file" in your loop. – fd0 Jun 2 '16 at 18:05
0

This won't be complete, but $(ls -l xxx) produces a single line, not a line per file. That messes up your parsing.

So, loop over $Namefile*, dealing with a single file at a time.

  • I know $(ls -l xxx) gives a single line .In fact, the numbers in loop are gives the modification date of an every single file .I still don't understand how it messes up. – onurcanbektas Jun 2 '16 at 11:32
  • By the way , I also didn't understand what do you mean by "loop over $Namefile* " ? – onurcanbektas Jun 2 '16 at 11:43
0

You got the indices wrong:

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
  • The code surprisingly doesn't give any output :) – onurcanbektas Jun 2 '16 at 11:38
  • I realized now , statement of "for" is not suitable. – onurcanbektas Jun 2 '16 at 11:42
0

I've solved it ;

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 output is ;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.