My script failed and can't fix it ?

Hi ,
I'd like to give you a little bit idea about my script which is used to get any generated file from remote server using ftp session then organized those file into directories based on their date ( at the end I supposed to have 1 months directories 20130401 20130402 ....20130430 ,
unfortunately my script which contains " two different block" went wrong whenever run it, please note that I run each block individually and success , could someone pass through every line & tell my what mistaken I did , thanks lot :-

#!/bin/sh
ddir=/dest     
sdir=/src/user/     
[ -d $sdir ] || mkdir -p  $sdir
echo  "$0 started: $(date)"
ftp -in x,x,x,x  << ENDFTP
user username password
cd $ddir
lcd $sdir
bin
prompt
mget *.text
bye
ENDFTP
echo  "$0 finished: $(date)"
cd $sdir
for dir in $( ls -1  | cut -c1-8 | sort -u ) ; do
    [ -d $dir ] || mkdir  -p $dir      
    for file in $( find $dir  -type f ) ; do
          echo "Move $file  to  $dir"
          mv  $file  $dir          
    done
done

:wall:

Change "find dir" to "find $dir".

Please use code tags as required by forum rules!

The only obvious mistake I can find at first glance is

for file in $( find  dir  -type f ) ; 
          

will not start the search in the respective directory; use find $dir instead.

no dears I already did that in my rel system , this mistake happened while copy paste , please find another clue ..:confused:

Please use code tags as required by forum rules!

And: post an execution log of your script running (= set the -x option)

1 Like

thanks for reminding me about the code tag , the change has been done ..

What's the error message? What's the specific problem? And please send an example of the directory structure. Then the dears can help you. :smiley:

1 Like

Why are the loops nested??
The find loop obscures the outer loop by moving files away.
Also the find loop finds the already moved files.
Untested propopsal:

for file in ?????????*
do
if [ -f "$file" ]
 then
  dir=$(echo "$dir" | cut -c1-8)
  mkdir -p "$dir"
  mv "$file" "$dir"
 fi
done
1 Like

I think the issue with this loop as you claimed but below line must be changed from $dir to $file , I will test it soon :

dir=$(echo "$file" | cut -c1-8)
1 Like

Yes you corrected it well.

The same issue I can not run it , I'd like to draw your attention if I test every block individually it works but as I mentioned I want to combine 2 blocks in one script ( block 1 is to get the files and block 2 is to organized the files into directories ) ...

Repost in this thread the script as you have it, with any corrections you have done. That way we can see the current situation. :slight_smile:

Dear Hanson44

#!/bin/sh ddir=/dest      sdir=/src/user/      [ -d $sdir ] || mkdir -p  $sdir echo  "$0 started: $(date)" ftp -in x,x,x,x  << ENDFTP user username password cd $ddir lcd $sdir bin prompt mget *.text bye ENDFTP echo  "$0 finished: $(date)" cd $sdir for dir in $( ls -1  | cut -c1-8 | sort -u ) ; do     [ -d $dir ] || mkdir  -p $dir           for file in $( find $dir  -type f ) ; do           echo "Move $file  to  $dir"           mv  $file  $dir               done done

See post #5. And: your post #13 is unreadable.

1 Like
+ ddir=/dest
+ sdir=/src/user/
+ [ -d /src/user/ ]
+ date
+ echo get_ftp.sh started: Tue Apr 23 15:39:33 MEST 2013
get_ftp.sh started: Tue Apr 23 15:39:33 MEST 2013
+ ftp -in 101.175.33.56
+ 0<<
user angel angel
cd /dest
lcd /src/user/
bin
mget *
bye
ENDFTP 
echo  "get_ftp.sh finished: + date
+ date
Tue Apr 23 15:39:33 MEST 2013" 
cd /usr/user
for file in * 
do      
  if [ -f  ]
  then
     dir=+ echo
+ echo

+ cut -c1-8
+ cut -c1-8

     mkdir -p 
     echo "MOVE   to "
     mv    
  fi
done
Local directory now /src/user/

:frowning:

---------- Post updated at 07:41 AM ---------- Previous update was at 07:35 AM ----------

I have posted the output from sh -x , if I run the script the ftp works and get the files from remote node , but locally the part for creating directories and organizing the files into them went wrong :confused::confused::confused: I can't detect any error in my script as I mentioned I tested each block alone it works successfully , but whenever combine "the ftp and the for loop " in one script it fails ,

:eek::eek::eek:

1) Thanks for the execution log.
2) That log does not seem come from a run of your previously posted script: The entire for dir... loop is missing.
3) looks like all variables are empty, maybe due to
4) you assing /src/user to sdir and use it to ftp files to. But then, in the log, you switch to /usr/user , which obviously is empty, thus producing the weird situation mentined in 3).

thanks I guess any statement after "here document " does not work , how can I overcome this issue :wall:

To help people help you, I've reformatted the really long, totally unreadable, one-liner script that was the last script you posted. Could you verify this is correct current script:

#!/bin/sh

ddir=/dest
sdir=/src/user/

[ -d $sdir ] || mkdir -p  $sdir
echo  "$0 started: $(date)"

ftp -in x,x,x,x  << ENDFTP
user username
password
cd $ddir
lcd $sdir
bin
prompt
mget *.text
bye
ENDFTP
echo "$0 finished: $(date)"
cd $sdir
for dir in $( ls -1  | cut -c1-8 | sort -u ) ; do
  [ -d $dir ] || mkdir  -p $dir
  for file in $( find $dir  -type f ) ; do
    echo "Move $file  to  $dir"
    mv  $file  $dir
  done
done

If it's supposed to be different from this, please post correct version. :slight_smile:

1 Like

. . . which does not fit the xtrace in post #15 . . . Thanks and acknowledgements for the effort, though.

appreciate your effort to help me , in fact I manage to resolve it by making ftp block as a function then re-call it , thanks a lot Hanson44