LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-15-2006, 11:35 AM   #1
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Rep: Reputation: 15
Binary operator expected - error


Hi, when I pass multiple argument to this function I get multiple errors - binary operator expected:

Code:
function checkExisting ()
{
if [ -d "$TRASH/$@" ] || [ -f "$TRASH/$@" ]; then
     rm -rf $TRASH/$@
     writePro $@

else
     writePro $@

fi
}
any ideas of a way round it?

ta

Mike
 
Old 07-15-2006, 12:27 PM   #2
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Also, is there a way to remove non-empty directories without using rm? I want to alias my script to rm when its done and if I use rm it will simply call my script.
 
Old 07-15-2006, 12:37 PM   #3
hfawzy
Member
 
Registered: Aug 2002
Location: Egypt
Distribution: Debian Sarge, Slackware 10.0
Posts: 163

Rep: Reputation: 30
To remove non-empty directories, you could still use rm, but by calling /bin/rm instead of directly rm (this will not call your alias).
Concerning your first post, is writePro a function you have written?
If so, please post it.

HTH.
 
Old 07-15-2006, 12:52 PM   #4
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by hfawzy
To remove non-empty directories, you could still use rm, but by calling /bin/rm instead of directly rm (this will not call your alias).
Concerning your first post, is writePro a function you have written?
If so, please post it.

HTH.
Hi here is my full script:

Code:
#!/bin/bash
# Ai program to emulate the "rm" command in UNIX.
# Created by Michael Kelly
# Last revison 12/07/2006


# INITIALIZE VARIABLES
NO_ARGS=0
FLAG_R=""
FLAG_F_I=""
FLAG_V=""
TRASH=$HOME/deleted

# FUNCTIONS

function errors() {
if [ "$#" -eq  "$NO_ARGS" ] ; then
      echo "rm: too few arguments"
      echo "Try \`rm --help' for more information."
      exit 0
elif [[  ! -f "$1"  && ! -d "$1"   ]] ; then
      echo "rm: cannot remove $ARG : no such file or directory"
      exit 0
elif [[ -d $ARG  &&  "$FLAG_R" = ""  ]] ; then
      echo "rm: \`$ARG' is a directory"
      exit 0
else
     writePro $1
fi
}

function verbose () {
if [ "$FLAG_V" = "v" ] ; then
echo "removing \`$1'"
fi
}

function interactive () {
echo -n "rm: remove $1 ?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     remove $1
     else
     exit 0
fi
}

function writePro () {
if ! [ -w  "$1" ] ; then
echo -n "rm: remove write-protected file \`$*'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     delete $1
     fi
else
     delete $1
fi

}
function checkExisting ()
{
if [ -d "$TRASH/$1" ] || [ -f "$TRASH/$1" ]; then


     shred -fp $TRASH/$1 2>/dev/null
     writePro $1

else
     writePro $1

fi
}

function force () {

mv -f $1 $TRASH 2>/dev/null
verbose $1
}


function remove () {
mv  $1 $TRASH 2>/dev/null
verbose $1
}

function delete() {
if [ "$FLAG_F_I" = "-i" ] &&  [ -w  "$@" ] ; then
   interactive $1
elif [ "$FLAG_F_I" = "-f"  ] ; then
   force $1
elif [ "$FLAG_R" = "-R"  ] ; then
   recursive $1
else
   remove $1
fi

}

# Getops

while getopts :rRfvi o
do    case $o in
           r|R) FLAG_R=-R
             ;;
             f) FLAG_F_I=-f
             ;;
             v) FLAG_V=-v
             ;;
             i) FLAG_F_I=-i
             ;;
             *) echo "rm: invalid option -$1"
                echo "try \`rm --help' for more information"
                exit 0
      esac
done
shift `expr $OPTIND - 1`

if ! [ -d "$HOME/deleted" ] ; then
     mkdir $HOME/delete

else
   for ARG in $*
   do
   errors $ARG
   done
