0

Below is my script, but it is not working after connecting to the sftp. Please help me with this.

I am not getting any error. It is not getting into the loop.

#!/bin/bash
CURRENT=`pwd`
File_Path=/appinfprd/bi/info/Client/Scripts/Test/
cd $File_Path
sftp STREAM@abc.sxm.com
for D in *;
do 
  echo $D;
  File_count=`find $D -type f| wc -l;`
  echo $File_Path$D '|' $File_count  >> File_count.txt
done
exit
Midhun
  • 331
  • 2
  • 5
  • 15
  • 1
    You are invoking `sftp` in interactive mode, so it's waiting for you to type some commands. Once you manually exit `sftp`, the remainder of your script will be executed. – tripleee May 30 '16 at 06:56
  • but before exit from sftp i want to count the no of files in all directories.How can i modify the script? – Midhun May 30 '16 at 07:01

1 Answers1

0

You are going to the file path before connecting with sftp to the target machine itself .

File_Path=/appinfprd/bi/info/Client/Scripts/Test/

So where do you want to count the files actually . In the source System or in target ?

If you want to run a set of commands in remote machine and store the result there , store these commands into a batch file like commands.batch,and the run like below :

sftp -b commands.batch STREAM@abc.sxm.com

And for running with ssh ,as it will be a better way to execute the commands you mentioned above ,store the commands in to file script.sh and run like below .

ssh -o BatchMode=yes abc.sxm.com sh -s < "script.sh"
Mathews Jose
  • 399
  • 6
  • 18
  • i want to count the no of files from sftp (Source System) – Midhun May 30 '16 at 07:09
  • Sorry . If you want to count the number of files in local why you are going into SFTP . So I am guessing you want to connect to remote machine .Then need to go to the path $File_Path . And then count the number of files in each subdirectory in this path there . is that right ? – Mathews Jose May 30 '16 at 07:32
  • Also I don't understand why we have to get this kind of requirement to done over sftp . If you want to implement something to get the number of files and all better done it with ssh itself .That will be easier I feels – Mathews Jose May 30 '16 at 07:39
  • Actually my clients will post files in sftp. i have to confirm that all the files have been posted. if not i have to alert customer. – Midhun May 30 '16 at 08:35
  • @ Mathews Jose i have modified my script like #!/bin/bash URL=STREAM@xfer.sxm.com TMPFILE=/appinfprd/bi/infogix/Client/Scripts/Test/File_count_sftp.txt echo 'ls -1' > $TMPFILE function handle_dir { echo "====== $1 =========" local dir=$1 sftp -b $TMPFILE "$URL:$dir" | tail -n +2 | while read info; do echo "$info" if egrep -q '^d' <<< $info; then info=$(echo $info) subdir=$(cut -d ' ' -f9- <<< $info) handle_dir "$dir/$subdir" echo $1 '/' $subdir >> $TMPFILE fi done } handle_dir "." Now i can print path not no of files. – Midhun May 30 '16 at 17:27
  • @Midhun yes ..I too saw the answer in the link added to the question . new info :) – Mathews Jose May 30 '16 at 17:29
  • ,Again for counting the no of files we can use printf 'cd Inbound\nls\n' | sftp -b - user@host | wc -l . But i dont understand where should i add this command. – Midhun May 30 '16 at 17:32
  • I believe you can add the below lines before the call to : handle_dir "$dir/$subdir" ####################################### echo "cd $dir/$subdir ; wc -l " > tmpf sftp -b tmpf user@host > tempresult echo "Number of files is `cat tempresult`" rm tmp tempresult Try once and let me know – Mathews Jose May 30 '16 at 17:43
  • Its not working. It is creating a files but does not have any data in it. – Midhun May 31 '16 at 03:51
  • try like below : **`echo "ls -1 " > tmpf ; sftp -b tmpf user@host:$dir/$subdir > tempresult ; echo "Number of files is \`cat tempresult\`" ; rm tmp tempresult` ** – Mathews Jose May 31 '16 at 04:19
  • Got created a tmp file with output of ls -l – Midhun May 31 '16 at 04:43
  • Got the output by using this ---- var="$(printf "cd $dir/$subdir\nls\n" | sftp -b - $URL | wc -l)" ------ Command. – Midhun May 31 '16 at 05:27