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
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
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 ) ...
+ 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/
---------- 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 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 ,
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).
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.