LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: mv command and arguments (https://www.linuxquestions.org/questions/programming-9/bash-mv-command-and-arguments-4175438882/)

fatalerror0x00 11-27-2012 03:52 AM

Bash: mv command and arguments
 
I'm writing a script so that instead of removing files with rm I have my own way or doing so and it puts files into a folder in the tmp directory heres where i run into issues. for now I can't mv any files there through my script using the second argument as the file to move so I have

Code:


#!/bin/bash

recycleBin=/tmp/recycle_bin

recycle_files() {

    mv -v "$2" $recycleBin

}

case "$1" in

    -r)
          recycle_file
          ;;

esac

I don't really see anything wrong with that code btw there are other pieces of code to the document but everything else works fine it's very small and lightweight and I get an error when I do rcbin -r test3/

mv: cannot stat '': No such file or directory

so some reason mv isn't picking up argument 2 of my script. Any ideas?

fakie_flip 11-27-2012 04:04 AM

Put

set -xv

After your hashbang line. Then run the script again, and it will give you some useful debugging output.

fatalerror0x00 11-27-2012 04:20 AM

it's giving me exactly what I told you is happening mv isn't grabbing $2 cause it shows it gets to recycle_files and then mv -v '' /tmp/recycle_bin mv: cannot stat '': No such file or directory

hope it helps to have confirmed it though :)

millgates 11-27-2012 04:38 AM

@fatalerror0x00: What does "$2" mean in your script?

fakie_flip 11-27-2012 05:59 AM

Code:

[root@beastlinux ~]# touch garbage
[root@beastlinux ~]# ./recycle -r garbage
`garbage' -> `/tmp/recycle_bin/garbage'
[root@beastlinux ~]# cat recycle
#!/bin/bash

recycleBin=/tmp/recycle_bin

function recycle_files {
  mv -v "$1" $recycleBin
}

case "$1" in
  -r) recycle_files $2 ;;
  *) echo "-r wasn't used" ;;
esac
[root@beastlinux ~]#


When you use $2 in a function, that means the 2nd argument passed to that function which was nothing when you were trying to run it. So I take the 2nd argument from the command line and pass it to the function. In the function, that is the first argument passed.

fatalerror0x00 11-27-2012 11:49 AM

$2 is the second argument so when I type in rcbin -r test3

rcbin is $0 the command you type $1 is -r the first argument and test3 is $2 it's a simple way of doing arguments and for me it's effective I guess there are other and better ways but this is the only method I know and it works great for me :)

alright code given works after I realized I type $2 at the end of the wrong function :P thank you guys :)

David the H. 11-28-2012 09:13 AM

Code:

#!/bin/bash

recycleBin=/tmp/recycle_bin

recycle_files() {

    mv -v -t "$recycleBin" "$@"

}

case "$1" in

    -r)  shift
          recycle_files "$@"
          ;;
      *)  mv "$@"
          ;;

esac

exit 0

This should not only be safer but will also allow you to move multiple files at once, as well as allow you to pass other mv arguments to it as-is. I also inserted a default action so that if the first argument isn't "-r" then it acts like a standard mv. You can change this to whatever you want, of course.


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