arguments in bash | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

arguments in bash

Shell Programming and Scripting


Tags
bash, linux, ubuntu, unix

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-18-2011
elginmulizwa elginmulizwa is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 19 December 2011, 2:25 AM EST
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Java arguments in bash

this script moves all the doc files to a specified directory....i have managed to put an argument but the problem im facing is puting 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)

this is my code


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


Last edited by radoulov; 12-18-2011 at 07:10 AM.. Reason: Code tags!
Sponsored Links
    #2  
Old 12-18-2011
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 17 December 2014, 9:56 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
You may try something like this:


Code:
#!/bin/bash

From=/home/elg19/lone/doc
To=/home/elg19/documents

while getopts :d: opt; do
  case $opt in
    ( d ) To=$OPTARG                                                 ;;
    ( ? ) printf >&2 'usage: %s -d <dest_dir>\n' "${0##*/}"; exit 1  ;;
  esac
done

shopt -s nullglob

mv -- "$From"/*.{pdf,txt,doc} "$To/"

Note that there is no recursion in this case,
let me know if you really need to move the files
from the From directory an its subdirectories.

If the filenames exceed the ARG_MAX limit of your system, you will need xargs.

Last edited by radoulov; 12-18-2011 at 07:45 AM.. Reason: Simplified.
Sponsored Links
    #3  
Old 12-19-2011
elginmulizwa elginmulizwa is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 19 December 2011, 2:25 AM EST
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
i want to replace $To in the existing script with the command argument after -d ...
this code is not working i dont know why....

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 radoulov; 12-19-2011 at 03:11 AM.. Reason: Code tags!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bash read within function with arguments serverchief Shell Programming and Scripting 11 09-08-2011 01:34 PM
Bash too many arguments error killer54291 Shell Programming and Scripting 10 01-02-2011 03:31 PM
bash functions arguments Tártaro Shell Programming and Scripting 2 09-16-2009 03:57 PM
Make a list in bash out of arguments jolecanard Shell Programming and Scripting 4 07-02-2009 05:23 PM
Bash Shell - # of arguments DNAx86 Shell Programming and Scripting 1 01-10-2008 01:23 PM



All times are GMT -4. The time now is 07:06 PM.