LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Yet another bash questions (https://www.linuxquestions.org/questions/programming-9/yet-another-bash-questions-4175429646/)

amboxer21 09-29-2012 04:01 PM

Yet another bash questions
 
EDIT: SOLUTION POSTED BELOW.

So the script i wrote to check the file time stamps against the terminal time stamp, has a problem. The more I think about it, the more tedious it seems!

I have this so far
Code:

#!/bin/bash

bashts=$(ls -l /bin/bash | awk '{print $7}' | sed 's/\://g');

readarray array < <(ls -l | awk '{ print $7" "$8}';
echo "${array[@]}" | sed -e 's/\://g; s/^[ \t]*//');

for i in "${array[@]}"; do 

        if [[ "$i" > "$bashts" ]] ; then
        echo $i | grep -v "~" | awk '{print $2}';
        fi

done

exit 0

The problem is that if a file modified in n ascending folder(a dir inside a dir), it will list the folder instead of the files that have been modified since opening the shell.

How could I go about this?? I figure for starters, I could check the results with file --mime and if it returns as a directory, i could go from there. This parses file --mime for the word directory -> file --mime dirName | sed -e 's/[-;]/ /g' | awk '{print $3}'

I am not asking how to do this. I can do it but it would be choppy. I want to know what the most efficient way of going about this is!

Thanks.

EDIT: I was initially going to put the end results in another array and run everything again that applies to directories but that seems a bit unnecessary!

The check for all files being a directory or not
Code:


readarray -t files < <(ls);
    for i in "${files[@]}"; do
    chk=$(file --mime $i | sed -e 's/[-;]/ /g' | awk '{print $3}');

        if [[ "$chk" == "directory" ]] ; then
        echo -e "$i is a dir";
        fi

    done

I just cant seem to think of any way to integrate that in the existing code without the use of the additional array. Hopefully someone view will have a suggestion.

amboxer21 09-29-2012 06:06 PM

here it is, but it feels/looks too heavy!

Code:

#!/bin/bash

bashts=$(ls -l /bin/bash | awk '{print $7}' | sed 's/\://g');

readarray array < <(ls -l | awk '{ print $7" "$8}';
echo "${array[@]}" | sed -e 's/\://g; s/^[ \t]*//');

for i in "${array[@]}"; do 

        if [[ "$i" > "$bashts" ]] ; then
        readarray -t direct < <(echo $i | grep -v "~" | awk '{print $2}');
          for x in "${direct[@]}"; do
          chk=$(file --mime $x | sed -e 's/[-;]/ /g' | awk '{print $3}');

            if [[ "$chk" != "directory" ]] ; then
            echo $i | grep -v "~" | awk '{print $2}'

            fi

          done
         
        fi

done

exit 0

ANY SUGGESTIONS ON HOW TO TIGHTEN THIS UP WOULD BE APPRECIATED!

ntubski 09-29-2012 08:00 PM

Quote:

Originally Posted by amboxer21 (Post 4792769)
list...the files that have been modified since opening the shell.

Is this what you are trying to do? Sounds like a job for find:
Code:

# this should go in .bashrc
if [ -n "$PS1" ] ;
    # remember when an interactive shell is opened
    export shell_opening_time=$(date)
fi

Code:

#!/bin/sh
touch --date="$shell_opening_time" reference_time
find . -cnewer reference_time -type f
rm reference_time


amboxer21 09-29-2012 08:17 PM

Quote:

Originally Posted by ntubski (Post 4792883)
Is this what you are trying to do? Sounds like a job for find:
Code:

# this should go in .bashrc
if [ -n "$PS1" ] ;
    # remember when an interactive shell is opened
    export shell_opening_time=$(date)
fi

Code:

#!/bin/sh
touch --date="$shell_opening_time" reference_time
find . -cnewer reference_time -type f
rm reference_time


That's a pretty cool thing you got going on there. I love the solution you came up with! Makes me think, I should have used the pid of the current shell I was using and use that instead of /bin/bash.


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