• February 23, 2025, 02:11:10 PM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

This Forum Beta is ONLY for registered owners of D-Link products in the USA for which we have created boards at this time.

Author Topic: Shell script problems  (Read 9894 times)

phanos

  • Level 1 Member
  • *
  • Posts: 8
Shell script problems
« on: May 12, 2013, 05:09:40 AM »

Hi I am having a strange problem regarding my DNS-323. I have written a script that detects and moves certain files of a specific directory. The script works fine when I run it from a shell prompt but it has issues when it is run from crontab or start up folder (/ffp/start/).

Specifically when it is run from the crontab or /ffp/start the script fails on the following line

 a=$(echo $line | tr ";" "\n")

The above line replaces the character ";" with the "\n". What I am attempting to do with this line is to separate the string in $line into substrings. Again this works fine when run from the shell prompt but when it is run from elsewhere the above line gives back an empty line.

Did anyone else experience anything similar to my problem? Does anyone knows how to solve my problem?

Thanks
Logged

dosborne

  • Level 5 Member
  • *****
  • Posts: 598
Re: Shell script problems
« Reply #1 on: May 12, 2013, 06:25:21 AM »

First thing that comes to mind is to ensure your environment (path) is set is the crontab environment, or just fully qualify any command. i.e replace "tr" with "/{--path--}/tr".
Logged
3 x DNS-323 with 2 x 2TB WD Drives each for a total of 12 TB Storage and Backup. Running DLink Firmware v1.08 and Fonz Fun Plug (FFP) v0.5 for improved software support.

phanos

  • Level 1 Member
  • *
  • Posts: 8
Re: Shell script problems
« Reply #2 on: May 12, 2013, 07:01:06 AM »

I am trying to locate the "sed" and "tr" commands but I am having trouble finding them. dosborne do you know where they are located? Thanks.
Logged

dosborne

  • Level 5 Member
  • *****
  • Posts: 598
Re: Shell script problems
« Reply #3 on: May 12, 2013, 08:31:36 AM »

Since you are not running a "standard" install, I can only guess. However, if you type:

which {--command--}

such as:

which sed
which tr

then the system will attempt to locate the command in question along your path. My guesss would be /ffp/bin for both :)
Logged
3 x DNS-323 with 2 x 2TB WD Drives each for a total of 12 TB Storage and Backup. Running DLink Firmware v1.08 and Fonz Fun Plug (FFP) v0.5 for improved software support.

phanos

  • Level 1 Member
  • *
  • Posts: 8
Re: Shell script problems
« Reply #4 on: May 12, 2013, 12:13:32 PM »

Thanks dosborne I was able to locate the binaries in /ffp/bin and got past that bug :). However is still gives me problems and it is driving me crazy. I am getting the following error when running the code through crontab 

