Controller is not going into IF test


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Controller is not going into IF test
# 1  
Old 10-05-2015
Controller is not going into IF test

Hi All,

I am searching for some files (*.sem and *.chk and *.temp) in morethan one directories.
if i found .sem and .chk files(base name should be same) i am deleting these 2 files because i need to delete .chk files if i found respective .sem file
i am also deleting other .sem files only even if i dont find respective .chk file as i should remove all .sem files
i am also check for .temp files and deleting them all of as well. Here is the example:

I have following files in different directories (this is the output of find command)

Code:
0.tar
1.temp
2.sem
2.temp
3.sem
5.chk
3.chk
4.txt

in the above case
  1. i should straight a way delete 3.sem and 3.chk as base name is same and .sem and .chk files present
  2. 1.temp and 2.temp can be deleted as i am deleting all the .temp files.
  3. 2.sem can also be deleted as i am deleting all other sem files

So Now, the output should be
Code:
0.tar
5.chk
4.txt

for that i am using below script, but the controller is not going inside the if loop, am i doing any mistake here? can somebody help me?

Code:
find ./*/in -type f \( -name "*.sem" -o -name "*.chk" -o -name "*.temp" \) -printf "%f\n" | while read file; 
do 
filename=`rev <<< "$file" | cut -d"." -f2- | rev`; 
echo "1"; 
if [[ -f $filename".sem" && -f $filename".chk" ]]; then 
echo "$semfile--$chkfile"; 
fi; 
echo "2"; 
if [[ -f $filename".temp" ]]; then 
echo "$tempfile"; 
fi; 
echo "3"; 
done;

in the above code i just mentioned echo instead of rm for testing purpose.
Thanks for your help,

Vasu
Moderator's Comments:
Mod Comment Please use CODE tags for sample input and sample output as well as for sample code segments.

Last edited by Don Cragun; 10-05-2015 at 08:21 PM.. Reason: Add CODE tags.
# 2  
Old 10-05-2015
There is some relatively slow and inefficient code in your script assuming you are using a shell with standard variable expansion capabilities. What operating system and shell are you using?

With a POSIX conforming shell, you might want to try something like:
Code:
find ./*/in -type f \( -name '*.sem' -o -name '*.temp' \) |
while read -r file
do	base="${file%.*}"
	[ -f "$base.chk" ] || [ -f "$base.temp" ] &&
	    echo rm -f "$base.chk" "$base.sem" "$base.temp"
done

and take out the echo if it looks like that is doing what you want.

Last edited by Don Cragun; 10-05-2015 at 09:49 PM.. Reason: Add sample code.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-06-2015
Paraphrasing your spec in post#1: delete all .temp and all .sem files unconditionally, and delete .chk files if the respective .sem exists.
Don Cragun's proposal above gets close but would delete .chk if a .temp exists. Try in lieu:
Code:
find . -type f \( -name '*.sem' -o -name '*.temp' \) |
while read -r file
        do echo rm -f "$file"
        CHK=${file/.sem/.chk}
        [ -f "$CHK" ] && echo rm -f "$CHK"
        done

---------- Post updated at 14:20 ---------- Previous update was at 14:17 ----------

or maybe
Code:
find . -type f \( -name '*.sem' -o -name '*.temp' \) |
while read -r file
        do CHK=${file/.sem/.chk}
           [ ! -f "$CHK" ] && unset CHK
           echo rm -f "$file" "$CHK"
        done

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 10-06-2015
Thank you So much for helping me Dan and Rudi!!!
Kindly explain what will happen with the following statements:

CHK=${file/.sem/.chk}
unset CHK
# 5  
Old 10-06-2015
Look for "parameter expansion: Pattern substitution" and "unset" in man bash.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-29-2015
Hello Rudi,

I am sorry for troubling again, there is some confusion in requirement. here is the actual requirement and what i have implemented.

1. Unconditionally delete .sem and .temp files
2. If we found the actual file, don't remove .chk file, otherwise remove .chk file as well

for example i have:

Code:
file1.20150912.001
file1.20150912.001.chk
file2.20150913.002.sem
file2.20150913.002.temp
file3.20150914.003.chk
file4.20150914.004
file4.20150914.004.chk
file4.20150914.004.sem
file4.20150914.004.temp
file5.20150915.005.sem
file6.20150916.006.temp
file7.20150917.007

I should straight a way delete following files:

Code:
file2.20150913.002.sem
file2.20150913.002.temp
file3.20150914.003.chk
file4.20150914.004.sem
file4.20150914.004.temp
file5.20150915.005.sem
file6.20150916.006.temp

Should NOT delete following files.
Code:
file1.20150912.001
file1.20150912.001.chk
file4.20150914.004
file4.20150914.004.chk
file7.20150917.007

for this i have write the script as following:

Code:
#!/bin/bash
cd /apps/NAS/it/phome
find ./*/in -type f \( -name '*.sem' -o -name '*.temp' \) |
while read -r dfile
do
        echo rm -f "$dfile"
        CHK=${dfile/.sem/.chk}
        FILE=${dfile%.*}
        [ -f "$CHK" ] && [ ! -f "$FILE" ] && echo rm -f "$CHK"
done

But no luck, its not deleting .chk files. Could you please help me to fix if there is any issue in my above code?
Thanks in advance, appreciate your patience.
Vasu
# 7  
Old 10-29-2015
Moderator's Comments:
Mod Comment The above post was double posted into a new thread: Conditional deletion of files based on extension

Continue any discussion on this new topic there.

This thread is closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Red Hat

Separate HDD test using RAID controller

Hello, I am using Red Hat 2.6.23.1 with RAID 6 controller (card). (When I use "cat /proc/partitions" I do not see the separate HDDs in the RAID, I see one drive for the RAID itself, as it is considered 1 large drive.) I used a "dd" check to test my RAID drive speed and found it was a bit slow,... (7 Replies)
Discussion started by: Dbugy
7 Replies

2. Shell Programming and Scripting

Prefixing test case methods with letter 'test'

Hi, I have a Python unit test cases source code file which contains more than a hundred test case methods. In that, some of the test case methods already have prefix 'test' where as some of them do not have. Now, I need to add the string 'test' (case-sensitive) as a prefix to those of the... (5 Replies)
Discussion started by: royalibrahim
5 Replies

3. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

4. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

5. Solaris

changing controller name

Dear Solarizer, If i have a disk let say like this c1t0d0s0 How to change the controller to be come example : c3t0d0s0 Thanxs For your replay Regads, tpx (3 Replies)
Discussion started by: tpx99
3 Replies

6. UNIX for Advanced & Expert Users

Controller Naming

Hello all, How does the Solaris identifies the controller subscript ? ( like c0txdxs0 or c1txdxsx ?? ) I have a unix box ( Ultra 30) running with 2.5.1. When I connected an external hard disk to the on-board scsi port, it got identified as c0t1dxsx... (... (1 Reply)
Discussion started by: shibz
1 Replies
Login or Register to Ask a Question