LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-09-2014, 03:41 AM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 89

Rep: Reputation: 15
Improving a wrapper script


I just wrote a script which
1) Runs only if a lockfile is not present
2) Only if arguments are passed to it

#!/bin/sh
FILENAME=/opt/foo/bin/foo-exec
ARGVcount=$#
OPTS=...


if [[ -x $FILENAME ]]
then
echo "File '$FILENAME' is executable"
else
echo "File '$FILENAME' is not executable or found"
fi

# check if lockfile exists
LOCKFILE=/var/run/foo.lock
if [ -f $LOCKFILE ];then
echo "foo-exec locked."
echo "Use: $PRGNAME arguments"
exit 1
else
/opt/foo/bin/foo-exec $OPTS "$@"
fi

Does anybody see a better way to write this script? I tested it and works fine. But if there a more concise or efficient way to do it, I would like to know.
Please advise.

Thanks
 
Old 04-09-2014, 04:17 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,166

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
A quick look at your script shows that you're only going to exit of the lock file exists. You do not have any other exit conditions based on foo-exec not being executable or on a zero argument count.

As regards the "best" way of writing that script, whatever way works for you is the best way. I'm sure there's someone that would come along and give a single long and convoluted regular-expression matching one-liner buy it's much better to write scripts in a way you'll understand when you go back to it in 6 months time!
 
Old 04-09-2014, 05:56 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Firstly, please use [code][/code] tags for code or data to make it more readable.

As for your script, you seem to set variables and then never use them, or use them but, as said above, make no effort to inform the user.

Also, whilst I prefer double brackets over single the choice is of course your, however, pick one and stick with it.

Lastly, you pass all passed in arguments to your command but other than assigning how many to a variable that isn't used, you do not appear to check for any validity on the options passed in?
Do you assume that only users intimately familiar with your script will be running it and that they never make mistakes?
 
Old 04-09-2014, 06:11 AM   #4
threezerous
Member
 
Registered: Jul 2009
Posts: 89

Original Poster
Rep: Reputation: 15
Unhappy

Damn...I pasted the wrong script in my question ..The script I wrote was

Code:
#!/bin/sh
FILENAME=/opt/foo/bin/foo-exec
ARGVcount=$#
OPTS=...


if [[ -x $FILENAME ]]
then
    echo "File '$FILENAME' is executable"
else
    echo "File '$FILENAME' is not executable or found"
fi

# check if lockfile exists
LOCKFILE=/var/run/webmd-foo-exec.lock
if [ -f $LOCKFILE ];then
   if [ $ARGVcount == 0 ]; then
       echo "foo-exec locked. No updates will be done."
       echo "Use: $PRGNAME arguments"
       exit 1
   else
      /opt/foo/bin/foo-exec $OPTS "$@"
   fi
fi
/opt/foo/bin/foo-exec $OPTS "$@"
 
Old 04-09-2014, 07:47 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Still all the same points and now with the addition of calling your main command with the exact same details in 2 different parts of your code ... this would seem non-efficient to me.
 
Old 04-09-2014, 08:27 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,166

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
Also, if there IS a lockfile and arguments then the main command will run twice.

Rather than nesting your tests you could prioritise them, for example the code below will return a different $? as follows:

3 - File wasn't executable
2 - Lock File Exists
1 - User didn't provide parameters

Code:
#!/bin/sh
FILENAME=/opt/foo/bin/foo-exec
LOCKFILE=/var/run/webmd-foo-exec.lock
ARGVcount=$#
OPTS=...

if [[ -x ${FILENAME} ]] ; then
  echo "${FILENAME} is not executable or not found."
  exit 3
fi

# check if lockfile exists
if [ -f ${LOCKFILE} ]; then
  echo "${FILENAME} locked."
  exit 2  
fi

if [ ${ARGVcount} == 0 ]; then
  echo "Use: $PRGNAME arguments." # NOTE: PRGNAME doesn't appear to be defined anywhere in this script, perhaps use ${0} instead?
  exit 1
fi

${FILENAME} ${OPTS} "$@"

#Elvis Has Left The Building

Last edited by TenTenths; 04-09-2014 at 08:28 AM.
 
1 members found this post helpful.
Old 04-09-2014, 09:07 AM   #7
threezerous
Member
 
Registered: Jul 2009
Posts: 89

Original Poster
Rep: Reputation: 15
Thanks for the suggestions. I revised my script accordingly.
 
  


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] help: wrapper script for expect frkingkool Programming 5 08-16-2011 03:04 AM
[SOLVED] improving a sed script cruzdelsur Programming 12 08-27-2010 10:59 AM
script wrapper thread investmentbnker75 Programming 6 01-17-2010 12:48 AM
improving my script lpn1160 Linux - Newbie 4 06-19-2006 09:09 PM
server wrapper script yapp Programming 2 08-02-2003 12:09 PM


All times are GMT -5. The time now is 05:56 AM.

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