Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

unix and linux operating commands

how to direct scp output to a file in bash shell or script

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-04-2009   -   Original Discussion by NewSolarisAdmin
NewSolarisAdmin's Unix or Linux Image
NewSolarisAdmin NewSolarisAdmin is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 25 June 2010, 1:11 PM EDT
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
how to direct scp output to a file in bash shell or script

I can run this from the command line:
scp -i identfile /path/file_to_send remotelogin@remotebox:/path_to_put_it/file_to_send

and I get:
file_to_send 100% |***************************************************************************| 0 00:00

but if I do:
scp -i identfile /path/file_to_send remotelogin@remotebox:/path_to_put_it/file_to_send > logfile

I get nothing for output at the command line (which I would expect since I redirected it) and a file called logfile is created but it is empty.

I tried:
scp -i identfile /path/file_to_send remotelogin@remotebox:/path_to_put_it/file_to_send > logfile 2>&1

but still nothing...

Can someone please tell me what I am doing wrong?
Sponsored Links
    #2  
Old Unix and Linux 02-04-2009   -   Original Discussion by NewSolarisAdmin
quirkasaurus's Unix or Linux Image
quirkasaurus quirkasaurus is offline Forum Advisor  
Registered User
 
Join Date: Jan 2009
Last Activity: 9 September 2015, 11:46 AM EDT
Location: canton, michigan
Posts: 486
Thanks: 0
Thanked 10 Times in 10 Posts
you're doing nothing wrong.
scp detects when it's writing to a tty or a pipe or file or whatnot
and suppresses that *********** line if writing to a pipe or file.

Maybe just checking the $? status?
Sponsored Links
    #3  
Old Unix and Linux 02-04-2009   -   Original Discussion by NewSolarisAdmin
quirkasaurus's Unix or Linux Image
quirkasaurus quirkasaurus is offline Forum Advisor  
Registered User
 
Join Date: Jan 2009
Last Activity: 9 September 2015, 11:46 AM EDT
Location: canton, michigan
Posts: 486
Thanks: 0
Thanked 10 Times in 10 Posts
maybe someone knows how to fool scp but i don't...
i tried various things like:

echo | scp . . ..

( scp . .. . ) > log 2>&1

( thinking it was similar to "tty" ) but nothing worked.

Oh.... this worked, but I couldn't get it to work in a script:

script
scp . . .
exit
    #4  
Old Unix and Linux 02-04-2009   -   Original Discussion by NewSolarisAdmin
NewSolarisAdmin's Unix or Linux Image
NewSolarisAdmin NewSolarisAdmin is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 25 June 2010, 1:11 PM EDT
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
I can't figure anything out either... This is really bumming me out, seems like it should be easy.
Sponsored Links
    #5  
Old Unix and Linux 02-04-2009   -   Original Discussion by NewSolarisAdmin
avronius's Unix or Linux Image
avronius avronius is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 22 May 2013, 8:38 AM EDT
Location: Calgary
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Try running script beforehand and close it after scp'ing the file.
It won't suppress displaying it on the screen, but it will capture everything that happens during the script session.

Last edited by avronius; 02-04-2009 at 05:41 PM.. Reason: moved emphasis to the correct word...
Sponsored Links
    #6  
Old Unix and Linux 02-04-2009   -   Original Discussion by NewSolarisAdmin
NewSolarisAdmin's Unix or Linux Image
NewSolarisAdmin NewSolarisAdmin is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 25 June 2010, 1:11 PM EDT
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by avronius View Post
Try running script beforehand and close it after scp'ing the file.
It won't suppress displaying it on the screen, but it will capture everything that happens during the script session.
can I use the script command in a bash script?
Sponsored Links
    #7  
Old Unix and Linux 02-04-2009   -   Original Discussion by NewSolarisAdmin
curleb's Unix or Linux Image
curleb curleb is offline Forum Advisor  
Registered User
 
Join Date: Mar 2008
Last Activity: 27 March 2017, 3:48 PM EDT
Location: Surrounded...
Posts: 383
Thanks: 1
Thanked 29 Times in 28 Posts
The following is a function that I use in a variety of my scripts to enable an SCP session within the process. It pulls its params in from a config file that is parsed elsewhere to provide to this function's call. You can swap in your own vars or even static values to get a sense of what it's doing.

