LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Dear I need some detailed explanation about the following program (https://www.linuxquestions.org/questions/linux-newbie-8/dear-i-need-some-detailed-explanation-about-the-following-program-4175649143/)

saleemsupra 02-26-2019 10:48 PM

Dear I need some detailed explanation about the following program
 
for file in `ls`
do
day=`ls -lt $file|cut -d' ' -f6`
if [[ $day = "2007-11-21" ]]
then
echo "$file matched"
fi
done

Turbocapitalist 02-26-2019 11:34 PM

Please re-read the forum guidelines on how to create a useful question.

That said, you'll find a nearly complete explanation here:
http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.org/BashGuide

Which specific parts do you have questions about?

rtmistler 02-27-2019 06:24 AM

Besides the accurate advice from Turbocapitalist.

Please use [code][/code] tags around code to maintain spacing and formatting.

This is not a program, it is a script.

Have you run it?

TB0ne 02-27-2019 06:29 AM

Quote:

Originally Posted by saleemsupra (Post 5967462)
Code:

for file in `ls`
do
    day=`ls -lt $file|cut -d' ' -f6`
    if [[ $day = "2007-11-21" ]]
    then
        echo "$file matched"
    fi
done


You mean the script you copied/pasted from here?
https://www.linuxquestions.org/quest...8/#post2965915

Did you read that thread, where it's explained, and its usage case? Did you LOOK at the script line by line? See the commands it's using and read their man pages?? After being here for ELEVEN YEARS, you should know to do basic research first.

BW-userx 02-27-2019 06:44 AM

Quote:

Originally Posted by saleemsupra (Post 5967462)

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! :D

rtmistler 02-27-2019 09:16 AM

Quote:

Originally Posted by TB0ne (Post 5967564)
You mean the script you copied/pasted from here?
https://www.linuxquestions.org/quest...8/#post2965915

Did you read that thread, where it's explained, and its usage case? Did you LOOK at the script line by line? See the commands it's using and read their man pages?? After being here for ELEVEN YEARS, you should know to do basic research first.

Thank you very much for the reference TB0ne.

I'm personally wondering how you managed to locate it, and if you care to share that.

The original question and thread title are inadequate, and these were points which Turbocapitalist covered very well.

Did notice that all of saleemsupra's posts seem to be lacking in great detail, and still chose to not offer much comment.

Knowing now that the script in question is a copy from a far older question, it does change my opinion a bit.

I'm not sure there's any particular LQ Rules violation.

But meanwhile I would say that it is poor posting on your part saleemsupra to have been on this site for your duration and never taken the time to offer any real feedback when people have responded to your other questions, nor to update your threads when people ask you for information.

pan64 02-27-2019 11:25 AM

you might want to insert set -xv at the beginning of your script. You will see what's happening.


All times are GMT -5. The time now is 01:55 AM.