LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2019, 10:48 PM   #1
saleemsupra
LQ Newbie
 
Registered: Jan 2008
Posts: 5

Rep: Reputation: 0
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
 
Old 02-26-2019, 11:34 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
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?
 
Old 02-27-2019, 06:24 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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?
 
Old 02-27-2019, 06:29 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,553

Rep: Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946
Quote:
Originally Posted by saleemsupra View Post
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.
 
2 members found this post helpful.
Old 02-27-2019, 06:44 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by saleemsupra View Post

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!

Last edited by BW-userx; 02-27-2019 at 07:35 AM.
 
Old 02-27-2019, 09:16 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by TB0ne View Post
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.
 
1 members found this post helpful.
Old 02-27-2019, 11:25 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275
you might want to insert set -xv at the beginning of your script. You will see what's happening.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Basic dig Command Usage and Detailed Explanation of its Output. LXer Syndicated Linux News 1 02-23-2019 03:46 AM
Detailed explanation required about nanosleep system call description. Hitx11 Linux - General 8 12-11-2017 11:28 PM
LXer: Ubuntu Netbook Remix: a detailed explanation LXer Syndicated Linux News 0 06-08-2008 02:21 PM
LFS book: Is there a more detailed explanation (like: --verbose) vadkutya Linux From Scratch 12 05-17-2008 01:36 PM
oh dear. oh dear oh dear oh dear. ilumin8d Linux - General 2 09-20-2002 05:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration