Bash script monitor directory and subdirectories for new pdfs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script monitor directory and subdirectories for new pdfs
# 1  
Old 02-27-2015
Bash script monitor directory and subdirectories for new pdfs

I need bash script that monitor folders for new pdf files and create xml file for rss feed with newest files on the list. I have some script, but it reports errors.

Code:
#!/bin/bash

SYSDIR="/var/www/html/Intranet"
HTTPLINK="http://TYPE.IP.ADDRESS.HERE/pdfs"
FEEDTITLE="Najnoviji dokumenti na Intranetu OUG"
FEEDLINK="http://TYPE.IP.ADDRESS.HERE/pdfs"
FEEDDESC="Novi dokumenti"
RSSDIR="/var/www/html/rss"
#DESC="`date`"



function testing_variables {
        if [ ! -d ${RSSDIR} ]; then
                echo -e 'ERROR: $RSSDIR does not exists!\nPlease create a directory and set the right path for $RSSDIR variable!'
                exit 1
        fi

        if [ ! -d ${SYSDIR} ]; then
                echo -e 'ERROR: $SYSDIR does not exists!\nPlease create a directory and set the right path for $SYSDIR variable!'
        fi
}

function rss_header {
### RSS HEADER
echo "<!--?xml version=\"1.0\"?-->
<rss version="\"2.0\"">
  <channel>
        <title>${FEEDTITLE}</title>
        <link>${FEEDLINK}
        <description>${FEEDDESC}</description>" > $1
}

function rss_body {
#RSS BODY
for FILES in `find ${SYSDIR} -type f -name "*.pdf" | xargs ls -t | grep -i ${2}`; do
NAME="`basename $FILES`"
#PARENTDIR="`dirname $FILES | awk -F "/" '{print $NF}'`"

echo "  <item>
                <title>${NAME}</title>
                <link>${HTTPLINK}/${2}/${NAME}
<!--                <description>${DESC}</description> -->
        </item>" >> ${1}
done
}

function rss_footer {
### RSS FOOTER
echo "</channel></rss>" >> ${1}
}


### Main code ###
 
for FILES in `find ${SYSDIR} -type f -name "*.pdf" | xargs ls -t`; do
        PARENTDIR="`dirname $FILES | awk -F "/" '{print $NF}'`"
 
        rss_header ${RSSDIR}/${PARENTDIR}.xml
        rss_body   ${RSSDIR}/${PARENTDIR}.xml ${PARENTDIR}
        rss_footer ${RSSDIR}/${PARENTDIR}.xml
done

It reports error on line 13 "syntax error near unexpected token '$'{\r' '
and 'function testing_variables {

Please some help?
Moderator's Comments:
Mod Comment Please use CODE tags, not ICODE tags, for multi-line sample input, output, and code.

Last edited by Don Cragun; 02-27-2015 at 06:19 AM.. Reason: Change ICODE tags to CODE tags.
# 2  
Old 02-27-2015
don't use notepad for scripts, use vi! ;-)

Code:
tr -d '\r' script.sh >script1.sh

# 3  
Old 02-27-2015
One could also ask why you define a function that is never called???
# 4  
Old 02-27-2015
I am not very familiar with code in this script, I just adapted existing script from script library, changed folder names and file type. Please can you review script and correct errors?

Last edited by markus1981; 02-27-2015 at 07:48 AM.. Reason: typo
# 5  
Old 02-27-2015
Now that you have made changes to your script based on the suggestions you have already received, what errors are you getting? What does your script look like now? What is it doing wrong?

Or, are you just saying that you want the UNIX and Linux Forums to serve as your unpaid programming staff?
# 6  
Old 02-27-2015
I commented out function testing_variables line.
When I run script I get:

syntax error near unexpected token near '$'do\r' '
'for FILES in 'find ${SYSDIR} -type f -name "*.pdf" | xargs ls -t | grep -i ${2}' ; do

No, I just expect this forum to help me to learn how to adjust this code. Thanks in advance.
# 7  
Old 02-27-2015
With that error (again), you clearly did not follow agent.kgb's advice in message #2 in this thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to create new directory by date followed by identifier and additional subdirectories

I have a bash that downloads a list and if that list has data in it then a new main directory is created (with the date) with several subdirectories (example1, example2, example3). My question is in that list there are portion of specific file types (.vcf.gz) - identifier towards the end that have... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

3. Shell Programming and Scripting

Script to monitor directory size of specific users

Hi, i am new to shell scripts, i need to write a script that can monitor size of directory of specific users. Please help. Thanks, Nitin (2 Replies)
Discussion started by: nicksrulz
2 Replies

4. Shell Programming and Scripting

Bash Script to Compress All Subdirectories

I'd like to create simple bash script that, given a directory, compresses each directory by name, e.g.: Contents of ~/Documents Folder1 Folder2 Folder3 compress-subdirectoies.sh ~/Documents Results: Folder1. Folder2. Folder2. Any advice would be appreciated (7 Replies)
Discussion started by: furashgf
7 Replies

5. Shell Programming and Scripting

need help with little bash server monitor script

hello, i`m new in bash scripting and i getting an error with my little server monitoring script example of my script: #!/bin/sh s1_ats=0 while ; do sleep 5 s1=`ping -c 1 xxxx.xxxx.xxxx.xxxx | grep 64 | awk '{print $1}'` if ; then $s1_ats=0 else if ; then (2 Replies)
Discussion started by: grauzikas
2 Replies

6. Shell Programming and Scripting

Bash: Gzip files in Directory and itīs Subdirectories

Hello dear Community, I have a task to wrtie a script which will gzip not zipped files in a directory and itīs subdirectories. I succeeded in gzippung the directory but not the subdirectories: #/bin/bash #go to the directory where to zip cd $1 #Zip unzipped files for i in `ls | xargs... (2 Replies)
Discussion started by: JamesCarter
2 Replies

7. Shell Programming and Scripting

Korn/bash Script to monitor a file a check for specific data

Hi, Im trying to write this script but im stuck on it, basicaly what i want to do is to write a code to verify a log file ( apache log file for example ) and for each new line with specific data , then, output this new line for another file: full ex: output of the server.log is (... (4 Replies)
Discussion started by: Thales.Claro
4 Replies

8. Shell Programming and Scripting

script to monitor files in a directory and sending the alert

Hi All, We are having important config files in an directory which was accessable by all /auto/config/Testbed/>ls config1.intial config2.intial config3.inital often we find that some of the lines are missing in config files, we doubt if some one is removing. I would like to write... (0 Replies)
Discussion started by: shellscripter
0 Replies

9. Shell Programming and Scripting

script to monitor directory

What is the best way for a script to run to monitor a directory for the presence of files and then perform a function afterwords? I was hoping to have it continually run and sleep until it detects that files are present in the directory, then break out of the loop and go on to the next step. ... (17 Replies)
Discussion started by: nulinux
17 Replies

10. UNIX for Dummies Questions & Answers

Hep with script to monitor directory

Hello, I am a newbie who is attempting to write a script to monitor a directory for a set of 3 files that I am expecting to get ftp'd. Occasionally, we suspend operations for maintenance etc. but we still get the files so there can be more than 1 set. If there is more than 1 set, I would like... (2 Replies)
Discussion started by: cmf00186
2 Replies
Login or Register to Ask a Question