LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-14-2014, 06:50 PM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 89

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.
Code:
FILENAME=/somedir/test.sh

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

# check if lockfile exists
LOCKFILE=/var/run/lockfile.lck
if [ -f $LOCKFILE ];then
   while getopts ":F" opt; do
      case $opt in
         F)
            echo "Lock file present, running with force enabled"
            exec ${FILENAME} "$@"
            ;;
         \?)
            echo "script locked."
            exit 2
         ;;
      esac
   done
else
   exec ${FILENAME} "$@"
fi
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, 07:08 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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: http://code.google.com/p/shflags/

jlinkels
 
1 members found this post helpful.
  


Reply


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 08:58 AM
How to pass arguments in a shell script? Drigo Linux - Newbie 1 11-14-2009 12:55 PM
getopts and mandatory arguments in BASH jmcejuela Programming 3 04-03-2009 05:00 PM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 03:18 PM
How to pass arguments from $prompt for php script ukjairaj Linux - Software 4 06-25-2004 12:14 PM


All times are GMT -5. The time now is 06:25 PM.

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