directories are not getting removed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting directories are not getting removed
# 1  
Old 11-15-2005
directories are not getting removed

hello Everyone.
I'm having the following problem:
I have number of installation in the directory. each installation consists of executable file and directory. when I do the new installation I move old one to File_name-Time_stamp. this is done for executable and for directory. Everything is done by shell script and works just fine. Now I want to get rid of old installations and keep only new one and last back up (which is created just before ) this script is working fine except one thing - it does not remove the directory. if I do the same manually - it works just fine, however inside the script it doesn't. Does anyone can help me and explain to me what's wrong in my script?

Thanks a lot in advance!
below is my script:

#!/bin/ksh
#cleanup.ksh
typeset SYS_NAME=$1
typeset TimeStamp="not defined"
typeset toRemove="not defined"
umask 002


function TimeStamp {
TimeStamp=`(set \`/bin/date +%d-%b-%y@%H:%M:%S\`; /bin/echo $1)`
print "TimeStamp: $TimeStamp"
return 0;
}

function cleanup {
for Item in `/bin/ls $SYS_NAME*`
do
toRemove=$Item
print "processing: $toRemove"
if [[ $toRemove != $SYS_NAMEc && $toRemove != $SYS_NAMEc-$TimeStamp && $toRemove != $SYS_NAMEc.1 && $toRemove != $SYS_NAMEc.1-$TimeStamp && $toRemove != "cleanup.ksh" ]]
then
rm -Rf $toRemove
print "$toRemove removed"
else
print "I'm not moving: $toRemove"
fi
done
return 0;
}
function backup {
if [[ -s ${SYS_NAME}c ]]
then
/bin/mv ${SYS_NAME}c ${SYS_NAME}c-${TimeStamp}
print "${SYS_NAME}c-${TimeStamp} has been created"
else
print "Could not create a backup for file ${SYS_NAME}c"
exit
fi
if [[ -d ${SYS_NAME}c.1 ]]
then
/bin/mv ${SYS_NAME}c.1 ${SYS_NAME}c.1-${TimeStamp}
print "${SYS_NAME}c.1-${TimeStamp} directory has been created"
else
print "Could not create a backup for directory ${SYS_NAME}c.1"
exit

fi
return 0;
}
TimeStamp
backup
cleanup
# 2  
Old 11-16-2005
You didn't brace the if properly:

if [[ $toRemove != $SYS_NAMEc && $toRemove != $SYS_NAMEc-$TimeStamp && $toRemove != $SYS_NAMEc.1 && $toRemove != $SYS_NAMEc.1-$TimeStamp && $toRemove != "cleanup.ksh" ]]

$SYS_NAMEc should be ${SYS_NAME}c

...etc...
-mschwage
# 3  
Old 11-16-2005
Thank you, -mschwage.
I have another version of this script where i did what you suggested to do awhile ago and it did not resolve my issue. What you are saying about braces will treat variable's value a little bit differently. My script is able to recognize the directories names properly, but they are not getting removed.
The problem is that files are removed by script but directories are not. I would like to understand why.

Thanks,
# 4  
Old 11-16-2005
Could be a permissions problem. Remove the -f from the rm -R and see what happens.

If you have a file owned by someone else in your directory, as long as you own the directory, you can remove it with -f. But if you don't use -f, it will complain. My guess is that you don't own one or more files in the directory.

Anyway, the -f is hiding your error from you so it's difficult to know at this point...
-mschwage
# 5  
Old 11-16-2005
Thank you...
I did what your said even though was in doubt this will fix the problem. I'm owning files and directories. I'm currently developing this script, so everything is done in my home directory. I should not have any permissions related problem for the reason that backup is created by the script, another words by the same owner...
# 6  
Old 11-16-2005
Hmmm... now this is going to taqke serious debugging. You need to run the script with the -x option. Change #!/bin/ksh to #!/bin/ksh -x

Private message me the output.
-mschwage
# 7  
Old 11-17-2005
would you please be so kind and send an -email to me, so I could reply to you with output?
I'm thinking now that problem is in this filename_TimeStamp, which cannot be read by OS (or script, or shell) correctly.
what "+" means in debugger?

Many thank! Appreciate your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Giving read write permission to user for specific directories and sub directories.

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. This is for Solaris. Please help. (1 Reply)
Discussion started by: blinkingdan
1 Replies

2. UNIX for Dummies Questions & Answers

List the directories, having given pattern in the directories name, sorted by creation date

It is for HP-Unix B.11.31. Requirement: 1. List the directories, having given pattern in the directories name, sorted by creation date. Example: Directories with name "pkg32*" or "pkg33*" 2. On the output of 1. list the directories by creation date as sort order, with creation date... (2 Replies)
Discussion started by: Siva SQL
2 Replies

3. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

4. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

5. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

6. Shell Programming and Scripting

How to list all the directories, sub directories in a mount along with size in ascending order?

Hi , I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In a particular mount, have to list all the directories and sub directories along with size of the directory and sub directory in ascending order. Please help me in this regard and many... (4 Replies)
Discussion started by: nmakkena
4 Replies

7. Shell Programming and Scripting

Script for parsing directories one level and finding directories older than n days

Hello all, Here's the deal...I have one directory with many subdirs and files. What I want to find out is who is keeping old files and directories...say files and dirs that they didn't use since a number of n days, only one level under the initial dir. Output to a file. A script for... (5 Replies)
Discussion started by: ejianu
5 Replies

8. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

9. UNIX for Dummies Questions & Answers

a way to tell what was removed after rm -rf ?

Hello all! I ran rm -rf on a wrong directory, noticed it and hit ctrl-c. Is there any way on a debian machine to tell what actually got deleted? As there were many dirs and files in this directory that I don't care for, I'd like to see if anything important was removed. Or do you know in... (4 Replies)
Discussion started by: thosch
4 Replies
Login or Register to Ask a Question