[: ==: unknown operand
[: ==: unknown operand
[: ==: unknown operand
[: ==: unknown operand
[: ==: unknown operand

Again when run through shell prompt everything works fine. After re-writing again and again most of the code and testing it I came to the conclusion that the problem lies to the following lines of code

if [ "$r" == "->" ]; then

or

if [ "$match" == "0" ]; then

or

if [ "$?" == "0" ]; then

etc

Any or similar to the above code is giving me the "[: ==: unknown operand" error. I have tried many things including  "if [[ "$match" == "0" ]]; then" or "if [ "$match"-eq 0 ]; then" or if [ "$match" -eq "0" ]; then etc but the result was the same.

Do you have any idea?

Thanks

 
Logged

dosborne

  • Level 5 Member
  • *****
  • Posts: 598
Re: Shell script problems
« Reply #5 on: May 12, 2013, 01:08:36 PM »

The == conditional should be fine for a shell script. If you want to send me more of the script, I can try and replicate. It may be possible you would need to escapte the > symbol i.e. \> but other than that I don't see anything wrong, but without know what the variables hold it is a bit difficult. Maybe single quotes for sed rather than double?

I have something very similar in a script that I run daily via cron
Code: [Select]
 CLCC=`/ffp/bin/grep -c "Load_Cycle_Count" ${LOG_FILE}`
  if [ "$CLCC" != "0" ]; then
    echo -n "`date` "
    /ffp/bin/grep -m 1 "Load_Cycle_Count" ${LOG_FILE}
    echo -n "`date` ">>${LMNT}/smart_LLC.log
    /ffp/bin/grep -m 1 "Load_Cycle_Count" ${LOG_FILE}>>${LMNT}/smart_LLC.log
  fi
  if [ "$CLCC" == "2" ]; then
    echo -n "`date` "
    /ffp/bin/tac ${LOG_FILE}|/ffp/bin/grep -m 1 "Load_Cycle_Count"
    echo -n "`date` ">>${LMNT}/smart_LLC.log
    /ffp/bin/tac ${LOG_FILE}|/ffp/bin/grep -m 1 "Load_Cycle_Count">>${LMNT}/smart_LLC.log
  fi
if [ -f "${LOG_FILE}" ]; then
  /ffp/bin/sed -i '/^\*\*\*\*\*/d' ${LOG_FILE}
  /ffp/bin/sed -i '/^\#\#\#\#\#/d' ${LOG_FILE}
  /ffp/bin/sed -i '/stand by now/d' ${LOG_FILE}
  /ffp/bin/sed -i '/awake now/d' ${LOG_FILE}
  /ffp/bin/sed -i '/deprecated SCSI ioctl/d' ${LOG_FILE}
  /ffp/bin/sed -i '/^$/d' ${LOG_FILE}
fi

Which shell are you running? #!/ffp/bin/sh ??
« Last Edit: May 12, 2013, 01:10:29 PM by dosborne »
Logged
3 x DNS-323 with 2 x 2TB WD Drives each for a total of 12 TB Storage and Backup. Running DLink Firmware v1.08 and Fonz Fun Plug (FFP) v0.5 for improved software support.

phanos

  • Level 1 Member
  • *
  • Posts: 8
Re: Shell script problems
« Reply #6 on: May 12, 2013, 01:31:20 PM »

Hi I am using /bin/bash. I also tried /bin/sh. Here is the code:
Code: [Select]

#!/bin/bash

IFS='
'
WD=/mnt/HD_b2/download
rulesfile=$WD/"rules"

checkfile()
{
arg1=$1
ls -l $f/*.$arg1 1>/dev/null 2>>/dev/null
if [ $? == 0 ]; then
   retval=0
else
   retval=1
fi
return "$retval"
}

logentry()
{
arg1=$1
LOG=$WD/movelog.txt
echo `date +%m/%d/%Y-%H:%M:%S" :: $arg1"` >> $LOG
}

logentry "Starting up..."

for f in $WD/~*;

do

  logentry "Found directory $f"
  logentry "Checking file extensions"
  EXT="1"
  checkfile mkv
  retval=$?
  if [ $retval == 0 ]; then
     EXT=mkv
  fi

  checkfile avi
  retval=$?
  if [ $retval == 0 ]; then
     EXT=avi
  fi

  checkfile mp4
  retval=$?
  if [ $retval == 0 ]; then
     EXT=mp4
  fi

  if [ $EXT == "1" ]; then
     logentry "No valid file found in directory $f"
  else
     logentry "Start reading rule file \"$rulesfile\""
     while read line
     do
        logentry "Attempting rule $line"
        match="0"
        line=$(echo $line | /ffp/bin/sed -e "s/\ /#/g")
arr=$(echo $line | "/ffp/bin/tr" ";" "\n")

    for x in $arr
        do
r=$(echo $x|/ffp/bin/cut -c1-2)
      if [ "$r" == "->" ]; then

if [ "$match" == "0" ]; then
              line=$(echo $line | /ffp/bin/sed -e "s/#/\ /g")
  logentry "Rule $line was match for directory $f"
 
              x=$(echo $x | /ffp/bin/sed -e "s/#/\ /g")
              rule=`echo $x| /ffp/bin/sed -r 's/^.{2}//'`
             
  logentry "Executing command \"mv $f/*.$EXT $rule.$EXT\""
              mv \"$f/*.$EXT\" \"$rule.$EXT\"
              result="$?"
  if [ "$result" == "0" ]; then
                 logentry "Command was executed succesfully"
              else
                 logentry "Command failed. Return code is $result"
               fi
             else
              logentry "Rule $line was not matched with directory $f"
            fi
         else
echo "$f" | grep "$x" 1>/dev/null 2>>/dev/null
            if [ "$?" == "0" ]; then
   if [ "$match" == "0" ]; then
                  match="0"
               fi
             else
               match="1"
            fi
         fi
        done
     done < $rulesfile
     logentry "Done reading rule file \"$rulesfile\""
  fi
done

Hope you can make something of it. Thanks
« Last Edit: May 12, 2013, 01:32:55 PM by phanos »
Logged

dosborne

  • Level 5 Member
  • *****
  • Posts: 598
Re: Shell script problems
« Reply #7 on: May 12, 2013, 02:01:58 PM »

Interesting script.
You have to be really careful on using $? on piped commands as you could be getting an unexpected result. There is some really good info on bash and piped results here: http://stackoverflow.com/questions/90418/exit-shell-script-based-on-process-exit-code

One shortcut :-)
ls -l $f/*.$arg1 &> /dev/null
instead of
ls -l $f/*.$arg1 1>/dev/null 2>>/dev/null
LOL

Personally, I always use #!/ffp/bin/sh but there can be slight differences. I tend to be a bit more explicit on some things like EXT="mp4" to ensure it is a string. Doubt it matters. You could also try a shell for assigning your commands to a variable, i.e. fdate=`date +"%Y%m%d"`, or r=`$(echo $x|/ffp/bin/cut -c1-2)`  Again, not sure if it matters and it may be because we are using different shells. I'll take another look later but hopefully something here will help.

I would try and stick with either numeric or string. return "$retval" does the conversion so I would quote all the tests. Another thing I always do (old habit) is (other than declaration) I bracket my variables to ensure there is no confusion by the script. example mv \"${f}/*.${EXT}\" \"$r{ule}.${EXT}\"

Here is an example of a full script (than runs daily via cron) in case you can see something else.
Code: [Select]
#!/ffp/bin/sh
#
# Daily maintenance script
#
# v1.0.0 Initial implementation

FLAG="/tmp/daily.run"
if [ -f "${FLAG}" ]; then
  exit
else
  echo "`date`" >"${FLAG}"
fi
echo "`date` Running script [$0]"

# Global Variables
SMNT="/mnt/HD_a2"
DMNT="/mnt/HD_b2"
UMNT="/mnt/usb"
LMNT="${UMNT}/logs"
LLOG="daily_rsync.log"
TLLOG="temp.${LLOG}"
HOSTS="/etc/hosts"
hostname="`hostname`"
export cnd=`date +"%Y%m%d"`
UserPass="username=archive,password=secret"
aHost="NAS40"

if [ -f "${LMNT}/${LLOG}" ]; then
  echo "`date` Clearing old log file [${LMNT}/${LLOG}]"
  rm "${LMNT}/${LLOG}"
fi

# Check for FFP startup log and move if required
for _f in "${SMNT}/Nas_Prog/logs/ffp.log" "${SMNT}/ffp.log"; do
  if [ -f "${_f}" ]; then
    if [ -d "${LMNT}" ]; then
      echo "`date` Moving [${_f}] to [${LMNT}/ffp.log]"
      mv "${_f}" "${LMNT}/"
    fi
  fi
done

if [ "${hostname}" = "NAS40" ]; then
# For NAS40 Only
  echo "`date` Rsyncing [${SMNT}] to [${DMNT}] Log:[${LMNT}/${LLOG}]"
  echo "`date` Rsyncing [${SMNT}] to [${DMNT}]" >>${LMNT}/${LLOG}
  /ffp/bin/rsync -av --stats --delete ${SMNT}/ ${DMNT}/ >${LMNT}/${TLLOG} 2>&1
  if [ -f "${LMNT}/${TLLOG}" ]; then
    echo -n "`date` "
    /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
    echo -n "`date` "
    /ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
    echo -n "`date` Number of files removed: "
    grep -c "deleting " ${LMNT}/${TLLOG}
    cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
    rm ${LMNT}/${TLLOG}
  fi

  echo "`date` Rsyncing [inreto.de::dns323/fun-plug/0.5] to [/ffp/0.5] Log:[${LMNT}/${LLOG}]"
  echo "`date` Rsyncing [inreto.de::dns323/fun-plug/0.5] to [/ffp/0.5]" >>${LMNT}/${LLOG}
  /ffp/bin/rsync -av --stats --delete inreto.de::dns323/fun-plug/0.5/ ${SMNT}/ffp/0.5/ >${LMNT}/${TLLOG} 2>&1
  if [ -f "${LMNT}/${TLLOG}" ]; then
    echo -n "`date` "
    /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
    echo -n "`date` "
    /ffp/bin/grep "Number of files transferred" ${LMNT}/${TLLOG}
    echo -n "`date` Number of files removed: "
    grep -c "deleting " ${LMNT}/${TLLOG}
    cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
    rm ${LMNT}/${TLLOG}
  fi
            
  echo "`date` Rsyncing [inreto.de::dns323/fun-plug/0.7] to [/ffp/0.7] Log:[${LMNT}/${LLOG}]"
  echo "`date` Rsyncing [inreto.de::dns323/fun-plug/0.7] to [/ffp/0.7]" >>${LMNT}/${LLOG}
  /ffp/bin/rsync -av --stats --delete inreto.de::dns323/fun-plug/0.7/oarm/ ${SMNT}/ffp/0.7/ >${LMNT}/${TLLOG} 2>&1
  if [ -f "${LMNT}/${TLLOG}" ]; then
    echo -n "`date` "
    /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
    echo -n "`date` "
    /ffp/bin/grep "Number of files transferred" ${LMNT}/${TLLOG}
    echo -n "`date` Number of files removed: "
    grep -c "deleting " ${LMNT}/${TLLOG}
    cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
    rm ${LMNT}/${TLLOG}
  fi

else
  for _f in ".bootstrap" "ffp/my-packages" "ffp/scripts" "ffp/ipkg" "ffp/0.5" "ffp/0.7"; do
  # Rsync DIRECTORIES
    if [ -d "${SMNT}/${_f}" ]; then
      echo "`date` Rsyncing [NAS40::root/mnt/HD_a2/${_f}] to [${SMNT}/${_f}] Log:[${LMNT}/${LLOG}]"
      echo "`date` Rsyncing [NAS40::root/mnt/HD_a2/${_f}] to [${SMNT}/${_f}]" >>${LMNT}/${LLOG}
      /ffp/bin/rsync -av --stats --delete 192.168.1.40::root/mnt/HD_a2/${_f}/ ${SMNT}/${_f}/ >${LMNT}/${TLLOG} 2>&1
      if [ -f "${LMNT}/${TLLOG}" ]; then
        echo -n "`date` "
        /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
        echo -n "`date` "
        /ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
        echo -n "`date` Number of files removed: "
        grep -c "deleting " ${LMNT}/${TLLOG}
        cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
        rm ${LMNT}/${TLLOG}
      fi
    fi
  done
  for _f in "etc/*.conf" "etc/*.save" "etc/*.subr" "etc/fun_plug.*"; do
  # Rsync FILES
    echo "`date` Rsyncing [NAS40::root/ffp/${_f}] to [${SMNT}/ffp/${_f}] Log:[${LMNT}/${LLOG}]"
    echo "`date` Rsyncing [NAS40::root/ffp/${_f}] to [${SMNT}/ffp/${_f}]" >>${LMNT}/${LLOG}
    /ffp/bin/rsync -av --stats 192.168.1.40::root/ffp/${_f} ${SMNT}/ffp/etc/ >${LMNT}/${TLLOG} 2>&1
    if [ -f "${LMNT}/${TLLOG}" ]; then
      echo -n "`date` "
      /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
      echo -n "`date` "
      /ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
      echo -n "`date` Number of files removed: "
      grep -c "deleting " ${LMNT}/${TLLOG}
      cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
      rm ${LMNT}/${TLLOG}
    fi
  done  
  if [ -d "${UMNT}/lighttp" ]; then
    echo "`date` Rsyncing [NAS40::${UMNT}/lighttp] to [${UMNT}/lighttp] Log:[${LMNT}/${LLOG}]"
    echo "`date` Rsyncing [NAS40::${UMNT}/lighttp] to [${UMNT}/lighttp]" >>${LMNT}/${LLOG}
    /ffp/bin/rsync -av --delete --stats 192.168.1.40::root${UMNT}/lighttp/ ${UMNT}/lighttp/ >${LMNT}/${TLLOG} 2>&1
    if [ -f "${LMNT}/${TLLOG}" ]; then
      echo -n "`date` "
      /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
      echo -n "`date` "
      /ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
      echo -n "`date` Number of files removed: "
      grep -c "deleting " ${LMNT}/${TLLOG}
      cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
      rm ${LMNT}/${TLLOG}
    fi
    if [ -f "${UMNT}/lighttp/html/index.html" ]; then
      echo "`date` Setting hostname [${hostname}] in [${UMNT}/lighttp/html/index.html]"
      /ffp/bin/sed -i "s/NAS40/${hostname}/" ${UMNT}/lighttp/html/index.html
    fi
  fi
fi

LOG_FILE="${LMNT}/smartctl.log"
if [ -f "${LOG_FILE}" ]; then
  echo "`date` Clearing old SmartCtl log [${LOG_FILE}]"
  rm "${LOG_FILE}"
fi
for _f in "/dev/sda" "/dev/sdb"; do
  echo "`date` Dumping SmartCtl data for [${_f}] to [${LOG_FILE}]"
  echo "`date` SmartCtrl Data for [${_f}]" >>${LOG_FILE}
  /ffp/sbin/smartctl -a -i -d marvell ${_f} >>${LOG_FILE} 2>&1
done
if [ -f "${LOG_FILE}" ]; then
  SCC=`/ffp/bin/grep -c "self-assessment test result: PASSED" ${LOG_FILE}`
  if [ "${SCC}" != "2" ]; then
    echo "`date` ************************ S.M.A.R.T. FAILURE ***********************"
    echo -n "`date` S.M.A.R.T. FAILURE - ${SCC} HDD" >>${LMNT}/SMART_Err.log
    if [ ${SCC} != "1" ]; then
      echo -n "s" >>${LMNT}/SMART_Err.log
    fi
    echo -n " passed" >>${LMNT}/SMART_Err.log    
    cat ${LOG_FILE} | /usr/bin/mutt -s NAS40_SMART_Alert ${EEMAIL}
  fi
  echo "`date` SmartCtl log Diagnostic Pass count = ${SCC}"
  CLCC=`/ffp/bin/grep -c "Load_Cycle_Count" ${LOG_FILE}`
  if [ "$CLCC" != "0" ]; then
    echo -n "`date` "
    /ffp/bin/grep -m 1 "Load_Cycle_Count" ${LOG_FILE}
    echo -n "`date` ">>${LMNT}/smart_LLC.log
    /ffp/bin/grep -m 1 "Load_Cycle_Count" ${LOG_FILE}>>${LMNT}/smart_LLC.log
  fi
  if [ "$CLCC" == "2" ]; then
    echo -n "`date` "
    /ffp/bin/tac ${LOG_FILE}|/ffp/bin/grep -m 1 "Load_Cycle_Count"
    echo -n "`date` ">>${LMNT}/smart_LLC.log
    /ffp/bin/tac ${LOG_FILE}|/ffp/bin/grep -m 1 "Load_Cycle_Count">>${LMNT}/smart_LLC.log
  fi
else
  echo "`date` ***WARNING***: SmartCtl log [${LOG_FILE}] does not exist"
fi

LOG_FILE="${LMNT}/dmesg.log"
echo "`date` Dumping dmesg information to [${LOG_FILE}]"
echo "`date` System dmesg Information Dump" >${LOG_FILE}
/ffp/bin/dmesg -c >>${LOG_FILE}
if [ -f "${LOG_FILE}" ]; then
  /ffp/bin/sed -i '/^\*\*\*\*\*/d' ${LOG_FILE}
  /ffp/bin/sed -i '/^\#\#\#\#\#/d' ${LOG_FILE}
  /ffp/bin/sed -i '/stand by now/d' ${LOG_FILE}
  /ffp/bin/sed -i '/awake now/d' ${LOG_FILE}
  /ffp/bin/sed -i '/deprecated SCSI ioctl/d' ${LOG_FILE}
  /ffp/bin/sed -i '/^$/d' ${LOG_FILE}
fi

if [ -d "/ffp/start" ]; then
  echo "`date` Setting permissions for [/ffp/start/*] to [666]"
  /ffp/bin/chmod 777 /ffp/start/
  /ffp/bin/chmod 666 /ffp/start/*
fi

for _f in "addcron" "automatic" "dnsmasq" "kickwebs" "lighttpd" "logrotate" "ntpd" "rsyncd" "telnetd" "transmission"; do
  if [ -f "/ffp/start/${_f}.sh.save" ]; then
    echo "`date` Copying [/ffp/start/${_f}.sh.save] to [/ffp/start/${_f}.sh]"
    cp -p /ffp/start/${_f}.sh.save /ffp/start/${_f}.sh
    if [ -f "/ffp/start/${_f}.sh" ]; then
      echo "`date` Marking [/ffp/start/${_f}.sh] as executable"
      /ffp/bin/chmod +x /ffp/start/${_f}.sh
    else
      echo "`date` WARNING: [/ffp/start/${_f}.sh] does not exist. The copy may have failed."
    fi
  fi
  if [ -f "/ffp/etc/${_f}.conf.save" ]; then
    echo "`date` Copying [/ffp/etc/${_f}.conf.save] to [/ffp/etc/${_f}.conf]"
    cp -p /ffp/etc/${_f}.conf.save /ffp/etc/${_f}.conf
  fi
done

if [ -d "${SMNT}/Archive" ]; then
  echo "`date` Reseting [${SMNT}/Archive] ownership to user [Archive:504]"
  /ffp/bin/chown -R archive:504 ${SMNT}/Archive/* &
  if [ ! "${hostname}" = "NAS40" ]; then
    echo "`date` Rsyncing [NAS40::root/mnt/HD_a2/Archive] to [${SMNT}/Archive] Log:[${LMNT}/${LLOG}]"
    echo "`date` Rsyncing [NAS40::root/mnt/HD_a2/Archive] to [${SMNT}/Archive]" >>${LMNT}/${LLOG}
    /ffp/bin/rsync -av --stats --delete 192.168.1.40::root/mnt/HD_a2/Archive/ ${SMNT}/Archive/ >${LMNT}/${TLLOG} 2>&1
    if [ -f "${LMNT}/${TLLOG}" ]; then
      echo -n "`date` "
      /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
      echo -n "`date` "
      /ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
      echo -n "`date` Number of files removed: "
      grep -c "deleting " ${LMNT}/${TLLOG}
      cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
      rm ${LMNT}/${TLLOG}
    fi
  fi
fi

for _f in "${SMNT}" "${DMNT}"; do
  if [ -d "${_f}/Public" ]; then
    echo "`date` Setting permissions on the directory [${_f}/Public]"
    /ffp/bin/chown -R nobody:501 ${_f}/Public &
    /ffp/bin/chmod -R 777 ${_f}/Public &
  fi
done
                
if [ ! -d "/ffp/packages" ]; then
  if [ -d "/ffp/0.5/packages" ]; then
    echo "`date` Creating symbolic link for [/ffp/packages]"
    ln -s "/ffp/0.5/packages" "/ffp/packages"
  fi
fi
            
if [ ! -d "/ffp/extra-packages" ]; then
  if [ -d "/ffp/0.5/extra-packages" ]; then
    echo "`date` Creating symbolic link for [/ffp/extra-packages]"
    ln -s "/ffp/0.5/extra-packages" "/ffp/extra-packages"
  fi
fi

if [ -f "${HOSTS}" ]; then
echo "`date` Running the HOSTS file check on [${HOSTS}]"
  for _f in "40" "41" "42"; do
    COUNT=`/ffp/bin/grep -c "NAS${_f}" ${HOSTS}`
    if [ "${COUNT}" == "0" ]; then
      echo "`date` Adding host [NAS${_f}] with IP [192.168.1.${_f}] to [${HOSTS}]"
      echo "192.168.1.${_f} NAS${_f} NAS${_f}" >>${HOSTS}
    fi
  done
fi
                            
for _f in "etc" "ffp" "opt"; do
  echo "`date` Rsyncing [/${_f}] to [${UMNT}/backup/${_f}] Log:[${LMNT}/${LLOG}]"
  echo "`date` Rsyncing [/${_f}] to [${UMNT}/backup/${_f}]" >>${LMNT}/${LLOG}
  /ffp/bin/rsync -av --stats --delete /${_f}/ ${UMNT}/backup/${_f}/ >${LMNT}/${TLLOG} 2>&1
  if [ -f "${LMNT}/${TLLOG}" ]; then
    echo -n "`date` "
    /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
    echo -n "`date` "
    /ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
    echo -n "`date` Number of files removed: "
    grep -c "deleting " ${LMNT}/${TLLOG}
    cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
    rm ${LMNT}/${TLLOG}
  fi  
done

if [ -f "/ffp/start/addcron.sh" ]; then
  echo "`date` Running the addcron script [/ffp/start/addcron.sh]"
  /ffp/start/addcron.sh skip
fi

if [ -d "${UMNT}/transmission" ]; then
  echo "`date` Archiving the USB drive transmission files"
  if [ -f "${UMNT}/archive/${hostname}_transmission.tar.gz" ]; then
    rm ${UMNT}/archive/${hostname}_transmission.tar.gz
  fi
  /ffp/bin/tar -cz -f ${UMNT}/archive/${hostname}_transmission.tar.gz ${UMNT}/transmission >/dev/null
fi

if [ -f "/ffp/sbin/logrotate" ]; then
  if [ -f "/ffp/etc/logrotate.conf" ]; then
    echo "`date` Performing the log rotations [logrotate] Log:[${LMNT}/logrotate.log]"
    /ffp/sbin/logrotate -v /ffp/etc/logrotate.conf -s ${LMNT}/logrotate.state >${LMNT}/logrotate.log 2>&1
  fi
fi

if [ -d "${SMNT}/www_backup" ]; then
  echo "`date` Rsyncing [192.168.1.2::backup] to [${SMNT}/www_backup] Log:[${LMNT}/${LLOG}]"
  echo "`date` Rsyncing [192.168.1.2::backup] to [${SMNT}/www_backup]" >>${LMNT}/${LLOG}
  /ffp/bin/rsync -av --stats --delete 192.168.1.2::backup ${SMNT}/www_backup/ >${LMNT}/${TLLOG} 2>&1
  if [ -f "${LMNT}/${TLLOG}" ]; then
    echo -n "`date` "
    /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
    echo -n "`date` "
    /ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
    echo -n "`date` Number of files removed: "
    grep -c "deleting " ${LMNT}/${TLLOG}
    cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
    rm ${LMNT}/${TLLOG}
  fi
                            
  echo "`date` Rsyncing [${SMNT}/www_backup] to [${UMNT}/www_backup] Log:[${LMNT}/${LLOG}]"
  echo "`date` Rsyncing [${SMNT}/www_backup] to [${UMNT}/www_backup]" >>${LMNT}/${LLOG}
  /ffp/bin/rsync -av --stats --delete ${SMNT}/www_backup/ ${UMNT}/www_backup/ >${LMNT}/${TLLOG} 2>&1
  if [ -f "${LMNT}/${TLLOG}" ]; then
    echo -n "`date` "
    /ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
    echo -n "`date` "
    /ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
    echo -n "`date` Number of files removed: "
    grep -c "deleting " ${LMNT}/${TLLOG}
    cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
    rm ${LMNT}/${TLLOG}
  fi
fi

if [ -d "${LMNT}" ]; then
  echo "`date` Archiving the USB drive log files to [${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz]"
  /ffp/bin/tar -cz -f ${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz ${LMNT}/* >/dev/null 2>&1
fi

# Mount the Archive directory and copy files

if [ ! -d "/mnt/${aHost}" ]; then
  echo "`date` Creating mount point [/mnt/${aHost}]"
  mkdir "/mnt/${aHost}"
fi

echo "`date` Mounting [//${aHost}/archive] to [/mnt/${aHost}]"
/ffp/bin/mount -t cifs //${aHost}/archive /mnt/${aHost} -o ${UserPass}

if [ -f "/mnt/${aHost}/flag.txt" ]; then
  echo "`date` Moving [${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz] to [/mnt/${aHost}/${hostname}_Logs/]"
  if [ -f "${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz" ]; then
    /ffp/bin/mv "${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz" /mnt/${aHost}/${hostname}_Logs/
  else
    echo "`date` *** Something went wrong. Could not find [${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz]"
  fi
  echo "`date` Copying [${UMNT}/archive/*] to [/mnt/${aHost}/${hostname}_Logs/]"
  /ffp/bin/cp -uv ${UMNT}/archive/* /mnt/${aHost}/${hostname}_Logs/
else
  echo "`date` *** Something went wrong with the mount. Could not find [/mnt/${aHost}/flag.txt]"
fi

if [ ! -f "/mnt/${aHost}/${hostname}_Logs/${cnd}_${hostname}_Logs.tar.gz" ]; then
  echo "`date` *** Something went wrong with the move. Could not find [/mnt/${aHost}/${hostname}_Logs/${cnd}_${hostname}_Logs.tar.gz]"
fi

echo "`date` Syncing changed blocks to disk prior to unmounting the share"
sync

echo "`date` Unmounting [/mnt/${aHost}]"
/ffp/bin/umount /mnt/${aHost}
if [ -d "/mnt/${aHost}" ]; then
  # Check to ensure the share was fully unmounted before deleting the mount point
  if [ ! -f "/mnt/${aHost}/flag.txt" ]; then
    echo "`date` Deleting mount point [/mnt/${aHost}]"
    rm -fR "/mnt/${aHost}"
  else
   echo "`date` *** Something went wrong with the unmount. Found [/mnt/${aHost}/flag.txt]"
  fi
fi

echo -n "`date` Current System Temperature "
/ffp/bin/dns323-temp

echo "`date` Disk Usage Information"
/bin/df -h

if [ -f "${FLAG}" ]; then
  rm "${FLAG}"
fi
  
echo "`date` End of script [$0]"
  
#EoF
« Last Edit: May 13, 2013, 05:35:38 AM by dosborne »
Logged
3 x DNS-323 with 2 x 2TB WD Drives each for a total of 12 TB Storage and Backup. Running DLink Firmware v1.08 and Fonz Fun Plug (FFP) v0.5 for improved software support.

phanos

  • Level 1 Member
  • *
  • Posts: 8
Re: Shell script problems
« Reply #8 on: May 12, 2013, 02:03:23 PM »

Thanks dosborne I will keep looking into to it.
Logged