LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash file...weird problem (https://www.linuxquestions.org/questions/linux-newbie-8/bash-file-weird-problem-759760/)

vexx 10-05-2009 03:05 AM

Bash file...weird problem
 
I've created a sh file to delete a cache folder for one of my sites. Basically, the bash looks like this:

Code:

#!/bin/bash
rm -fdr cached*
mkdir cached
chmod 0777 cached

When I launch it within the console (root), it does not delete the cached folder and when I "ls -l", I always find another folder called "cached\r".

The cached folder has 0777 permissions all the time, owner root, group root.

The funny part is, I have another site, on the same server, different account and this bash works perfectly.

If I run the commands individually from the console, works perfectly...

I'm puzzled ...

Tnx in advance

wfh 10-05-2009 03:09 AM

Try using full pathnames for your cached folder, such as:

/home/me/cached

...cause when you execute the shell, it does not use your pwd.

konsolebox 10-05-2009 03:13 AM

If you try to echo the arguments, what do you think would it have expanded to?
Code:

echo rm -fdr cached*

vexx 10-05-2009 03:23 AM

Quote:

Originally Posted by konsolebox (Post 3708072)
If you try to echo the arguments, what do you think would it have expanded to?
Code:

echo rm -fdr cached*

if I echo all the commands, it prints them to the screen, same order as in the sh file.

As for the full path, it's the same thing, tried it now, same result. The sh file is in the root folder, where the dir cached resides.

I've double checked permissions now, all seems ok, I don't rly get it, especially when the same command runs without problems under other account.

wfh 10-05-2009 03:25 AM

Add a "pwd" to your script and pipe the output to a file.

pwd > foo.txt


...see where you're operating at the time you rm the directory.

konsolebox 10-05-2009 03:30 AM

How 'bout if you remove the '-f' option. It might print the errors.

wfh 10-05-2009 03:43 AM

"The funny part is, I have another site, on the same server, different account and this bash works perfectly."

What do you mean by "another site"? Another path? Another account? You say it works with the other account, but what is fundamentally different in your account and the other?

catkin 10-05-2009 03:44 AM

Try adding a comment after cached
Code:

mkdir cached # this is a comment

wfh 10-05-2009 03:47 AM

Make your 'rm' command do the following:

rm -rdf cached* 2>&1

...to see if errors are being thrown.

slakmagik 10-05-2009 03:51 AM

I don't know how, but I bet you have a DOS line ending on that line and possibly others.

Does 'cat -A file' show

Code:

mkdir cached^M$
(actually, any decent editor should show it if it's there)

catkin 10-05-2009 05:24 AM

Quote:

Originally Posted by wfh (Post 3708106)
Make your 'rm' command do the following:

rm -rdf cached* 2>&1

...to see if errors are being thrown.

Why do you think any errors will not be shown already? Stderr is directed to terminal by default so the suggested technique is only necessary if it has been redirected somewhere else earlier.

catkin 10-05-2009 05:25 AM

Quote:

Originally Posted by slakmagik (Post 3708109)
I don't know how, but I bet you have a DOS line ending on that line and possibly others.

Does 'cat -A file' show

Code:

mkdir cached^M$
(actually, any decent editor should show it if it's there)

That was my guess, hence suggesting adding the comment so any DOS line end is no longer part of the executed command.

vexx 10-05-2009 06:18 AM

Quote:

Originally Posted by catkin (Post 3708171)
That was my guess, hence suggesting adding the comment so any DOS line end is no longer part of the executed command.

guess what..u're both right...i feel like an idiot..i've edited that file with windows notepad..a while ago and i forgot...left some spaces after the command...ruined it :)

tnx alot guys for the help

SharpyWarpy 10-05-2009 06:22 AM

Don't know for sure if the "-f" switch negates this, but is your "rm" command aliased to "rm -i"? Try running "rm" like this:
/rm
to see.

chrism01 10-05-2009 11:17 PM

I think you mean

\rm


All times are GMT -5. The time now is 05:54 AM.