Sponsored Content
Top Forums Shell Programming and Scripting how to direct scp output to a file in bash shell or script Post 302284091 by curleb on Wednesday 4th of February 2009 06:00:07 PM
Old 02-04-2009
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" 

}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

scp output fm script won't go to file

Have a script that scp's tar file to multiple other servers in a for loop. Need to set monitoring and notification on it for when it fails. Running this line of code in a 'for' loop... scp $SOURCE_RECOVERY_TARFILE ${HOST}:${CURR_RECOV_TARFILE} 2>&1 | tee ${MONFILE} Their are two outputs... (7 Replies)
Discussion started by: nmikes
7 Replies

2. UNIX for Dummies Questions & Answers

direct output to a file then email it

Ok so i have this script and I dont know how to have the output go to a file and then email that file to someone. #!/bin/ksh print "AL" print "AM" print "AN" print "RL\n" nawk '/PROD/ {print $3, $2}' /home/user/switch_listtest | sort -k1,2 print "End of Report" Thank you in... (2 Replies)
Discussion started by: llsmr777
2 Replies

3. Shell Programming and Scripting

Direct the output of a script to a log file

Hi, I have a script to compare 2 files. file1=$1 file2=$2 num_of_records_file1=`awk ' END { print NR } ' $file1` num_of_records_file2=`awk ' END { print NR } ' $file2` i=1 while do sed -n "$i"p $file1 > file1_temp sed -n "$i"p $file2 > file2_temp diff file1_temp... (5 Replies)
Discussion started by: autosys_nm
5 Replies

4. UNIX for Dummies Questions & Answers

How do i tell my bash shell script to test the output of a command against something

How do i tell my bash shell script to test the output of the command i'm using?? I want this script to look for lines not equal to 1 then let me know.. $ cat blah ; echo ---- ; cat blah.sh 1 fe 1 fi 1 fo 0 fum 1 blahda 1 blah 0 blahh 1 bla 1 bl 1 blahhh ---- #!/bin/bash while... (1 Reply)
Discussion started by: phpfreak
1 Replies

5. Shell Programming and Scripting

Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute

:wall::wall::wall: Hi I have horrible script below, need help in renaming ls -l output into new filename format: Desired output: cp -pv original_path/.* newDirectory/owner_of_file.%dd%mm%y.file_extension.first_8_characters_of_original_filename localuser@localuser:~ vi... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

6. Shell Programming and Scripting

Addition to Bash shell script that emails final output as attachement?

Greetings. I have a nice bash shell script that runs a multi-step analysis well. I already have the SGE options set up to email me the progress of the run (started, completed, aborted), but a final step would be to code the shell script to email the final output (a .txt file) to the same email... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

7. Shell Programming and Scripting

Manipulating sed Direct Input to Direct Output

Hi guys, been scratching round the forums and my mountain of resources. Maybe I havn't read deep enough My question is not how sed edits a stream and outputs it to a file, rather something like this below: I have a .txt with some text in it :rolleyes: abc:123:xyz 123:abc:987... (7 Replies)
Discussion started by: the0nion
7 Replies

8. UNIX for Dummies Questions & Answers

Output of ssh command from localhost - direct to local file.

Hi, i'm trying to gather details from remote hosts and want them to be written to my local linux machine from where i'm using SSH. My command looks some thing like this ssh -q remotehost 'bash -s' <command.txt where command.txt is a file in my local machine containing ps -ef |grep httpd |... (1 Reply)
Discussion started by: poga
1 Replies

9. Shell Programming and Scripting

Disk Space Script to direct output

Hi, I am working on Sun Solaris 5.10 and want to direct the output from a disk space check script to an output file; #!/bin/bash CURRENT=$(df -k /log/logs | grep /log/logs | awk '{ print $5}' | sed 's/%//g') THRESHOLD=30 if ; then echo "Remaining free space is low" > output.txt else... (10 Replies)
Discussion started by: SSKAAB
10 Replies

10. Shell Programming and Scripting

For loop in bash - Direct output to two files

Hello all, i have a code in which when doing a for loop, i need to direct the output to two files, one just a single output, the other to always append (historical reasons). So far i managed to do the following, which is working, but am still considering it as "dirty". ... (4 Replies)
Discussion started by: nms
4 Replies
tee(1)							      General Commands Manual							    tee(1)

NAME
tee - pipe fitting to copy standard output to file SYNOPSIS
[file]... DESCRIPTION
The command transcribes the standard input to the standard output and makes copies in the files. Options This option ignores interrupts. This option appends the output to the files rather than overwriting the files. EXTERNAL INFLUENCES
Environment Variables determines the language in which messages are displayed. If is not specified in the environment or is set to the empty string, the value of is used as a default for each unspecified or empty vari- able. If is not specified or is set to the empty string, a default of "C" (see lang(5)) is used instead of If any internationalization variable contains an invalid setting, behaves as if all internationalization variables are set to "C". See environ(5). International Code Set Support Single- and multibyte character code sets are supported. RETURN VALUE
The command returns zero upon successful completion, or nonzero if the command fails. EXAMPLES
Write a list of users to the screen and also append the list to the file STANDARDS CONFORMANCE
tee(1)
All times are GMT -4. The time now is 12:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy