nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Shell Script dought

This is a discussion on Shell Script dought within the Shell scripting forums, part of the Development/Scripting category; Hi, I have small dought and got a bit confused about this code. flags="" while getopts "difPRrvw" opt do case ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 02-11-2005, 11:10 AM
Junior Member
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0
Mahesh
Default Shell Script dought

Hi,

I have small dought and got a bit confused about this code.

flags=""
while getopts "difPRrvw" opt
do
case $opt in
f ) exec cp "$@" ;;
* ) flags="$flags - $opt" ;;
esac
done

shift $(( $OPTIND - 1 ))

Actually this code is a part of the script which uses to make a duplicate copy of file/directory when ever there is an usage of cp command..

here flags are nothing but the "options given"

So can anybody help me out to understand these code line by line as i totally couldnt work around it.

What is OPTIND, opt, getopts etcc...?

Let me know your ideas and helps..
Thanks in Advance

Mahesh
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-11-2005, 11:42 AM
Junior Member
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0
Mahesh
Default

Hi,

Again i am having one more line as

exec $cp $flags "$@"
This line is the last line in the script.

in previous post $cp is nothing but /bin/cp
$@ -- for all parameters i understand but why it is been used here
I hope you people will help me out in understanding these things..

Thanks in advance
Reply With Quote
  #3 (permalink)  
Old 02-11-2005, 05:08 PM
Administrator
Site Admin
 
Join Date: Jan 1970
Posts: 43
Rep Power: 10
vivek has disabled reputation
Default

The getopts command is use by shell to parse command line argument.

Syntax is
getopts {optsring} {variable1}

Where
optstring contains the option letters to be recognized

So in your script difPRrvw all are option to /bin/cp commands. All theses options can be read from man cp command.

$@ means all arguments passed to shell script.
OPTIND is special variable of getopts command which is nothing bit The index of the next argument to be processed by the getopts

Opt is variabe name when option matches it places in varilable opt. This is just variable name it can be anything.

So each time it is getopts get called it places the next option in the shell variable opt, initializing opt if it does not exist, and the index of the next argument to be processed into the variable OPTIND

$@ is used to match what ever source and target directory passed to script are passed as it is to /bin/cp command. So if script called as follows
./script –fir /home/ab/ab1.txt /home/ab/ab2.txt /backup

$@ will match to /home/ab/ab1.txt /home/ab/ab2.txt /backup

while getopts "difPRrvw" opt --> this line will scan all command line args passes to script
do
case $opt in
f ) exec cp "$@" ;; --> when option f encounters it will only run cp command
* ) flags="$flags - $opt" ;; --> This will add all arguments to flags variable
esac
done

shift $(( $OPTIND - 1 )) --> This will shift the index of the next argument

Hope this clears your mind. Feel free to ask more

Also checkout http://nixcraft.net/linux/lsst/ch04sec13.html
Reply With Quote
  #4 (permalink)  
Old 02-16-2005, 01:13 PM
Junior Member
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0
Mahesh
Default Thanks a lot

Hi Vivek,

Its cool..
Its was very good explanation..
Thanks for your help and sepending some time on it..

Mahesh
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Run mv command from ftp shell script subin Shell scripting 6 05-02-2008 10:44 AM
shell script for ftp the file vishal_titre Shell scripting 3 12-10-2007 09:40 AM
passing options to a shell script bigpaw Shell scripting 2 10-11-2007 03:40 AM
writing a shell script to find out my shell name jaymob123 Shell scripting 1 10-08-2007 01:36 AM
require shell script puppen Shell scripting 4 04-12-2006 10:42 PM


All times are GMT +5.5. The time now is 05:48 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36