Search code examples
linuxshellputty7zip

7z not working from shell script under putty


I want to backup a file periodically using the 7z command.

Below is testscript.sh:

#!/bin/bash
SRCDIR="/var/www/html/folder2beBackedup/"
DESTDIR="/var/www/html/backups/"
FILENAME=bk_test-$(date +%Y%m%d%H%M%S).7z
7za a $DESTDIR$FILENAME $SRCDIR

I run the script in putty like so:

/bin/bash /var/www/html/testscript.sh

But putty returns this error: "Cannot find 1 file". See enter image description here

If I directly run the 7z command via putty then it works, but the same command is not working from testcript.sh.


Solution

  • I suspect there is a copy/paste error with your SRCDIR variable, as the WARNING: No more files output indicates that it is trying to scan a directory with no name. Simply specifying the wrong variable name (and thus no directory) would cause it to include the files from the current directory, so it's probably something more sinister like an invalid character in your script somewhere. Have you tried looking at the script with hexdump -C to ensure there's no weird stray characters anywhere?

    If I use a source directory named "/tmp/src/"^M, where ^M is literally the carriage return character (inserted in vim with ^V ^M), I get the same result as shown in your screenshot.

    Here's an excerpt from the bash tag wiki:

    1. Check whether your script or data has DOS style end-of-line characters

      • Use cat -v yourfile or echo "$yourvariable" | cat -v .

        DOS carriage returns will show up as ^M after each line.

        If you find them, delete them using dos2unix (a.k.a. fromdos) or tr -d '\r'