Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-14-2014, 05:50 PM   #1
Registered: Jul 2009
Posts: 94

Rep: Reputation: 15
Question wrapper script using getopts to pass all arguments except one

I have a wrapper script which need to execute the base script, when there is no lock, by accepting all arguments except the -F option. I got so far as below, but not sure how I can ignore the -F switch.

if [ ! -x $FILENAME ]; then
    echo "File '$FILENAME' is not present or is not executable"
    exit 1

# check if lockfile exists
if [ -f $LOCKFILE ];then
   while getopts ":F" opt; do
      case $opt in
            echo "Lock file present, running with force enabled"
            exec ${FILENAME} "$@"
            echo "script locked."
            exit 2
   exec ${FILENAME} "$@"
Whenever it tries to run ${FILENAME} "@" it should ignore -F switch but accept any remaining switches (it may not have any other switches either).

Any suggestions if this is feasible and how we can go about it? I was thinking of adding an argument count and then go through each argument and skip -F. But I am not sure that is the best/efficient way. Hoping there is a better way. Thanks much
Old 04-14-2014, 06:08 PM   #2
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,634

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
I would take the complete option string which is passed to the wrapper script. Then replace "-F" by a space. Then pass the result to your original script.

In case that you need to know whether "-F" was included in the original call you can search for that occurrence in the option string. Since you don't have to inspect each option and act upon it, this might be feasible.

Unless of course in the call can be a string which also holds "-F" like "MY-FILE". Then you could search for <space>-F<space>.

If that all is not feasible you can't avoid actually parsing all options. When you don't parse you don't know what is an option and what not.

Check for Bash string functions if you can use the search/replace option.

If not and you have to parse, I found this script very helpful:

1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ksh script Migrationfrom Solaris to Linux; getopts: arguments changed since last call hmhoang04 Linux - Newbie 3 03-07-2012 07:58 AM
How to pass arguments in a shell script? Drigo Linux - Newbie 1 11-14-2009 11:55 AM
getopts and mandatory arguments in BASH jmcejuela Programming 3 04-03-2009 04:00 PM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 02:18 PM
How to pass arguments from $prompt for php script ukjairaj Linux - Software 4 06-25-2004 11:14 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:44 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration