0

I am using BASH to attempt to automate a very tedious manual scanning process.

I am very new to scripting and am learning as I go, but I've been googling and reading manuals for a day and a half and still haven't found anything to solve the following problem:

After SSHing into a remote server and then SCPing (sometimes thousands of) files from a second remote server...

1.) I need to count the number of files (inclusive of dotfiles (.pl, .xml, .sh, etc.)) in a directory and assign that numerical value as a variable (eg. $filecount)

2.) Then I need to use either an if or case statement to check if $filecount is >, = or < then 1. (if > 1 a previous SCP was successful, if = 1 the SCP failed or finally if < 1 something went really wrong)

Any guidance is greatly appreciated

Geordeaux
  • 11
  • 1
  • 3
  • 1
    `find /path/to/dir -maxdepth 1 -type f | wc -l` and to assign the result, simply use a *command substitution*, e.g. `filecount=$(find /path/to/dir -maxdepth 1 -type f | wc -l)` – David C. Rankin Jul 22 '19 at 06:06
  • Advise, just use `rsync`, e.g. `rsync -uav /path/to/dir user@remotehost:/path` to copy `dir` on local to `/path` on remote (resulting in `/path/dir` on remote) To check if your backup succeeds, `if rsync ...; then echo OK; else echo FAILED; fi`. No need to mess around with counts, etc.. See [rsync(1) - Linux manual page](http://man7.org/linux/man-pages/man1/rsync.1.html) – David C. Rankin Jul 22 '19 at 06:11
  • Thanks! I wasn't expecting such quick responses! thanks so much! – Geordeaux Jul 22 '19 at 06:29

1 Answers1

1

For counting the number of files I would use find and print a dot for each file. Then count the dots with wc:

find "${dir}" -maxdepth 1 -type f -printf "." | wc -c

If I would print the real file names and then count the number of lines, the solution would get confused by filenames with newlines in them, which is allowed on UNIX. Therefore I'm just printing a dot.

To store the results in a variable use command substitution:

nfiles=$(find "${dir}" -maxdepth 1 -type f -printf "." | wc -c)

I leave checking the value of $nfiles in an if statement as an exercise for you.


Btw, normally you would check if the scp command succeeded by checking it's exit state:

if ! scp ... ; then 
    echo "scp command failed"
fi
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • The 'dot' solution is a clever approach. – David C. Rankin Jul 22 '19 at 06:23
  • Thanks. :) iirc CharlesDuffy showed that once. I also liked it when I saw him using it. – hek2mgl Jul 22 '19 at 06:24
  • Thanks i'll give these both a try and let you know – Geordeaux Jul 22 '19 at 06:30
  • @hek2mgl Figures... Between he and Ed, they provide a wealth of shell education.... He and Ed are apparently co-workers, -- their employer is lucky to have them) – David C. Rankin Jul 22 '19 at 06:30
  • Yeah, I'm thankful for all I've learned from them, good to know that their employer supports it!! – hek2mgl Jul 22 '19 at 06:39
  • @hek2mgl RE: your edit checking the exit state. Could I just simply add an until-do loop around the SCP command and continue looping the scp until the exit status is successful? since there are sometimes hundreds and sometimes thousands of files... is the SCP dropped out and restarted wouldn't that result in duplicate files? then I could add a remove duplicates function? – Geordeaux Jul 22 '19 at 06:47
  • @Geordeaux Use `rsync` – hek2mgl Jul 22 '19 at 07:39