Need File Name comparison help on Korn Shell Script
I am trying to compare two filenames including the paths. Basically if two paths are similar then my program wants to display message. So I need to check for if two pathnames are same in the sorce and destination. So I was trying for the code to compare tow filename sincluding "\" and if the Parent FIle Path is found in the child file path then I need to displays ome message. I was trying to modify something like this:
#!/bin/ksh # Sample script file1="C:\Text\Latest\Article\News.txt" file2="C:\Text\Latest" pattern1=$file2 if [[ $file1 = pattern1* ]] then echo "Do something" fi ======================================== Also tried == and $file2 in place of pattern1. |
You might try the regular expression match operator:
Code:
#!/bin/ksh Code:
#!/bin/ksh |
Well I am not sure why you have copied file2 into pattern1, but ultimately you only made 2 small typos to get your script to work as is:
Code:
#!/bin/ksh |
Need More Clarification
The approaches suggested do help. But I am trying something like:
##Logic #Need to copy from one file-path to other file-path #e.g. from D:\Books\Latest\Novels\Finkler to D:\Books\Latest #The two string approach would give match even if the destination is only D:\. #So introduced third string str (which is constant) to check for match with str2 first #Then compare the fixed path in str3 with str #! /bin/ksh str=$1 str2=$2 str3="D:\Books\Latest" echo $str echo $str2 if [[ $str2 == [$str3]* ]] then if [[ $str == [$str2]* ]] then echo "Go" else echo "NoGo1" fi else if [[ $str == [$str3]* ]] then echo "Go2" else echo "NoGo2" fi fi The above code gives me NoGo2 even for matching string. I am entering arguments as: D:\Books\Latest D:\Books\Latest\Novels\Finkler |
So we should have mentioned before, please use [code][/code] tags to make your code more readable and indented.
As for your code: 1. Make sure arguments are passed to script with single quotes around them to preserve the \ and not allow escaping of next character 2. str3 is same as above and needs single quotes 3. Replace [] around comparisons, ie [$str3]*, with "" as I explained in previous post, ie "$str3"* 4. Without any of the changes above your script fails anyway due to missing semi-colon before each of your 'then' statements Once these changes are made the result I get is: NoGo1, which appears to be correct based on input. |
Quote:
If you want to test if string $file1 starts with $file2 or vice versa, use Code:
if [ "${file1:0:${#file2}}" == "${file2:0:${#file1}}" ]; then Code:
typeset -u temp1 temp2 |
Any alternate approaches
Code:
if [ "${file1:0:${#file2}}" == "${file2:0:${#file1}}" ]; then Code:
typeset -u temp1 temp2 The above approach does not work for me. Am working in Korn Shell on very old version of Unix.What are the other alternatives in case I basically just want to check if one file path contains other file path in it? |
Well, I have access to ksh on SunOS 5.10, and for it, case seems to work well:
Code:
FILE1="/path/to/some/file" You'll want to use that, if you do not want /some/path to match /some/path2/file . Otherwise, just use plain $TEMP . If you are working on a truly ancient Unix, you can always work around the problems by using wc for length, cut for string manipulation, and optionally tr for case conversion: Code:
FILE1="/some/path" Old shells don't always support $(cmd) subshells, so I use the ancient but equivalent `cmd` instead. Note that the lengths are intentionally swapped, so that TEMP1 and TEMP2 end up being the same length. I put the colons into the compared patterns just in case your shell does not compare empty strings properly. |
All times are GMT -5. The time now is 08:01 PM. |