LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-18-2011, 12:40 PM   #1
congos
LQ Newbie
 
Registered: Oct 2011
Posts: 20

Rep: Reputation: Disabled
Post arguments in bash


This script moves all the doc files to a specified directory. I have managed to put an argument but the problem I am facing is putting the full path where the scripts are moving to, for example I want to run the script like this below

./loo -d then path where im moving the files (i.e ./loo -d the second argument where files are moving to)
is there anyone who can help me put $2 somewhere in the code so that it does what i want?

this is my code

#!/bin/bash
From="/home/elg19/lone/doc"
To="/home/elg19/documents"

if [ $1 = -d ]; then
cd "$From"
for i in pdf txt doc; do
find . -type f -name "*.${i}" -exec mv "{}" "$To" \;
done
fi
 
Old 12-18-2011, 01:27 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Understanding that you want to replace $To in the existing script with the command argument after -d ...
Code:
#!/bin/bash
From="/home/elg19/lone/doc"
To=$2

if [ $1 = -d ]; then
    cd "$From"
    for i in pdf txt doc; do
      find . -type f -name "*.${i}" -exec mv "{}" "$To" \;
    done
fi

Last edited by catkin; 12-18-2011 at 01:29 PM.
 
Old 12-18-2011, 02:02 PM   #3
congos
LQ Newbie
 
Registered: Oct 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
its giving me errors like can not move no such file or directory
 
Old 12-18-2011, 05:52 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Please post exact msg and/or use 'set -xv' thus
Code:
#!/bin/bash

# this shows what's happening
set -xv

From="/home/elg19/lone/doc"
To=$2

if [ $1 = -d ]; then
    cd "$From"
    for i in pdf txt doc; do
      find . -type f -name "*.${i}" -exec mv "{}" "$To" \;
    done
fi
 
Old 12-18-2011, 11:23 PM   #5
congos
LQ Newbie
 
Registered: Oct 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
mv: cannot move `./look.txt' to `elgn/Documents':No such file or directory
 
Old 12-19-2011, 03:54 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Can you confirm that the file is in the correct directory and that the To directory exists?
Remember you have cd'ed into From so your To must exist in the same directory as you have used a relative path.
So the following needs to exist:
Code:
/home/elg19/lone/doc/elgn/Documents
 
1 members found this post helpful.
  


Reply

Tags
bash, bash scripting, linux, unix



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
[SOLVED] Generating two bash arguments from 'ls' mzh Linux - General 4 07-06-2011 09:24 AM
bash script arguments R3N3G4D3 Programming 5 11-01-2007 10:16 AM
bash script - variable arguments indikabandara19 Linux - Newbie 4 05-21-2007 10:40 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 08:24 AM
bash arguments stuckinhell Programming 6 08-13-2004 05:10 AM

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

All times are GMT -5. The time now is 07:22 PM.

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