LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-22-2009, 11:55 AM   #1
TechVandal
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Rep: Reputation: 0
Bash Scripting Help


Hello everyone,

I am currently writing a bash script and I am running into a little bit of a problem. My background is in C++ and some concepts are just not transferring over.

The script is meant to search each user directory(usernames come from the ./user file) for files that are newer then the file in the LATE variable. When it finds the newer file, I want it to replace the current contents with that file path and name instead.

The output of the script is not what would be expected and I am trying to figure out why. Here is the code

Code:
#!/bin/bash

LATE=sue-238/words.bmp
LATE2=sue-238/cloud.jpg
Late3=littleboy/Life.jpg
TMP=""

echo $LATE > older
echo $LATE > latest

for each in `cat ./users`; do
        TMP=`find $each -type f -newer $LATE` #> t3mp
        echo TMP BEFORE TEST = $TMP
        echo $TMP > t3mp
        if [ $TMP==" " ];then
               TMP=`cat older`
               echo TMP OLDER = $TMP
               else
               TMP=`cat t3mp`
                echo TMP NEWER = ${TMP}
           fi
        echo TMP = $TMP
        echo $LATE > older
        echo $TMP > latest
        LATE=`cat latest`
        echo LATE= $LATE
done
here is the output of the code:

TMP BEFORE TEST = fatman/test.jpg
TMP OLDER = sue-238/words.bmp
TMP = sue-238/words.bmp
LATE= sue-238/words.bmp
TMP BEFORE TEST = sue-238/cloud.jpg
TMP OLDER = sue-238/words.bmp
TMP = sue-238/words.bmp
LATE= sue-238/words.bmp
TMP BEFORE TEST = littleboy/test.jpg littleboy/test2.jpg
feeder.sh: line 15: [: littleboy/test.jpg: unary operator expected
TMP NEWER = littleboy/test.jpg littleboy/test2.jpg
TMP = littleboy/test.jpg littleboy/test2.jpg
LATE= littleboy/test.jpg littleboy/test2.jpg


Much thanks is appreciated.

Last edited by TechVandal; 02-22-2009 at 04:57 PM.
 
Old 02-22-2009, 01:34 PM   #2
TechVandal
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
ok sorry i didnt solve the problem apparently

Last edited by TechVandal; 02-22-2009 at 04:58 PM. Reason: issue not fixed
 
Old 02-22-2009, 07:25 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
I'm trying to figure out what you're trying to do.

I started by trying to simplify your code, but it seems incomplete, Here's what I got so far, but I lack test data, and the grand kids just came over to wathch the Oscars.

Code:
#!/bin/bash

LATE=sue-238/words.bmp
LATE2=sue-238/cloud.jpg
Late3=littleboy/Life.jpg

older=$LATE
latest=$LATE

dbg=yes

# For each whitespace delimited field in the file "users" inthe current directory
for each in $(cat ./users); do
	# Get a list all the objects with a modification date after the mod date
	# of $LATE in the directoy $each and all it's children. Discard any
	# error messages.
        TMP=$(find $each -type f -newer $LATE 2>/dev/null)
	# Perhaps $(find $each -maxdepth 1 -printf "\"%p\"\n") might be a better choice?

        [ -n "$dbg" ] && echo TMP BEFORE TEST = $TMP

        if [ x$TMP = x ];then
               TMP=$older
               [ -n "$dbg" ] && echo TMP OLDER = $TMP
        else
               [ -n "$dbg" ] && echo TMP NEWER = $TMP
        fi
        [ -n "$dbg" ] && echo TMP = $TMP
        older=$LATE
        latest=$TMP
        LATE=$latest
        [ -n "$dbg" ] && echo LATE = $LATE
done
 
Old 02-23-2009, 11:39 AM   #4
TechVandal
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Hey PTrenholme thanks for the input so far.

The script is incomplete so far, but I try to code and test as I go rather then come up with a giant script that doenst work and begin working out the bugs.

Plus, Im doing this between working on sys admin tasks so it comes along slowly.

Anyway, as I said, the script is suppose to run through the ./user file which contains only 3 lines at this point(the usernames of FTP users)

fatman
sue-238
littleboy

and thats it. I put it in a loop so the script could work in iterations and this seemed like an easy way to do it. So it goes and assigns variable TMP to the results of a file name and path newer then the current setting in LAST. If there is no newer file, it returns blank, which was basically voiding the rest of the test so I through that if statement in there. So if TMP is blank, then it sets the contents of the variable to what it was before the newer test(thus keeping the latest file in the variable), and then outputting that to txt files so the values stay for the next iteration(I was having troubles with variables at the start of the script so to be safe i echoed the contents to txt files)

The output is strictly for testing right now, just to see if the test worked. The output is rather strange for me and ill explain why:

TMP BEFORE TEST = fatman/test.jpg<-newer file then the contents of LAST
TMP OLDER = sue-238/words.bmp<-somehow the TMP var is blank for the if so it returns the older file variable
TMP = sue-238/words.bmp<-after the test, still the older variable to be expected
LATE= sue-238/words.bmp<-same
TMP BEFORE TEST = sue-238/cloud.jpg<-newer file then what is in LAST
TMP OLDER = sue-238/words.bmp<-returns old file again, but WHY? ? ? ! GRAR
TMP = sue-238/words.bmp<-same as above
LATE= sue-238/words.bmp<-same as above
TMP BEFORE TEST = littleboy/test.jpg littleboy/test2.jpg<-both newer then LAST
feeder.sh: line 15: [: littleboy/test.jpg: unary operator expected<-error from it returning more then 1 file name i understand this
TMP NEWER = littleboy/test.jpg littleboy/test2.jpg <- both files newer
TMP = littleboy/test.jpg littleboy/test2.jpg<-both files newer
LATE= littleboy/test.jpg littleboy/test2.jpg<-both files newer

out of 3 iterations, the script worked on the last one, but not the first two, why??

Here are the contents of those directories

atombomb@atombombcafe.com [~/public_html]# ls -alh littleboy fatman sue-238
fatman:
total 68K
drwxr-xr-x 2 atombomb atombomb 4.0K Feb 22 13:40 ./
drwxr-x--- 16 atombomb nobody 4.0K Feb 23 09:40 ../
-rw-r--r-- 1 atombomb atombomb 54K Feb 22 13:40 test.jpg

littleboy:
total 192K
drwxr-xr-x 2 atombomb atombomb 4.0K Feb 22 13:35 ./
drwxr-x--- 16 atombomb nobody 4.0K Feb 23 09:40 ../
-rw------- 1 atombomb atombomb 4 Feb 12 03:58 .ftpquota
-rw-r--r-- 1 atombomb atombomb 54K Feb 16 01:55 Life.jpg
-rw-r--r-- 1 atombomb atombomb 54K Feb 22 13:35 test2.jpg
-rw-r--r-- 1 atombomb atombomb 54K Feb 21 10:55 test.jpg

sue-238:
total 2.9M
drwxr-xr-x 2 atombomb atombomb 4.0K Feb 20 15:01 ./
drwxr-x--- 16 atombomb nobody 4.0K Feb 23 09:40 ../
-rw-r--r-- 1 atombomb atombomb 846K Feb 10 15:36 100_5033.jpg
-rw-r--r-- 1 atombomb atombomb 605K Feb 20 15:01 cloud.jpg
-rw------- 1 atombomb atombomb 4 Feb 9 18:07 .ftpquota
-rw-r--r-- 1 atombomb atombomb 704K Feb 20 15:01 words.bmp
-rw-r--r-- 1 atombomb atombomb 704K Feb 11 18:17 wtf.bmp


all of the help is definitely appreciated. I will work on understanding your updates to my code.
 
Old 02-25-2009, 07:16 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
man that's hard work

Code:
find dir -newer time_stamp_file  | while read filename;do
echo "$filename"
done
create a timestamp file like:

Code:
touch -t YYYMMHHmm filename

Last edited by bigearsbilly; 02-25-2009 at 07:18 AM.
 
Old 03-01-2009, 03:22 PM   #6
TechVandal
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bigearsbilly View Post
man that's hard work

Code:
find dir -newer time_stamp_file  | while read filename;do
echo "$filename"
done
create a timestamp file like:

Code:
touch -t YYYMMHHmm filename
I see what youre doing, but then I will still need to find the file that has the most recent update within it, and I would need to update the timestamp file with the newer timestamp, this seems like an equal amount of work, though i will think on it.

I really appreciate the help
 
  


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
Bash Scripting Help? jhyland87 Programming 7 12-14-2008 11:58 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Bash scripting disruptive Programming 5 06-29-2006 03:49 PM
bash scripting fnoyan Programming 1 01-18-2005 07:35 AM
Bash scripting JonCooperUK Programming 3 03-04-2004 08:55 PM

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

All times are GMT -5. The time now is 05:23 PM.

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