SCP (or SSH, for that matter...) is not overly verbose, unless you specifically tell it to be. The terminal output you see when in the shell is not captured, most likely since it speaks to security anyway. Best bet might be to track the return code(s) and debug as needed in the case of problems. You can, however, modify the verbosity some by incrementing the verbosity switch/flag on your particular scp client. This is seen in the ${verbosity} parameter shown in the function's actual scp call:



Code:
   ###########################
   #  function SCP_mthd ... 
   #     - process SCP transfer 
function SCP_mthd 
{ 

   $_dbg_mode

   print "\n\tGreetings from SCP_mthd ($@) "                 >>${MY_LOG} 2>&1 

   #  Scrap it if there aren't sufficient params... 
   if [[ -z ${param_num1} ]] \
         || [[ -z ${param_num2} ]] \
         || [[ -z ${param_num3} ]] \
         || [[ -z ${param_num4} ]] \
         || [[ -z ${param_num5} ]] \
         || [[ -z ${param_num6} ]] 
   then 
      print "\n ===\n\n ${my_name}: $(date) "                |tee -a ${MY_LOG} 
      print "\n   ${0} called w/ invalid argument(s)... "    |tee -a ${MY_LOG} 
      print "\n                                         "    |tee -a ${MY_LOG} 
      print "\n\tCalled as: $0 ${@} \n" \
         "\tParameters: ${param_num1} \n " \
         "\t            [ -------- ]        \n "  \
         "\t            ${param_num3:-"#"}  \n "  \
         "\t            ${param_num4:-"#"}  \n "  \
         "\t            ${param_num5:-"#"}  \n "  \
         "\t            ${param_num6:-"#"}  \n "  \
         |grep -v "#"                                        |tee -a ${MY_LOG} 
      error_flag='Y' ; logger_heads ;return 2 
   fi 

   #  Hereby reserving the param_num2 column for use as an alternate IdentityFile \
   #     in SSH-based calls (ie, -i "/..." for ssh/scp, -o "/..." for sftp, etc) 
   #     -  placing "not used" string within column in .cfg file will bypass \
   #           usage of alternate file 
   if [[ ! -z ${param_num2} ]] \
      && [[ ${param_num2} != "not_used" ]] 
   then 
      typeset  host_pass=$(print ${param_num2} |tr "_" " " |tr "+" "-" ) 
   fi 
   typeset -l  host_name="${param_num3}" 
   typeset -l  host_user="${param_num4}" 
   typeset     host_dest="${param_num5}" 
   typeset     locl_path="${my_file%/*[pf][rt][nm]}" 
   typeset     locl_file="${my_file#${locl_path}/*}" 

#  print       "host_name=${host_name}" 
#  print       "host_user=${host_user}" 
#  print       "host_pass=${host_pass}" 
#  print       "host_dest=${host_dest}" 
#  print       "locl_path=${locl_path}" 
#  print       "locl_file=${locl_file}" 

   scp ${verbosity} ${host_pass} ${my_file} ${host_user}\@${host_name}\:${host_dest}\/\. \
                                                             >>${MY_LOG} 2>&1 

   rc=${?} 

   if [[ ${rc} != 0 ]] 
   then 
      print "\n ===\n\n ${my_name}: $(date) "                |tee -a ${MY_LOG} 
      print "\n   ${my_mthd} had an error... "               |tee -a ${MY_LOG} 
      print "\n                                         "    |tee -a ${MY_LOG} 
      print "\n\tCalled as: $0 ${@} \n" \
         "\tReturn Code: ${rc} \n " \                        |tee -a ${MY_LOG} 
      error_flag='Y' ; logger_heads ;return 2 
   fi 

   echo "nicely done" 

}

Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Capture Shell Script Output To A File rajan_san Shell Programming and Scripting 4 01-13-2009 01:30 PM
Direct the output of a script to a log file autosys_nm Shell Programming and Scripting 5 08-27-2008 08:16 AM
help removing characters for output file in shell script wingchun22 Shell Programming and Scripting 1 08-02-2008 01:55 AM
Unix Shell Script with output to CSV File heather.morton@ UNIX for Advanced & Expert Users 1 01-28-2008 01:18 PM
direct output to a file then email it llsmr777 UNIX for Dummies Questions & Answers 2 11-21-2007 02:21 AM



All times are GMT -4. The time now is 09:10 PM.