LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Scripting Help (https://www.linuxquestions.org/questions/programming-9/bash-scripting-help-706640/)

TechVandal 02-22-2009 11:55 AM

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.

TechVandal 02-22-2009 01:34 PM

ok sorry i didnt solve the problem apparently

PTrenholme 02-22-2009 07:25 PM

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


TechVandal 02-23-2009 11:39 AM

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.

bigearsbilly 02-25-2009 07:16 AM

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

TechVandal 03-01-2009 03:22 PM

Quote:

Originally Posted by bigearsbilly (Post 3456829)
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


All times are GMT -5. The time now is 02:33 PM.