Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 07:36 PM
08-10-2006 07:36 PM
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 07:50 PM
08-10-2006 07:50 PM
Re: rm -fr *
what are you trying to achieve with your script? You can alias rm to rm -i to build a double protection against accidential delete.
I assume your script is going to replace the normal rm command and give a warning if the "*" is used ?
Please give more background.
(Congratulations on having a good backup !)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 09:01 PM
08-10-2006 09:01 PM
Re: rm -fr *
I would be happy to clarify any more queries.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 09:09 PM
08-10-2006 09:09 PM
Re: rm -fr *
a file * can be removed via
rm "*" or rm \* (no spaces)
$ touch *
$ ll
total 0
-rw-rw-rw- 1 gp users 0 Aug 11 10:14 *
$ rm "*"
$ ll
total 0
$ touch *
$ ll
total 0
-rw-rw-rw- 1 gp users 0 Aug 11 10:15 *
$ rm \*
$ ll
total 0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 09:12 PM
08-10-2006 09:12 PM
Re: rm -fr *
As it is the shell which processes carateres *, you would have to modify the shell itself !
You cannot replace the command rm by a script asked to handle carateres * because it will receive the list of the files of the current directory : shell behavior.
I think there is no simple answer to your question unless you rewrite C-Shell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 09:13 PM
08-10-2006 09:13 PM
Re: rm -fr *
(not tested fully. So double check before you use it)
#/usr/bin/ksh
first=$(echo ${1}|tr -cd '\*'|cut -c1)
if [ ${first} = "\*" ];then
echo "you have used "\*" in rm command!!1 Will exit now.\n"
exit 1
done
We also need to add more error checking to see if any of the args has "*" and not just first arg.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 10:30 PM
08-10-2006 10:30 PM
Re: rm -fr *
Peter,
The idea of using a \* or a "*" did occur to me. But it kinda defeats the purpose of checking for a wildcard since if I am careful enough to write a \* then I will be careful enough not to delete my home dir.
Eric,
Your explanation is very plausible. But as gurus of UNIX say "there is always another way of doing it". There must be some other way apart from editing the shell.
Rac,
I tried the code given. There were some errors. (I use csh by force of requirement but for this I did switch to ksh for once and i have no idea of ksh syntax!!). A blind copy-paste-run of the script gives syntax error: `done' unexpected.
also after I comment out done it gives me a sdyntax error: `then' unmatched.
can you give the same for csh??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 10:38 PM
08-10-2006 10:38 PM
Re: rm -fr *
#/usr/bin/ksh
first=$(echo ${1}|tr -cd '\*'|cut -c1)
if [ ${first} = "\*" ];then
echo "you have used "\*" in rm command!!1 Will exit now.\n"
exit 1
fi
Again, this code checks if first argument to rm has "*" in it or not. Ideally, what you need is code that checks all arguments to rm and if any of those arguments has "*" or not.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 11:23 PM
08-10-2006 11:23 PM
Re: rm -fr *
first=$(echo ${1}|tr -cd '\*'|cut -c1)
if [ ${first} = "\*" ];then
echo "you have used "\*" in rm command!!1 Will exit now.\n"
exit 1
fi
This code gives another type of error. it says test: arguement expected. even when i type "rem *" rem being the script name. even a valid filename insted of * gives the same error. :(
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 11:45 PM
08-10-2006 11:45 PM
Re: rm -fr *
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 11:53 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2006 11:53 PM
08-10-2006 11:53 PM
Re: rm -fr *
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2006 12:11 AM
08-11-2006 12:11 AM
Re: rm -fr *
Yes, James, you are right with posix shell but preetam wants to work with C-shell. I don't practice c-shell but i did not hear of same behavior that set -/+f with posix shell
Anyway, i could never work with shell without character expansion. Ok, you can do some big mistake but it's life ;-) Anyway Peter's suggestion to make an alias to rm -i is a good protection.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2006 12:22 AM
08-11-2006 12:22 AM
Re: rm -fr *
In the 'csh' you can use 'noglob' to disable file name expansion.
Users of 'csh' might want to read this:
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
Barring any shell preferences, on HP-UX you cannot change root's default shell from other than '/sbin/sh' since to do so leads to an unbootable system. That said, one would do well to understand the ramifications of file name expansion in the Posix shell, and be extra careful typing 'rm -f' under all conditions!!!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2006 12:23 AM
08-11-2006 12:23 AM
Re: rm -fr *
trying to work it into a script...
eric, i have to work in csh coz my repository has all the required scripts in csh else i would ve preffered a different shell...
Please do keep me posted abt any brainstorms regarding this on csh...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2006 12:28 AM
08-11-2006 12:28 AM
Re: rm -fr *
wildcard=1
for file in $(echo *); do
if [ $(echo " $* " | grep -c " $file ") -ne 1 ]; then
wildcard=0;
break;
fi;
done
echo "used wildcard? $wildcard"
Heavy-handed, but it should work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2006 12:33 AM
08-11-2006 12:33 AM
Re: rm -fr *
"Globbing" is the real term for filename expansion using wildcards. Now, you should be able to remember that in 'csh' you set 'noglob' to disable it :-))
See the manpages:
http://docs.hp.com/en/B2355-60127/csh.1.html
By the way, while aliasing 'rm' to 'rm -i' is an extra layer of protection in an interactive shell, anytime you type 'rm -f' you override the '-i' switch.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2006 12:43 AM
08-11-2006 12:43 AM
Re: rm -fr *
You may also want to think about implementing a staging area for files pending deletion (a la "Trash" or "Recycle Bin"). Move the 'rm' binary somewhere safe and replace it with a script that moves files to a secure staging area. Make sure the script accounts for large files (perhaps by deleting them outright instead of moving them). Then cron a clean up job to purge the staging area.
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2006 02:32 AM
08-11-2006 02:32 AM
Re: rm -fr *
the following will work in TCSH but not in csh,
because csh won't deal with then parameters 'argn' supplied to
source filename arg1 arg2 ...
cat ~/bin/mec
#!/usr/bin/tcsh # just for syntax highlighting
if ("$argv[1]" == '*') then
echo I see a star
else
echo $*
endif
Set this alias:
alias myecho ~/bin/mec
Now entering
myecho *
will lead to the above message, anything else will return the shell-expanded arguments.
Adopting for a rm-lile solution is trivial.
NB: TCSH only!
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2006 02:33 PM
08-11-2006 02:33 PM
Re: rm -fr *
rm -fr /var /tmp/myfiles*
In this case, the accidental space just started rm removing the entire /var directory (a disaster since all installed patches and HP product histories are now gone).
So a wrapper won't work. Instead, alias the dangerous commands rm mv and cp to:
alias rm="/usr/bin/rm -i"
alias mv="/usr/bin/mv -i"
alias cp="/usr/bin/cp -i"
-i forces these commands to ask permission for each file or directory that will be destroyed.
Now unfortunately this only helps when you don't use the -f option which overrides -i. Here is where a wrapper script would help. Use getopts to process all the possible option combinations and disallow -f. You can rename the real binaries like rm to something like rm.real, etc. then have your wrapper script implement -i for everything. For knowledgeable users, they can use the real binaries. Note that all users must have the above aliases so they don't use any shell builtin commands.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2006 08:36 AM
08-13-2006 08:36 AM
Re: rm -fr *
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2006 08:31 PM
08-28-2006 08:31 PM
Re: rm -fr *
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2006 09:02 PM
08-29-2006 09:02 PM
Re: rm -fr *
that you got a lot of fantastic replies!
If I could see these fantastic replies awarded with points, it would be a motivation for the responders, to do that for further questions as well.
If a problem is not solvable in a way you want it to be solved, it's not a reason to give few points (or assign nothing), IMHO.
mfG Peter