LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   arguments in bash (http://www.linuxquestions.org/questions/linux-newbie-8/arguments-in-bash-919355/)

congos 12-18-2011 12:40 PM

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

catkin 12-18-2011 01:27 PM

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


congos 12-18-2011 02:02 PM

its giving me errors like can not move no such file or directory

chrism01 12-18-2011 05:52 PM

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


congos 12-18-2011 11:23 PM

mv: cannot move `./look.txt' to `elgn/Documents':No such file or directory

grail 12-19-2011 03:54 AM

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


All times are GMT -5. The time now is 11:51 AM.