Mv command in for loop - not working

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Mv command in for loop - not working
# 1  
Old 10-10-2017
Mv command in for loop - not working

HI Folks -

I'm very frustrated - I'm trying to execute a verys imple for loop and rename the files if they exist.

here is my loop :

Code:
ydate=`TZ=aaa24 date +%m%d`   
CR_YR=$(date "+%Y")
echo $ydate
echo ${CR_YR}

cd /hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/item/itmsales

for A in AWID601 GSCD601
do
    if ls $A${CR_YR}* 1> /dev/null 2>&1
    then
        echo "$A is available for processing"
        mv ${A}${CR_YR}*.TXT ${A}${ydate}.TXT
    else
        echo "$A does not exist - data import process aborted"
        exit 1
    fi  
done

But it says the files exist, however getting an error when trying to "mv" them.

Code:
10/10/17
StartTime:
04:15:40 AM
1009
2017
AWID601 is available for processing
Usage: mv [-I] [ -d| -e] [-i | -f] [-E{force|ignore|warn}] [--] src target
   or: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src1 ... srcN directory
GSCD601 is available for processing
Usage: mv [-I] [ -d| -e] [-i | -f] [-E{force|ignore|warn}] [--] src target
   or: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src1 ... srcN directory
$

I can execute the mv command manually on each file with success. Do you see anything off here?

File names I'm trying to rename:
GSCD6012017TEST.TXT
AWID6012017TEST.TXT
# 2  
Old 10-10-2017
It's strange that you're trying to move what could be multiple files into a single one.

e.g.
Code:
$ touch 1 2 3 4
$ mv 1 2 3 4
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory

Change
Code:
mv ${A}${CR_YR}*.TXT ${A}${ydate}.TXT

To:
Code:
echo "mv ${A}${CR_YR}*.TXT ${A}${ydate}.TXT"

What does that say?

You should probably change the for-loop spec to use the actual filenames (or better a while-loop for handling files in this way)
# 3  
Old 10-10-2017
I'll second what Scott suggested. Put an echo before the mv to see what is going on.

My suspicion is that one of these:

Quote:
Originally Posted by SIMMS7400
Code:
ydate=`TZ=aaa24 date +%m%d`   
CR_YR=$(date "+%Y")

contain extra blanks or other empty space which messes up your filenames. But you won't see them because:


Quote:
Originally Posted by SIMMS7400
Code:
echo $ydate
echo ${CR_YR}

would not show them. If you want to test-display what is in a variable do it always like this:

Code:
echo "ydate is: \"$ydate\""

because this would make surrounding white space, hidden NLs or similar non-printable stuff stand out.

I hope this helps.

bakunin
# 4  
Old 10-10-2017
I had this behaviour in HPUX. We had the understanding that if multiple source files were specified, then the last one mentioned would be overwritten with the penultimate one, however HPUX insisted that multiple files had to be copied to a directory.

So:-
AIX/RHEL etc.
Code:
cp a b c d e f             # Create/Overwrite f with e or copy all into directory f

HPUX
Code:
cp a b c d e f             # Errors unless f is a directory


I can see arguments for both designs, but I think HPUX is probably correct because it is strict. Not that I enjoyed having to recode chunks of scripts as we migrated ........ Smilie


I hope that this helps,
Robin

Robin
# 5  
Old 10-10-2017
Hi All!

Thank you so much for the feedback!

So, here is my output:

Code:
10/10/17
StartTime:
06:19:17 AM
1009
2017
AWID601 is available for processing
mv AWID6012017*.TXT AWID6011009.TXT
GSCD601 is available for processing
mv GSCD6012017*.TXT GSCD6011009.TXT
$

Does that look weird? I can't see anything out of the ordinary...

---------- Post updated at 06:27 AM ---------- Previous update was at 06:21 AM ----------

Also, I'm not trying to move multiple files into 1.

GSCD6012017TEST.TXT transforming in GSCD6011009.TXT
AWID6012017TEST.TXT transforming into AWID6011009.TXT
# 6  
Old 10-10-2017
Well, suggesting to echo the mv wasn't my brightest - it was obvious what the result would be!

I can only suggest that the AWID6012017*.TXT returns multiple files, and you need to iterate through them otherwise you're trying to move multiple files into a single one, and mv takes a source and a target, and as the target is not a directory is this case, it's complaining.

Something like:
Code:
for A in AWID601 GSCD601; do
  for F in $A$CR_YR*.TXT; do
    echo mv $F ${F/$A$CR_YR/$A$ydate}
  done
done

A test:
Code:
ydate=`TZ=aaa24 date +%m%d`
CR_YR=$(date "+%Y")

# create some test files
for c in {1..5}; do
  for A in AWID601 GSCD601; do
    touch $A$CR_YR$RANDOM.TXT
  done
done

for A in AWID601 GSCD601; do
  for F in $A$CR_YR*.TXT; do
    echo mv $F ${F/$A$CR_YR/$A$ydate}
  done
done

mv AWID601201715991.TXT AWID601100915991.TXT
mv AWID601201721628.TXT AWID601100921628.TXT
mv AWID601201727511.TXT AWID601100927511.TXT
mv AWID601201730768.TXT AWID601100930768.TXT
mv AWID6012017823.TXT AWID6011009823.TXT
mv GSCD601201720182.TXT GSCD601100920182.TXT
mv GSCD601201724525.TXT GSCD601100924525.TXT
mv GSCD601201724813.TXT GSCD601100924813.TXT
mv GSCD60120175060.TXT GSCD60110095060.TXT
mv GSCD60120177596.TXT GSCD60110097596.TXT

# 7  
Old 10-10-2017
Wow Scott - thank you.

Yes, that was exactly it. The directory is massive and I didn't realize there were other similarly named files out there. What I have done is create an archive process to be executed once the processing is complete.

Then when the ftp process lands the new files, there wont be any conflicts.

My code is as follows:

Code:
Archive() {

cd /hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/dailyinv/dailyinv

_ARCHIVEPATH=Archives/AWI_GSC_Archives/${CR_YR}_${ydate}/
mkdir -p "${_ARCHIVEPATH}"

for A in GSCXMITINV AWIXMITINV
do
    if ls ${A}* 1> /dev/null 2>&1
    then
        echo "$A is available for processing"
        mv ${A}* "${_ARCHIVEPATH}"
    else
        echo "$A does not exist - file will not be archived"
    fi  
done

cd /hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/item/itmsales

_ARCHIVEPATH=Archives/AWI_GSC_Archives/${CR_YR}_${ydate}/
mkdir -p "${_ARCHIVEPATH}"

for A in AWID601 GSCD601
do
    if ls ${A}* 1> /dev/null 2>&1
    then
        echo "$A is available for processing"
        mv ${A}* "${_ARCHIVEPATH}"
    else
        echo "$A does not exist - file will not be archived"
    fi  
done

}

cd /hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/dailyinv/dailyinv

for A in GSCXMITINV AWIXMITINV
do
    if ls ${A}${CR_YR}* 1> /dev/null 2>&1
    then
        echo "$A is available for processing"
        mv ${A}${CR_YR}*.TXT ${A}${ydate}.TXT
    else
        echo "$A does not exist - data import process aborted"
        exit 1
    fi  
done

cd /hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/item/itmsales

for A in AWID601 GSCD601
do
    if ls ${A}${CR_YR}* 1> /dev/null 2>&1
    then
        echo "$A is available for processing"
        mv ${A}${CR_YR}*.TXT ${A}${ydate}.TXT
    else
        echo "$A does not exist - data import process aborted"
        exit 1
    fi  
done

cd /hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/scripts/tivoli/dailysales

perl 2passdatechange.pl
sleep 60
startMaxl.sh oraclez.mxl ${CR_YR}

#::-- Archive data files after execution --::#
Archive

Thank you all for your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For-loop not working

Hello all, I would like to unzip some files with a for-loop. Is there anyone who could tell me how I should do this - in a correct way? for file in $(ls); do echo gzip -d < $file | tar xf -; done The problem is the pipe - I believe. But how could I do it? I need it for the command... (4 Replies)
Discussion started by: API
4 Replies

2. Shell Programming and Scripting

Cut command not working in for loop

grep -Fxvf testdata.xls file_GTDA1.xls >file_GTDA.xls SLS_COUNT=`grep 'GTDA_Dly_Sls' file_GTDA.xls |wc -l` PMIX_COUNT=`grep 'GTDA_Dly_Pmix' file_GTDA.xls |wc -l` if ; then var1=`cat file_GTDA.xls|grep 'GTDA_Dly_Sls_'` var4="|" for i in $var1... (7 Replies)
Discussion started by: renuk
7 Replies

3. Programming

C While Loop not working like C++

Hi new to C give me a hand thank. do { bzero(input,256); printf("Please enter the country name!"); scanf("%s",input); //& character is essential for scanf(), %f = float, %399s as the last character to be the null character ('\0'). } while... (5 Replies)
Discussion started by: GQiang
5 Replies

4. Shell Programming and Scripting

For loop not working

Hi All, For loop in ksh not working if it was given in the following method. simple script: for i in {1..4} do echo $i done Output: {1..4} Even below also not working :( Script: for (( c=1; c<=5; c++ )) do echo "Welcome $c times..." done Output: ./x.sh: 0403-057 Syntax error... (13 Replies)
Discussion started by: girish_satyam
13 Replies

5. Shell Programming and Scripting

While loop not working

here we go.. While loop is not working file.. It also invokes one more shell scripts for which parameters need to passed on. while read line do #### #### We want to have a logfile for each load #### PLog="${LogDir}/${Script}.log"; #### ... (5 Replies)
Discussion started by: premkumardr
5 Replies

6. UNIX for Advanced & Expert Users

If loop is not working

The following piece of code is not running because it is fails to go inside the if condition. i want to create a directory if there is no directory in the input path. i am using Linux, by CENT. Please help. echo " Enter the path where you u want to extract the tar" read EXTRACT_PATH ls -ld... (12 Replies)
Discussion started by: toanilsharma1
12 Replies

7. UNIX for Dummies Questions & Answers

If then else loop not working

If then else segment of below code is not working. For each filename code is displaying output for if part as well as else part. Please help its urgent. for usercusttop in `echo ${filename}|sort|uniq|cut -c 1-${actualwordcount}` do ... (2 Replies)
Discussion started by: findprakash
2 Replies

8. Shell Programming and Scripting

For loop not working...! :(

Could some one help me on this... For loop is working...! for x in $i do for y in $j do z=`echo $y | awk '{print $2}'` if then FS=`/usr/bin/echo $y` echo $FS >>$Basic_location/out.csv fi done CPRT="Cpoyright @ BTOIDCIM" done (3 Replies)
Discussion started by: bullz26
3 Replies

9. Shell Programming and Scripting

if loop is not working

Hello i am trying to remove a line using an input file , but this depends upon user interaction here is the sample #!/bin/sh echo "Please enter whether you want to remove Profile" read value1 if ;then sed /movie/d temp.txt> temp3.txt else echo "Script didnot remove profile" fi ... (3 Replies)
Discussion started by: ranga27
3 Replies

10. Shell Programming and Scripting

Loop not working

Apologize if this is doesn't come under this group. I have a small script to find out users who last logged in to check there mail. (Tru 64 4.0, Netscape mail 3.6) -----> cat $1|awk -F: '$2=="SMTP-Accept" && $5~/@maildomain/ {s=$5;u++;las=substr($1)} END {for (i in u) {print... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question