Wednesday, December 18, 2013

Script to copy files using getopt

www.unixbabuforum.inI am writing a script to copy files to a remote location and I need to pass 
the filenames using getopt 
I need to remove some 3 to 4 files, But I could delete only one file.

www.unixbabuforum.inTry this:
Isn't getopt() a C function in the GNU C library? 
Here's how I would do it: 
#!/bin/ksh 
if [[ $# = 0 ]]; then 
print "Usage: $(basename $0) file1 .. filen" 
print "\tfile1 is the file to be removed." 
print "\tAll other files will just be copied." 
exit 0 
fi 
fileList=$* 
fileToBeRemoved=${1} 
scp ${fileList} ${user}@${host}:${destinationDir} 
rm ${fileToBeRemoved}

www.unixbabuforum.ingetopt is useful when you want check options 
in "shell environment", you can use $1 $2 $3 .... $n or $* 
you can check number of tails with "$#" and if you want you can use "shift" 
builtin command.

0 comments:

Post a Comment

 
Design by BABU | Dedicated to grandfather | welcome to BABU-UNIX-FORUM