Problem with find


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with find
# 15  
Old 11-06-2013
Quote:
Originally Posted by tempestas
Hello,

Thanks SO much! It does work!
Unfortunately, I don't understand every step. May I ask for explanations?
In particular: IAm=${0##*/}
I have never seen this. If I echo $IAm the output is the name of the script that is called by the list, i.e. "rename.sh". What are the ##*/ meaning? 0 marks the argument from the list, right?

The other thing I don't get are the exits exit 2; exit 3; exit 4. Why are they numbered?

lastname="${2%%_*}"
2 again stands for the argument, right? What is the %%_* saying?

ymd="${3:6}${3:3:2}${3:0:2}"
First, I thought I understood for the day that the first number in the brackets {3:0:2} refers to the argument from the list, and 0:2 means the first 2 characters of the argument. But this logic does not seem to work on {3:3:2} and {3:6}. What is the logic behind?

And last but not least, the if statements and the counter.
if [[ ! -e "xx_EV_${lastname}.pdf" ]]
asks if the correct named file already exists, i.e. this is to circumvent that an already processed file is overwritten, correct? Why do you use -e instead of -f? What is the advantage?
And the counter. Why is it set to "1" but never tested again? I always thought you would use a counter in order to test whether it is less than or greater than and make it part of the if condition.

Sorry for asking so many questions. But I would like to understand how the script works so that for next purpose I know better where to start.

Thank you SO much again!

tempestas
Never apologize for asking questions! We want to help you learn. I'm glad it is working for you. Here are my responses with a little more detail than CarloM provided. I hope it helps.

Yes, $0 is the pathname used to invoke your script. So, it could be rename.sh, ./rename.sh, ../scripts/rename.sh, or lots of other strings. IAm=${0##*/} removes the longest string from the start of the expansion of $0 that matches the pattern */. So, if your script was invoked using any of the above pathnames, IAm will be set to rename.sh. You will note that $IAm is used as the 1st string printed as part of any diagnostic messages. It is a convention used to make it easy to figure out which script generated an error message.

When a script completes successfully, it should almost always exit with a zero exit value. When a script completes unsuccessfully, it should exit with a non-zero exit value. Using different non-zero values sometimes help find the line where a problem was detected. (After a shell script completes, its exit status can be seen by looking at the output from the command echo $?.)

lastname="${2%%_*}" is very similar to IAm=${0##*/}, but sets lastname to the expansion of the 2nd command line argument with longest string at the end matching the pattern _* removed. So, if $2 is last_first or last_first_middle, $lastname will be set to last.

Assume the 3rd command line argument is 20.02.2013. The ${3:start:length} expands to the substring of $3 whose first character is at position start (numbered from 0) and contains up to length characters. If :length is omitted, the remainder of the string starting at position start is used. So, in this example, ${3:6} expands to 2013, ${3:3:2} expands to 02, and ${3:0:2} expands to 20.

Yes, test -e filename evaluates to true if a file of any type is currently named filename. test -f filename evaluates to true if a regular file is currently named filename. I didn't want the renaming process to overwrite any existing file so I used -e instead of -f.

Note that I set ec (an abbreviation for exit code) to 0 before starting the for loop. After the loop terminates, the exit returned by this script is $ec. If any moves were skipped because they would have overwritten an existing file, ec is set to 1. In this case the exit code will be 0 if all files were moved successfully and will return 1 if one or more files were not moved. (Obviously, you should also check the exit status of the move commands and set ec to 1 if any move failed; but I'll leave that as an exercise for the reader.) The script could have exited at the time it decided not to move a file, but with something like this I like it to do as much as it can, print a diagnostic for each failure, and return a non-zero exit status if any problems were found.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with find command "find: cannot open"

Hello Guys , I am trying to run below find command in one of my SH script on a HP UX machine. find /tmp -type f -name "MGCA*.log" -prune -exec rm -f {} \; 2>&1 I want this to check my tmp directory and delete MGCA log files .But below error message is printed on Promt :- find: cannot... (2 Replies)
Discussion started by: himanshu sood
2 Replies

2. Shell Programming and Scripting

find problem

Hi I want to find files with size +10000M and specified directorys except when i use more than one `-name` acton is fail I try -a (and) and -o (-or) but results fail :( find . \( -name ./oracle* -prune -o -name "*arc*" -prune -o -name "*oracle*" -prune -o -size +10000k \) -exec ls -lh {}... (7 Replies)
Discussion started by: ygemici
7 Replies

3. Shell Programming and Scripting

find problem

for index in `find /root -name *.jar | grep "/lib"`; do echo "$index"; done sometimes its throwing error saying find: paths must precede expression Usage: find some1 suggested me that : Code: for index in `find /technologies -name '*.jar' | grep "/lib"` do echo "$index"... (2 Replies)
Discussion started by: crackthehit007
2 Replies

4. Shell Programming and Scripting

Problem with find command.

I'm trying to display the full file name (including the full path) and file size of all files whose name (excluding the path) is longer than 10 characters. I came up with find path -type f -name ".{10, }" -printf "%s %p\n", but I'm getting a "find: path: No such file or directory". What's wrong... (2 Replies)
Discussion started by: raidkridley
2 Replies

5. Shell Programming and Scripting

Find command problem

Hi All, I am using following find command to delete the records older than 7 days but getting missing conjuction error.Kindly suggest: The command is: find <complete_dir_path> \(! -name usr -prune \) -type f -name "*.txt" -mtime +6 -print | xargs rm (11 Replies)
Discussion started by: visingha
11 Replies

6. UNIX for Dummies Questions & Answers

problem with find and mtime

I am using HP-UNIX , The below command doesnt display anything although i have changed a file in the directory by toutch -t 200010101800 nfile find /tmp/transfer/ -name "*.*" -mtime +1 Any problrm with the find command i written . .Please help ??.. Thanks, Arun (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

7. UNIX for Dummies Questions & Answers

Problem with find and tar

When I am doing the first command the result shows all the files, links, directories except the ones that contain the word logs find . -type f -o -type l -o -type d | grep -v logs But when I am trying to do this even the logs are getting tarred tar -cvf fdtvision.tar `find . -type f -o -type l... (2 Replies)
Discussion started by: venu_nbk
2 Replies

8. Shell Programming and Scripting

problem with find

Hi, Iam having a strange problem, wandering if soneone can throw some lights. I have statement find . -maxdepth 1 -name 'File1*.tsv' -mtime +1 -print I expect the above statement to print the files older than 1 day or 24 hrs, however it doesn't work that way. When issue above command,... (2 Replies)
Discussion started by: braindrain
2 Replies

9. Shell Programming and Scripting

Problem with `find ...`

Hi all, Have the following snippet of code that I'm having trouble trying to work ... The snippet of code is running on our Production Server and the intent is to copy the second most recent IDE file across from the Development Server. I have the following files defined in $DEVLOC ...... (4 Replies)
Discussion started by: Cameron
4 Replies

10. Shell Programming and Scripting

Another Find Problem

HI All, I am having a bit of trouble using the find command in my shell (korn) script. I was hoping someone could help me. I am trying to build up a dynamic find command based on some parameters. When I execute the command I get the following error: find: incomplete statement I have... (6 Replies)
Discussion started by: sethkor
6 Replies
Login or Register to Ask a Question