Quote:
Originally Posted by saleemsupra
for file in `ls`
do
day=`ls -lt $file|cut -d' ' -f6`
if [[ $day = "2007-11-21" ]]
then
echo "$file matched"
fi
done
|
best way to figure it out and see what it is doing, a few ways actually, using
Code:
#!/bin/bash
set -xv
bash script code here
open a terminal and break it down to see what each piece does as best as possible. mostly I'd be concerned about the test condition value it is looking for.
Code:
day=`ls -lt $file|cut -d' ' -f6`
//first issue
ls -lt
// second issue
ls -lt | cut -d' ' -f6
or writting it like this in a terminal
Code:
userx@vOiDeD.gov:~
$ var="$(ls -lt . | cut -d' ' -f6)"
$ echo $var
56611204
or writing it like this
Code:
for file in `ls`
do
# to see the value within day variable.
ls -lt $file|cut -d' ' -f6
day=`ls -lt $file|cut -d' ' -f6`
if [[ $day = "2007-11-21" ]]
then
echo "$file matched"
fi
done
The for loop, This is something you should read up on. Search 'for loops bash' for more information on loops and bash, search 'loops bash' there are three basic loops, 'for', 'while do', and, 'do while', then you have nested loops.
Doing that will give you your explanation about the code in question.
Experiment with the loops to gain a better understanding of what they do and how to use them to your advantage.
the quick of it all is, that it loops through all of the output/files in a given directory, cuts off the front of the output 'ls -lt' gives out, then puts that into a variable, then checks what that is against the condition in the test, then gives the results if a match is found.
looking at the example that I gave showing the output that code will never work.
using 7 for the placement to cut gives me this
Code:
$ var="$(ls -lt . | cut -d' ' -f7)"
$ echo $var
Jan
even trying this I get bad results
Code:
for f in `ls` ; do echo $(ls -lt "$f" | cut -d' ' -f6) ; done
//or this
for f in `ls` ; do echo $(ls -lt "$f" | cut -d' ' -f6-) ; done
//gets me this
Feb 26 08:00 coverart
Feb 26 08:00 fixmp3extention
Feb 25 10:10 fixmp3name
Feb 26 10:25 fixmp3titles
Feb 26 08:00 makeCopiesMP3
Feb 27 06:16 resample_SingleCopy_newMusic
Feb 27 06:17 resample_SingleCopy_newMusic2
Feb 27 06:18 resample_SingleCopy_newMusic3
Feb 26 08:00 resample_newMusic
Feb 26 08:00 sortbymetatag
Feb 26 08:00 testrun-sortbymetatag
//where you are looking for this
if [[ $day = "2007-11-21" ]]
this gives the three items one is looking for in the same order but still it is not the same "pattern" in the test.
Code:
for f in `ls` ; do echo $(ls -lt "$f" | awk '{print $6"-"$7"-"$8}') ; done
Feb-26-08:00
Feb-26-08:00
Feb-25-10:10
Feb-26-10:25
Feb-26-08:00
Feb-27-06:16
Feb-27-06:17
Feb-27-06:18
Feb-26-08:00
Feb-26-08:00
Feb-26-08:00
so now you know what you need to do next, right?
I found this, 'll --full-time'
Code:
ll --full-time | awk '{print $6}'
you just need to manipulate it to get it into a variable. This is probably already over kill but there ya go.
And be mindful about using code tags in here.
cheers!