0

Ok. So here is the situation.

I scan to an ftp folder. I want to know if new files have appeared in that folder using the following program code. The number of lines output to count.txt is 3, the reason being one of the files is .ftpquota.

so if I run this from the command line

ls -1 ~/public_html/scan | wc -l > count.txt

and then run the script

#!/bin/bash
var=$(cut -d, -f1 count.txt)
echo "No of files is $var"

if [[ "$var" -gt 3 ]]
  then
    printf "Found new documents...\n"
  else
    printf "No new documents found.\n"
  fi

The program works fine because value of $var is 3. But my problem is that because I would like to include this line

ls -1 ~/public_html/scan | wc -l > count.txt

as a part of the script so that it updates count.txt everytime, it does not work. So in other words if I do this:

#!/bin/bash
ls -1 ~/public_html/scan | wc -l > count.txt
var=$(cut -d, -f1 count.txt)
echo "No of files is $var"

if [[ "$var" -gt 3 ]]
  then
    printf "Found new documents...\n"
  else
    printf "No new documents found.\n"
  fi

It breaks the script and it stops working and $var returns a value of 0 .Please help. I would like this code corrected instead of being given alternative solutions. I spent days on this script and now I'm almost there if that one command works.

  • Try replacing `~/public_html/scan` with the actual full path. Maybe the path is not being interpreted properly. – fedorqui Mar 11 '14 at 22:40
  • 1
    Are you running the script from the command line? Or from a `cron` job? – lurker Mar 11 '14 at 22:41
  • @user3408193 Can you change your ls command to log error in count.txt like below and let us know what you get ? `code` ls -1 ~/Release_tool 2> count.txt | wc -l > count.txt `code` – Mayur Nagekar Mar 11 '14 at 23:18
  • mbratch - This script is going to go in a cron job. Hence the automation that I need. If nothing works then I might have to cron "ls" command separately and then the script as a second job. – user3408193 Mar 12 '14 at 11:22
  • Miyurz - What do you mean by ls -1 ~/Release_tool 2> count.txt | wc -l > count.txt. Do you want me to add it to the start of my script because when I did it, It gives this error message. No of files is 0 : cannot access /home/omar1/Release_tool: No such file or directory ./scan.sh: line 5: [[: 0 : cannot access /home/omar1/Release_tool: No such file or directory: syntax error in expression (error token is ": cannot access /home/omar1/Release_tool: No such file or directory") No new documents found. – user3408193 Mar 12 '14 at 11:24

0 Answers0