nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-02-2005, 10:10 AM
Junior Member
User
 
Join Date: Feb 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 11-02-2005, 10:42 AM
Junior Member
User
 
Join Date: Feb 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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 11-02-2005, 04:08 PM
Administrator
User
 
Join Date: Jan 1970
Posts: 43
Thanks: 0
Thanked 1 Time in 1 Post
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 16-02-2005, 12:13 PM
Junior Member
User
 
Join Date: Feb 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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


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 Off


Similar Threads

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


All times are GMT +5.5. The time now is 10:03 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38