Find the answer to your Linux question:
Results 1 to 8 of 8
Hi people! I would like to start by saying that I'm new to bash scripting! I'm trying to write a script that could list all files that have been modified ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    4

    Bash script variables comparision

    Hi people!
    I would like to start by saying that I'm new to bash scripting! I'm trying to write a script that could list all files that have been modified today an that are bigger then 400k recursively from the location specified in the argument.

    The problem is that the script does not return any results !!
    so when i replace the $date with "2010-01-30" the script returns some results !!

    I'm quite sure that the problem comes from the comparison of ($6 == $dat)

    Code:
    #!/bin/bash
    
    
    if [ -z $1 ]
    then
    echo "Enter a path: "
    
    else [ -n $1 ]
    
    
    dat=`date +%F`
    
    path1=$1
    
    
    file="$(ls $path1 -lR | awk '{if(($5>400)&&($6 == $dat))print $5 "k\t" $6 "\t" $8 "\n\v\r"}')"
    echo $file
    
    fi
    Please tell me what i'm i doing wrong

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    i'll tell you right now your date format is wrong.

    $6 (Jan 17:25) will not equal date +%F (2010-01-30). there are a few ways around it, what i would do is add --full-time to my ls command. your ls output will look like this:

    Code:
    [user1@dumbbot ~]$ ls -lR --full-time /home/user1
    /home/user1:
    total 21416
    -rw-r--r-- 1 user1 wheel 21887319 2010-01-30 17:25:34.000000000 -0600 cpinfo-00034076.txt
    -rwx------ 1 user1 wheel      196 2010-01-30 18:10:12.000000000 -0600 new-script.sh

  3. #3
    Just Joined!
    Join Date
    Jan 2010
    Posts
    4
    Thanks for the quick answer!

    when i run the ls command in my terminal, i get the following output

    Code:
    kalle@ubuntuFS:~/uppgift7$ ls -l
    total 196
    drwxr-xr-x 2 kalle kalle  4096 2010-01-29 00:36 awk1
    -rwxr-xr-x 1 kalle kalle   562 2010-01-27 23:00 exp1.sh
    -rwxr-xr-x 1 kalle kalle   415 2010-01-27 23:38 exp2.sh
    so the $6 in this case is 2010-01-27.

    The output from ls -lR --full-time
    Code:
    kalle@ubuntuFS:~/uppgift7$ ls -lR --full-time
    .:
    total 196
    drwxr-xr-x 2 kalle kalle  4096 2010-01-29 00:36:48.000000000 +0100 awk1
    -rwxr-xr-x 1 kalle kalle   562 2010-01-27 23:00:40.000000000 +0100 exp1.sh
    so i'v changed the script as you recommended
    Code:
    #!/bin/bash
    if [ -z $1 ]
    then
    echo "Enter a path: "
    else [ -n $1 ]
    dat=`date +%F`
    path1=$1
    
    file="$(ls $path1 -lR --full-time | awk '{if(($5>400)&&($6 == $dat))print $5 "k\t" $6 "\t" $8 "\n\v\r"}')"
    echo $file
    fi
    This is what i got
    Code:
    kalle@ubuntuFS:~/uppgift7$ ./uppgift15.sh /home/kalle/uppgift7/
    
    kalle@ubuntuFS:~/uppgift7$
    and if i change the $dat to "2010-01-30" in the script, then the output becomes like this (like i'm expecting it to be!)
    Code:
    kalle@ubuntuFS:~/uppgift7$ ./uppgift15.sh /home/kalle/uppgift7/
    4096k 2010-01-30 +0100 
     448k 2010-01-30 +0100 
     513k 2010-01-30 +0100 
     513k 2010-01-30 +0100 
    
    kalle@ubuntuFS:~/uppgift7$
    Any ideas? Help appreciated!

  4. #4
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    sorry on i was testing your script on a centos box, and the timestamp format is different.

    try using eq instead of ==

  5. #5
    Just Joined!
    Join Date
    Jan 2010
    Posts
    4
    Well the eq instead of == gave some results but the output is still wrong


    Code:
    kalle@ubuntuFS:~/uppgift7$ ./uppgift15.sh /home/kalle/uppgift7/
    4096k 2010-01-29 +0100 
     562k 2010-01-27 +0100 
     415k 2010-01-27 +0100 
     4096k 2010-01-30 +0100 
     4096k 2010-01-29 +0100 
     556k 2010-01-29 +0100 
     12243k 2010-01-29 +0100 
     11777k 2010-01-29 +0100 
     448k 2010-01-30 +0100 
     580k 2010-01-28 +0100 
     513k 2010-01-30 +0100 
     513k 2010-01-30 +0100 
     4096k 2010-01-29 +0100 
    
    kalle@ubuntuFS:~/uppgift7$
    Thanks for trying!

  6. #6
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    why do you have to do it that way? why not use a command like find?

    Code:
    find $1 -type f -daystart -size +40k
    where $1 is your directory, otherwise it will run the command in the pwd

  7. #7
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    You may consider :

    Code:
    find "$1" -type f -newermt 00:00 -size +400k -printf "%p %T+ %u %g %s\n"
    
    man find
    EDIT:
    If you use daystart, you must provide a further test on a time value, else find will list everything

    Code:
    find $1 -type f -daystart -size +400k -mtime 0 #-printf "%p %T+ %u %g %s\n"
    Last edited by nmset; 01-31-2010 at 09:59 AM.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  8. #8
    Just Joined!
    Join Date
    Jan 2010
    Posts
    4
    Thanks for the alternative solutions! worked just fine. But still have to do this script with the AWK command to fulfill the conditions of the lab!

    When i echo the $dat, it returns today's date 2010-01-31!
    The print $6 from the output of the AWK command returns a date in the same format "2010-01-*". some days do match the $6 output "2010-01-31". In this case the comparison ($6 == $dat) should return true! !

    so what is the problem? I'm very confused!

    When i replace the $dat with the string "2010-01-30" ($6 == "2010-01-30"), the script return what i'm looking for.

    Code:
    #!/bin/bash
    
    if [ -z $1 ]
    then
    echo "Enter a path: "
    
    else [ -n $1 ]
    
    
    dat=`date +%F`
    echo $dat
    path1=$1
    echo $path1
    
    file="$(ls $path1 -lR --full-time | awk '{if(($5>400)&&($6 == "2010-01-30"))pri$
    echo $file
    Thanks again for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...