Sponsored Content
Top Forums Shell Programming and Scripting String variable as part of expression in find command Post 302946062 by Don Cragun on Saturday 6th of June 2015 09:29:11 AM
Old 06-06-2015
Executing commands containing quoted strings produced by command substitution can be tricky. You can easily see the problem if you use set commands to turn tracing on and off around the command substitution in your code:
Code:
set -xv
Find_File2Exclude=`find ${paths[$k]} -maxdepth 1 -type f \( $exclude \) -printf '%t %p\n' | sort -k 1 -n | head -n 1 | cut -d'/' -f3`
set +xv

which clearly shows that you end up with double-quoted single-quoted strings for your patterns containing wildcard such as "'*.log'" which causes find to look for filenames containing literal single-quote characters.

If all of the following are true:
  1. All of the elements of the array ${paths[]} are absolute pathnames.
  2. None of the elements of the array ${paths[]} contain any whitespace characters.
  3. You don't mind the script creating an empty directory in your home directory (which the script will use to run the concocted find command pipeline with no chance that unquoted filename patterns containing wildcards with expand to actual filenames).
then the following replacement for your non-working code snippet may work:
Code:
[ -d "$HOME/_empty_dir" ] || mkdir "$HOME/_empty_dir" || exit 1
cd "$HOME/_empty_dir"
exclude="out log cps suc dat"
cmd="find ${paths[$k]} -maxdepth 1 -type f $(printf '! -iname *.%s ' $exclude) -printf"
Find_File2Exclude=$($cmd '%t %p\n' | sort -k1,1n | head -n 1 | cut -d'/' -f3)
cd - > /dev/null
rmdir "$HOME/_empty_dir"
printf '%s\n' "$Find_File2Exclude"

Note that I could not test this exact code on my system because the find utility on my system does not recognize the -maxdepth, -iname, and -printf primaries. But, similar constructs have been successfully tested with both bash and ksh.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Repacing part of string with a variable

I have following strings in a file DUPTASMTRMMBAL,20070416200704160117232101172321,,,,,,,@@@Y DUPTASMTRMMCON,20070416200704160127189901271899,,,,,,,@@@Y DUPTASMTRMMHG,,20070416200704160112051001120510,,,,,,,@@@Y What i need to do is replace the date 20070416 with anoth date which is stored in... (4 Replies)
Discussion started by: divz
4 Replies

2. UNIX for Dummies Questions & Answers

using regular expression for directories in find command

Hi, I want to find the files available in a directory /var/user/*/*/data/. I tried using the command "find /var/user/ -path '*/*/data/ -name '*' -type f" it says find: 0652-017 -path is not a valid option and then i tried using "find /var/user/ -name '*/*/data/*' -type f" but its not... (3 Replies)
Discussion started by: vinothbabu12
3 Replies

3. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

4. Shell Programming and Scripting

How read the part of the string into a variable?

Hi, I'm using bash and brand new to shell script. I would like to do the following. I have a string which is "UPDATE=1.0". I would like to read the value "1.0" alone in a variable. i.e the things afer "=" How do I do that? Thanks, (1 Reply)
Discussion started by: scriptfriend
1 Replies

5. Shell Programming and Scripting

find the line starting with a pattern and save a part in variable

Hi i have a file which has mutiple line in it. inside that i have a pattern similar to this /abc/def/hij i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable. thanks in advance (13 Replies)
Discussion started by: kichu
13 Replies

6. Shell Programming and Scripting

Find, regular expression, anyway to simplify this find command?

Hello everyone, first post here, trying to learn scripting on my own and this forum as been really helpful so far. I made few little scripts working great but I m facing some problems with RE. I have a bunch of files in many subdirectories called *001.ext *002.ext OR simple *.ext or *01.ext... (7 Replies)
Discussion started by: Sekullos
7 Replies

7. Shell Programming and Scripting

Regular Expression in Find command [KSH]

Hello, I am trying to use regex wtih find command in KSH. For some reason it is not working as expected. Input: comm_000_abc_0102.c comm_000_abc.c 456_000_abc_1212.cpp 456_000_abc_.cpp Expected Output: comm_000_abc_0102.c kkm_000_abc_8888.cpp (Basically I want to find all... (6 Replies)
Discussion started by: vinay4889
6 Replies

8. UNIX for Dummies Questions & Answers

find command: names matching the expression

Hello all, I need to print directories using find command. The directories names contain date in the format YYYYMMDD or the name of directory is only the date format. I want print directories, which doesn't start with this date. E.g I have dirs like foo20120101 foo20120101foo 20120101foo... (1 Reply)
Discussion started by: satin1321
1 Replies

9. Shell Programming and Scripting

Command substitution inside of a variable expression (AIX, KORN)

Hello all. This is my first post/question on this site. I’m a new Systems Analyst with previous experience with BASH. Although now I'm using AIX, and I’m trying to get a feel for the Korn shell (for those of you that don’t know AIX only uses the KORN shell). I hope I put this into the correct... (10 Replies)
Discussion started by: sydox
10 Replies

10. Shell Programming and Scripting

Grep command to find string in Variable in file2

Hi , I am executing 2 queries and output is saved in file1.txt and file2.txt example of file1.txt Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,, Testing Excel _.xlsx,/Lab/Development and... (3 Replies)
Discussion started by: Sunil Mathapati
3 Replies
All times are GMT -4. The time now is 02:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy