LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-12-2003, 12:23 PM   #1
szf2
LQ Newbie
 
Registered: Aug 2003
Distribution: RedHat 9.0
Posts: 17

Rep: Reputation: 0
My first BASH script, what's wrong with it?


After deleting some files I didn't want to , I decided to protect myself by aliasing rm to a "rmv" function I wrote and placed in my local bin. Basically, I want rmv to copy whatever folder or file is being deleted to my trash folder, then remove the folder or file. Further, I would like rmv to accept the following flags:

-f = do not copy folders/files to trash, just delete
-i = interactive mode
-v = verbose


Code for the function is as follows:

Code:
#!/bin/bash
for a in "$@"
do
if[-f "$a"] ; then
/bin/rm -rf "$@"
elif[-i "$a"] ; then
cp -ri $a /path/to/trash/folder
/bin/rm -ri $a
elif[-v "$a"] ; then
cp -rv $a /path/to/trash/folder
/bin/rm -rfv $a
elif[-* "$a"] ; then
echo "Unexpected flag in call to rmv.\n"
exit -1
else 
cp -r $a /path/to/trash/folder
/bin/rm -rf $a
fi
done
I know its far from perfect, but hopefully enough for a BASH expert to get the idea and tell what all I've done wrong. Also, I'm not worried about overwriting files of the same name. The trash folder is basically there just to catch my typos when using rm.

Thanks
Steve
 
Old 11-12-2003, 01:05 PM   #2
evil_Tak
Member
 
Registered: Oct 2003
Distribution: Debian/unstable
Posts: 85

Rep: Reputation: 15
Re: My first BASH script, what's wrong with it?

#!/bin/bash
for a in "$@"
^^ this loops through ALL the arguments passed to the script; do you really want to do that?

do
if[-f "$a"] ; then
^^ you're not testing if $a equals "-f", you're testing whether $a meets the builtin bash test -f

/bin/rm -rf "$@"
elif[-i "$a"] ; then
cp -ri $a /path/to/trash/folder
/bin/rm -ri $a
^^ if you're copying everything to a trash folder, why prompt the user for every file?


How about this?
Code:
#!/bin/bash
SW="$1"
shift

cp "${SW}"r "$@" /path/to/trash/folder && rm "${SW}"r "$@"
Even better, you could do one like this:
Code:
#!/bin/bash
mv "$@" /path/to/trash/folder
 
Old 11-12-2003, 01:43 PM   #3
szf2
LQ Newbie
 
Registered: Aug 2003
Distribution: RedHat 9.0
Posts: 17

Original Poster
Rep: Reputation: 0
Rock on. . I especially like the last one liner. Very nice.

However, I would like it so if I pass the "rmv" function a -f flag it will just remove the files (no copying to trash). This is especially true since some of the scripts I run generate a fair amount of temp files and I would like to be able to "rm -f" them w/o filling up the trash. Just wondering how a person tests for an input flag (since obviously my first method was incorrect). I am taking a shot in the dark with the following:

Code:
#!/bin/bash
if( "$1" -f) ; then
shift
rm -rf "$@"
else
mv "$@" /path/to/trash/folder
fi
Actually, I know this does not work because I've tried running it. I just need to know what the SOP is for testing for input flags in a bash script (and of course any other flaws in the script)

Thanks,
Steve

Last edited by szf2; 11-12-2003 at 01:48 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Programming - Where am I going wrong? [NL]_Target Programming 9 07-21-2005 08:49 PM
what am I doing wrong in the bash shell? alexes Linux - Newbie 4 05-18-2004 01:33 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
Basic BASH script, what's wrong??? Satriani Linux - General 2 06-02-2003 05:34 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration