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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
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 |
| Sponsored Links | ||
|
|
|
|||
|
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 |
|
|||
|
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 |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |