-1

I have below script but I can't find the error. Somebody an help me ?

In concrete I split a big file in different then compress any file, move it and send by ftp rename destination filename.

Something not work :(

in line: put ${file} ${7}.T${j}(+1)

I try to rename the file with (+1) ended new filename

Kind regards

#!/bin/bash

# configuration stuff

# ${1} absolute path file
# ${2} num_files
# ${3} output_filename
# ${4} ipMainframe ip to put files
# ${5} FTP username
# ${6} FTP password
# ${7} destination filename

if [ ! $# == 7 ]; then
        #number of parameter different of two
        echo "Number of parameter incorrect"
        echo "Command use: LLP_split_gzip_sendFTPandTrigger.sh absolute_path_file number_of_pieces output_filename ipMainframe userFTP pwdFTP destinationFilename"
        exit 1
fi

if [ -f ${1} ]; then
        # If file exists
        if [[ ${2} =~ ^[\-0-9]+$ ]] && (( ${2} > 0)); then
                # if number of pieces is an integer > 0
                #Remove old files
                echo "home directory = $HOME"
                CMD=`rm -f '"$HOME"/"$3"*'`
                if [ $? != 0 ]; then
                        echo "Impossible to remove old files $home/llp_tmp* $home/"$3"* in home directory"
                        echo $CMD
                fi
                # Calculate line for every file splitted
                total_lines=$(cat ${1} | wc -l)
                ((lines_per_file = (total_lines + ${2} - 1) / ${2}))
                # Split the actual file, maintaining lines.
                CMD=`split -l "$lines_per_file" "$1" "$HOME"/llp_tmp`
                if [ $? != 0 ]; then
                        echo "SPLITTING FILE ERROR: problem to split file."
                        echo $CMD
                        exit 3
                fi
                #For every file splitted rename and zip it
                i=1
                for file in $HOME/llp_tmp*; do
                        CMD=`mv "$file" "$3"."$i"`
                        if [ $? != 0 ]; then
                                echo "Impossible to rename file"
                                echo $CMD
                                exit 5
                        fi
                        CMD=`gzip "$3"."$i"`
                        if [ $? != 0 ]; then
                                echo "Impossible to compress file $3.$i"
                                echo $CMD
                                exit 6
                        fi
                        i=`expr $i + 1`
                done
                ftp -n ${4} << EOF

                j=1
                user ${5} ${6}
                for file in $3.*; do
                        put ${file} ${7}.T${j}(+1)
                        j=`expr $j + 1`
                done
                quit
        else
                echo "number of pieces second parameter must be more than 0."
                exit 2
        fi
else
        echo "absolute path first paramater doesnt exist"
        exit 1
fi
exit 0
SkyBlackHawk
  • 95
  • 10

2 Answers2

2

You are not terminating your here document. When I run your script I get:

gash.sh: line 72: warning: here-document at line 54 delimited by end-of-file (wanted `EOF')
gash.sh: line 73: syntax error: unexpected end of file

ftp -n ${4} << EOF is the issue. Where is your here document?

The warning says it all, you don't have an EOF marker. Note that this MUST NOT BE INDENTED! The EOF must be in "column 0" and have no trailing characters, including whitespace.

Edit: It appears you want to use program constructs within a single FTP session - I don't know of a way of doing that in Bash. Perl has an easy to use FTP module where you can do it, simple example:

use strict;
use Net::FTP;

my $ftp = Net::FTP->new ("hostname");
$ftp->login ("username", "password");
$ftp->binary ();

for my $file (glob("$ENV{HOME}/llp_tmp*")) {

    $ftp->put ($file); 
}
$ftp->quit();
cdarke
  • 42,728
  • 8
  • 80
  • 84
  • but If I substitute this line with: CMD=`ftp -n "$4" << EOF` give me the same error. My problem is open FTP session in not interactive mode and put files renaming them by a loop but I need to add an EOF when I call FTP -n command for not interactive mode. Do you know How I can solve it ? – SkyBlackHawk Sep 25 '12 at 16:32
0

You don't need parentheses around +1.

Change it to:

put "${file}" "${7}.T${j}+1"

It's good practice to quote variables.

Another tip: Instead of j=`expr $j + 1`, you can simply use ((j++)).

dogbane
  • 266,786
  • 75
  • 396
  • 414