LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2012, 04:01 PM   #1
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
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.

Last edited by amboxer21; 09-29-2012 at 06:08 PM.
 
Old 09-29-2012, 06:06 PM   #2
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
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!

Last edited by amboxer21; 09-29-2012 at 06:29 PM.
 
Old 09-29-2012, 08:00 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by amboxer21 View Post
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
 
1 members found this post helpful.
Old 09-29-2012, 08:17 PM   #4
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
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.
 
  


Reply



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
Basic questions re: bash veeruk101 Linux - Newbie 2 09-26-2010 10:42 AM
bash questions john6443 Linux - Newbie 19 02-19-2010 11:50 AM
Easy questions about bash....... CoOlGuY_NY Programming 4 02-02-2009 10:37 PM
Bash scripting questions: Geminias Programming 3 08-06-2007 08:58 AM
bash questions xeon123 Linux - General 2 11-23-2006 05:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 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