LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2012, 04:52 AM   #1
fatalerror0x00
Member
 
Registered: Oct 2012
Location: Holden, ME, USA
Distribution: SlackWare64 14.00
Posts: 185

Rep: Reputation: 0
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?
 
Old 11-27-2012, 05:04 AM   #2
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,452

Rep: Reputation: 85
Put

set -xv

After your hashbang line. Then run the script again, and it will give you some useful debugging output.
 
Old 11-27-2012, 05:20 AM   #3
fatalerror0x00
Member
 
Registered: Oct 2012
Location: Holden, ME, USA
Distribution: SlackWare64 14.00
Posts: 185

Original Poster
Rep: Reputation: 0
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
 
Old 11-27-2012, 05:38 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

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

Last edited by millgates; 11-27-2012 at 05:41 AM.
 
Old 11-27-2012, 06:59 AM   #5
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,452

Rep: Reputation: 85
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.

Last edited by fakie_flip; 11-27-2012 at 07:01 AM.
 
Old 11-27-2012, 12:49 PM   #6
fatalerror0x00
Member
 
Registered: Oct 2012
Location: Holden, ME, USA
Distribution: SlackWare64 14.00
Posts: 185

Original Poster
Rep: Reputation: 0
$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
 
Old 11-28-2012, 10:13 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
  


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
Problem with analyzing command line arguments: bash shell script newbiecolonopenparens Linux - Newbie 7 05-24-2012 09:29 PM
[SOLVED] bash command line arguments izghitu Linux - Software 4 07-01-2010 10:23 AM
getting command line arguments in bash DEF. Programming 4 05-06-2009 12:50 PM
Bash command line arguments msreddy999 Programming 1 03-20-2009 03:56 PM
Bash command separator/arguments separator reverse Programming 11 12-02-2007 10:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 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