Bash Output at the end of program -


i'm new bash scripting , i'm working on bash script right that's supposed download list of files server using scp command. however, there's no guarantee file there, want able output list of files not found. currently, i'm doing output error message:

for filename in $@ scp $server:/home/usr/$filename .   if [ $? -ne 0 ]     echo "warning: $filename not found in $server."   fi done 

the code above works knowledge, problem i'm having due how scp works, there's going ton of text on screen (since each scp command going show 100% downloaded line) , miss warning message or two. there easy way me have of warning messages print @ end, having list files not found?

edit: forgot clarify why i'm using echo instead of default "file not found" - code supposed in multiple locations if can't find in first place. so, more complete version looks kind of this:

for filename in $@ scp $server:/home/usr/$filename .   if [ $? -ne 0 ]       scp $server:/home/usr/documents/$filename .       if [ $? -ne 0 ]          scp $server:/home/usr/documents/local/$filename .          if [ $? -ne 0 ]            echo "warning: $filename not found in $server."          fi       fi   fi done 

if doesn't find in first location, that's fine, want throw error if can't find in of locations.

the simplest (though not efficient solution) check file existence @ end.

add after original loop:

for filename in $@     if [ -s "$filename" ]         echo "warning: $filename not found in $server." >&2     fi done 

alternatively if normal scp output isn't interesting (and don't care other potential scp failures) original loop become:

for filename in $@ scp -q $server:/home/usr/$filename . 2>/dev/null   if [ $? -ne 0 ]       scp -q $server:/home/usr/documents/$filename . 2>/dev/null       if [ $? -ne 0 ]          scp -q $server:/home/usr/documents/local/$filename . 2>/dev/null          if [ $? -ne 0 ]            echo "warning: $filename not found in $server."          fi       fi   fi done 

if want scp output (normal or error or both) also want succinct list of missing files @ end this:

missing_files=() filename in $@ scp $server:/home/usr/$filename .   if [ $? -ne 0 ]       scp $server:/home/usr/documents/$filename .       if [ $? -ne 0 ]          scp $server:/home/usr/documents/local/$filename .          if [ $? -ne 0 ]            missing_files+=("$filename")          fi       fi   fi done  echo 'missing files:' printf %s\\n "${missing_files[@]}" 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -