LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 01: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, 02:27 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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 02:29 PM.
 
Old 12-18-2011, 03: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, 06:52 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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-19-2011, 12:23 AM   #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, 04:54 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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


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


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