fi
if [ $# -eq $NO_ARGS ] ; then
errors
fi
I am trying to emulate the "rm" command and then set this script as an alias to rm and simply move files rather than delete then.

I am struggling with the directories, I can handle files fine.

Thanks for the help!
Mike
 
Old 07-15-2006, 02:01 PM   #5
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi again,

I have amended my function to this:

Code:
function checkExisting ()
{
if [ -d "$TRASH/$1" ] || [ -f "$TRASH/$1" ]; then
     shred -fu $TRASH/$1/* 2>/dev/null
     rmdir $TRASH/$1
     writePro $1
else
     writePro $1

fi
}
but that will only shed for one directory, if there is a directory with a directory and so on it only shreds the contents of the first directory, can I improve it to do this recursively? if so how to I do it?

Ta

Mike

Last edited by mike9287; 07-15-2006 at 02:05 PM.
 
Old 07-15-2006, 04:34 PM   #6
hfawzy
Member
 
Registered: Aug 2002
Location: Egypt
Distribution: Debian Sarge, Slackware 10.0
Posts: 163

Rep: Reputation: 30
Are you using shred to avoid the problem you stated in your second post ?
Quote:
Also, is there a way to remove non-empty directories without using rm? I want to alias my script to rm when its done and if I use rm it will simply call my script.
If so, I said in my earlier post that you could still use rm (and not shred), but by calling /bin/rm instead of rm. By calling /bin/rm instead of rm, the command that will be executed is really /bin/rm, whereas if you call rm after you aliased it to your script, your script will be executed instead of rm.

Anyway, if you really intend to use shred, you can delete all files recursively using this command :
Quote:
find -type f -exec shred -u '{}' \;
and then remove the tree of empty directories by calling /bin/rm -rf *

HTH.
 
Old 07-16-2006, 01:36 PM   #7
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by hfawzy
Are you using shred to avoid the problem you stated in your second post ?

If so, I said in my earlier post that you could still use rm (and not shred), but by calling /bin/rm instead of rm. By calling /bin/rm instead of rm, the command that will be executed is really /bin/rm, whereas if you call rm after you aliased it to your script, your script will be executed instead of rm.

Anyway, if you really intend to use shred, you can delete all files recursively using this command :

and then remove the tree of empty directories by calling /bin/rm -rf *

HTH.

Hi, thanks for the info, much appreciated.

Can I use the second commands to remove directories with rmdir? I tried

find -type d -exec rmdir -u '{}' \deleted;


but it didn't work, any idea if this is possible (without /bin/rm)?
 
Old 07-17-2006, 05:52 AM   #8
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi,

anyone help with the above post? can I use find and rmdir to remove empty directories within empty directories after using shred in one of the previous posts to delete the files?

Please Help!

Mike
 
Old 07-17-2006, 06:21 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Code:
function checkExisting ()
{
if [ -d "$TRASH/$@" ] || [ -f "$TRASH/$@" ]; then
If you pass 3 arguments to checkExisting(), then the expression "$TRASH/$@" will expand to "/home/username/DELETED/arg1 arg2 arg3"

Here is an example:
Code:
jschiwal@hpamd64:~/temp> function testfunction() { if [ -d "$@" ] ; then echo "is a dir"; elif [ -f "$@" ]; then echo "is a file"; fi; }
jschiwal@hpamd64:~/temp> ls
sample  testa  testb  testc
jschiwal@hpamd64:~/temp> testfunction testa
is a dir
jschiwal@hpamd64:~/temp> testfunction sample
is a file
jschiwal@hpamd64:~/temp> testfunction testa testb
bash: [: testa: binary operator expected
bash: [: testa: binary operator expected
 
Old 07-17-2006, 08:27 AM   #10
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi,

I managed to fix that, instead of using $@ I am now using a for loop to processs each argument
individually, can anyone help with the recursive removal of empty directories?


Also in this code, kindly recommended by hfawzy where do I specify the folder?
Code:
find -type f -exec shred -u '{}' \;
do I do it like this:
Code:
find -type f -exec shred -u '{}' $TRASH;
And does anyone know more about using rmdir as above like shred is being used to remove recursivley?

Thanks for any help!

Mike
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 02:21 PM
Can't fix this checksum error ! Binary File ! My VERY FIRST Question ! help pls !!!! Zaher Programming 1 03-20-2006 09:35 PM
shell script provides an error that a binary operator is required max_rsr Linux - General 1 03-12-2005 08:26 AM
Java "<identifier> expected" error. nro Programming 2 09-01-2004 12:20 AM
Error upgrading kernel, /sbin/mkinitrdi integer expression expected amp2000 Linux - General 2 12-05-2003 05:38 AM


All times are GMT -5. The time now is 02:54 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration