'*' not working in shellscript


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting '*' not working in shellscript
# 15  
Old 01-03-2013
Quote:
Originally Posted by Bopty
Hi Don,

Thanks for you mail.

Your confusion is due to typing issue. Let me clearly explain you.

1.) I try to move files from one directory to another, so used a below command,

Code:
mv ${DATA_DIR}\ABC_*.dat ${ARCHIVE}

The Value for DATAA_DIR and ARCHIVE are,

Code:
${DATA_DIR} = /u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data
${ARCHIVE} = /u01/data/oracle/rms/UPGRMSBATCH/archive

2.) If i run the above mv command inside my shellscript, im getting the error like

Code:
mv: cannot stat `/u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data/ABC_*.dat': No such file or directory

But if i run the same command (i.e:
Code:
mv ${DATA_DIR}\ABC_*.dat ${ARCHIVE}

) outside the shell script, then it works fine.

3.) Instead of using '*' if i used the complete file name (i.e: ABC_20120103) its working fine inside the shell script itself. But i don't want to do that, since n number of files with different date format is present in the directory. So i want to move all the files from data directory to archive directory.

I hope now you are clear with my issue.

Thanks,
Bopty
I think I am clear with your issue. You refuse to see that the backslash character (\) and the slash character (/) are different.

You say that the following:
Code:
mv ${DATA_DIR}\ABC_*.dat ${ARCHIVE}

works on the command line, but don't understand why the following:
Code:
mv ${DATA_DIR}/ABC_*.dat ${ARCHIVE}

doesn't work in your script.

Please execute the following commands exactly and post the results:
Code:
uname -a
ls -l /u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data\ABC_*.dat
ls -l /u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/dataABC_*.dat
ls -l /u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data/ABC_*.dat

On UNIX and Linux systems, the / character separates components in pathnames. On UNIX and Linux systems, the shell uses \ as an escape character; not to separate a directory name from the name of a file in that directory. Therefore, the command:
Code:
mv ${DATA_DIR}\ABC_*.dat ${ARCHIVE}

and the command:
Code:
mv ${DATA_DIR}/ABC_*.dat ${ARCHIVE}

are VERY different. If the first one is working on the command line and the second one is not working in your script; use the first one in your script.
Note that the first one uses the asterisk (*) to expand to a list of files that match the pattern dataABC_*.dat in the directory:
Code:
/u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS

while the second one uses it expand to a list of files that match the pattern ABC_*.dat in the directory:
Code:
/u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data

# 16  
Old 01-18-2013
Hi All,

Thanks for you help. Here is the answer,
Code:
ls ${DATA_DIR} | grep "ABC" | while read x; do echo $x; mv ${DATA_DIR}/$x ${ARCHIVE}; done


Thanks,
Bopty Smilie

Last edited by Franklin52; 01-18-2013 at 04:18 AM.. Reason: Please use code tags for data and code samples
# 17  
Old 01-18-2013
Quote:
Originally Posted by Bopty
Hi All,

Thanks for you help. Here is the answer,

ls ${DATA_DIR} | grep "ABC" | while read x; do echo $x; mv ${DATA_DIR}/$x ${ARCHIVE}; done

Thanks,
Bopty Smilie
If the above command works, your current working directory has to be $DATA_DIR. In that case, the commands:
Code:
printf "%s\n" $PWD/*ABC*;mv *ABC* $ARCHIVE

will do the same thing MUCH more efficiently, but it will print all of the filenames before it moves any of them.

If it is important to list each filename just before it is moved, the MUCH less efficient commands:
Code:
for i in *ABC*
do      echo $PWD/$i
        mv $i $ARCHIVE
done

will still be more efficient than the script you're currently using.
# 18  
Old 01-18-2013
And if there is not any, test it or you get some unwanted action. Example:
Code:
for i in *ABC*
do     
        [ "$i" = "*ABC*" ] && continue  # there wasn't any *ABC* files 
        echo $i
        mv $i $ARCHIVE
done

If file name generation result is 0 files, then what you are doing ?
You are doing something for filename *ABC*.

Code:
touch *ABC*   # change files timestamp if you have *ABC* files
                    # but if not, touch will create filename *ABC*

# 19  
Old 01-18-2013
Quote:
Originally Posted by kshji
And if there is not any, test it or you get some unwanted action. Example:
Code:
for i in *ABC*
do     
        [ "$i" = "*ABC*" ] && continue  # there wasn't any *ABC* files 
        echo $i
        mv $i $ARCHIVE
done

If file name generation result is 0 files, then what you are doing ?
You are doing something for filename *ABC*.

Code:
touch *ABC*   # change files timestamp if you have *ABC* files
                    # but if not, touch will create filename *ABC*

Good point. From the description of the problem, I was under the impression that there would always be at least one match, but it would be better to verify that there is a match. The test can be inside the loop, or an if could be added before the loop that exits if there is no match. I believe some shells also have a set special built-in utility option that causes patterns that contain wildcard characters to be removed from the list if there are no matching files.

In my script, if there is no match, $PWD/*ABC* will be printed (with the asterisks) and mv will print a file not found diagnostic message and terminate with a non-zero exit status causing the script to return a non-zero exit status.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Telnet shellscript

cat << EOF | telnet alt1.aspmx.l.google.com 25 HELO verify-email.org MAIL FROM: <check@verify-email.org> RCPT TO: <test@gmail.com> quit EOF Hello, I'm trying to get the result of that execution, and can not see the result or bring it to a txt ... the direct command in ssh running the result... (5 Replies)
Discussion started by: c0i0t3
5 Replies

2. Shell Programming and Scripting

Help with shellscript

I am new in shell script i want to convert .txt file in the format axsjdijdjjdk to a x s j d i j d j j d k (5 Replies)
Discussion started by: sreejithalokkan
5 Replies

3. Shell Programming and Scripting

Needed shellscript for the following

hi all, i need the shell script for he below requirement i had the input file as a_20121217_035120( frmat is a_date_hhmmss) a_20121217_035128 a_20121217_035456 a_20121217_035767 a_20121217_035178 a_20121217_035189 a_20121217_035220 my output should be a_20121217_035456... (0 Replies)
Discussion started by: hemanthsaikumar
0 Replies

4. UNIX for Dummies Questions & Answers

How can I do aliasing in shellscript?

#Example.sh alias rmv 'sh Example2.sh' when i execute exapme.sh alias name not working. how i solve this problem?? (9 Replies)
Discussion started by: arun508.gatike
9 Replies

5. Shell Programming and Scripting

Need help with shellscript

Hello. I am a novince at writing shell scripts but here is the question. I have to write a shell script that does the following: Once executed via crontab, the script should do the following: a. get date/time stamp in for format 10-MAR-05 and b. execute shell script my_script.sh (which... (2 Replies)
Discussion started by: jigarlakhani
2 Replies

6. UNIX for Advanced & Expert Users

shellscript problem

hI, Pls consider the following shell script #!/bin/csh -f sqlplus tkyte/tkyte <<"EOF" > tmp.csh set serveroutput on declare a number:=5; begin dbms_output.put_line( 'a:='||a ); end; / spool off "EOF" The above script does the followin 1)it connects... (1 Reply)
Discussion started by: ravi raj kumar
1 Replies

7. Programming

Shellscript for MQSeries

Iam new to shellscript. 1)How to strart QUERYMANAGER using shellscript. 2)How to put and get messages in MQSeries using shellscripts. 3)iam using local queues . Thanks lot. (0 Replies)
Discussion started by: ram2s2001
0 Replies

8. Shell Programming and Scripting

Another shellscript question

Folks; on a unix server I have a mapping file which holds a list mountpoints of all databases and their mountpoints. tab delimited or colon deliminted..I needed to copy the datafiles from the pristine mountpoints to test's mountpoints in this case. I needed to do this by passing sid name using... (18 Replies)
Discussion started by: jigarlakhani
18 Replies

9. UNIX for Advanced & Expert Users

ftp in Shellscript

Can I do something like this from a shellscript ?: ftp 12.34.56.78 <<! username password put a.c bye ! It does not go through as the login fails. Is there any alternative to do the above requirement? Thanks in advance. Gowrish (3 Replies)
Discussion started by: ggowrish
3 Replies
Login or Register to Ask a Question