Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Running options in bash script

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-11-2018   -   Original Discussion by Cody Learner
Cody Learner's Unix or Linux Image
Cody Learner Cody Learner is offline
Registered User
 
Join Date: Jan 2018
Last Activity: 15 January 2018, 2:45 PM EST
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Running options in bash script

Hello UNIX & Linux Forums community! Long time Linux daily user hobbyist, new to shell scripting....

I'm working on a script that does all the "work" in one script, and makes calls to a second script to display info to the user via mostly expanding variables in heredocs.

I'm contemplating a -q --quiet option to stop the info displaying. Being new to this, I've thought through and tested the following.

However, I'm at a loss as to the terminology used for what I'm trying to do. That leaves me at a loss to find out if my method would be considered bad scripting, and if so, what would be a more appropriate method to easily implement a --quiet option.

The scripts are quite tested and working well at this point. This is more of an afterthought and mental exercise than something really needed.

BTW, I'm manually parsing options without using getopts. I could post the scripts, around 300 lines total, if needed. It's a (current version) bash script, running Arch Linux if it matters. (mostly current version prog's)



Code:
#!/bin/bash


mainfunc () {
    echo "run mainfunc commands"
    $switch    
}

optfunc () {
    echo "run user info extras"
}



normal () { switch=optfunc
}
quiet () { switch=  
}


while    :; do
    case "${1}" in
        -R)     normal ; mainfunc     ;;
       -Rq)     quiet  ; mainfunc    ;;
         *)    break
    esac
    shift
done

Sponsored Links
    #2  
Old Unix and Linux 01-12-2018   -   Original Discussion by Cody Learner
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 25 May 2018, 9:11 PM EDT
Location: San Jose, CA, USA
Posts: 11,311
Thanks: 637
Thanked 3,936 Times in 3,367 Posts
Why aren't you using getopts? Having consistent methods of parsing options greatly contributes to the ability of users unfamiliar with your code automatically know how to get a quick view of the syntax of your utility (with something like: utility -?), the ability to specify options separately or combined and in any order:


Code:
utility -Rq
utility -qR
utility -R -q
utility -q -R

should all produce the same results. And, you should have a built-in help/usage message if an unknown option is found. Instead, your code silently ignores unknown options (leading users to believe that the options they provided were correct when, instead, nothing will be done by your code).

You generally want to set flags while processing options, not run the functions that the options indicate should be run (after you have determined all of hte options that are present).

Your code seems to indicate that the -R option in your utility must always be present. Why have an option if it is always required to be set?
The Following User Says Thank You to Don Cragun For This Useful Post:
Cody Learner (01-12-2018)
Sponsored Links
    #3  
Old Unix and Linux 01-12-2018   -   Original Discussion by Cody Learner
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 25 May 2018, 10:49 AM EDT
Posts: 343
Thanks: 16
Thanked 96 Times in 91 Posts
I can think of two methods of implementing a quiet mode. First, use redirections.



Code:
if [ "${quietmode}" = "yes" ]
then 
   exec >/dev/null
fi
printf "this will not be printed in quiet mode\n"
printf "this is a required message and will be printed\n" > /dev/tty

Alternatively:


Code:
if [ "${quietmode}" = "yes" ]
then
   exec 3>/dev/null
else
   exec 3>&1
fi
printf "This won't be printed in quiet mode\n" >&3
printf "This will always be printed\n"

The other way is to use a function:


Code:
function myprint() {
   local mode=$1
   shift
   if [ "${mode}" = normal ] && [ "${quietmode}" = "yes" ]
   then
      return
   fi
   printf "$@"
}

myprint normal "this will not be printed in quiet mode\n"
myprint quiet "this will be printed always\n"

Untested.

Andrew
The Following User Says Thank You to apmcd47 For This Useful Post:
Cody Learner (01-12-2018)
    #4  
Old Unix and Linux 01-12-2018   -   Original Discussion by Cody Learner
Cody Learner's Unix or Linux Image
Cody Learner Cody Learner is offline
Registered User
 
Join Date: Jan 2018
Last Activity: 15 January 2018, 2:45 PM EST
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks for replies.

Second time someone asked why not using getopts. I have not looked into it so far, although I probably should.

With manually parsing options, I consider each argument as an operation rather than an option. No real option ability...

When the program is called with no operations, or a non existent op, they get this or in the former, without the error message.:



Code:
$ aurt -qR

  Input Error: Please try again or see    aurt --help

 |=====================================================================================|
 |    Aurt, a tiny AUR helper script.         USAGE:  $ aurt [operation] [package]     |
 |-------------------------------------------------------------------------------------|
 | -S  Install Update                -F  Files Provided By Pkg          NOTES:         |
 | -Sv Update By Version             -s  Search For Pkg                                |
 | -Sf Force Install Update          -p  Pacman Log                                    |
 | -R  Remove Pkg And Bld Dir        -h  Help                      leafpad-aurt: lat   |
 | -C  Ck All Pkgs For Updates                                  thunar-base dir: tbd   |
 | -D  Package Dependency Details                              Orphans:  R $(p -Qtdq)  |
 |=====================================================================================|

The scripts:


Code:
https:bbs.archlinux.org/viewtopic.php?id=232437

I was contemplating using the --quiet "option" with the -S and -Sv operations only, as the rest couldn't use --quiet.

I read getopts will not take long options? My script needs the ability to process package names, ie: aurt -S <package name> That and manually parsing them seemed more straight forward to me when I started, likely because of my inexperience.

At this point, it sounds like I need to get my head around understanding getopts and figure out how to deal with the program names separately?

Just reading the replies from you guys gives my more things to research, test, and think about.
Sponsored Links
    #5  
Old Unix and Linux 01-12-2018   -   Original Discussion by Cody Learner
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 25 May 2018, 3:04 PM EDT
Location: Saskatchewan
Posts: 22,680
Thanks: 1,179
Thanked 4,324 Times in 3,987 Posts
getopt does not prevent you from getting package names. That's not what "long options" means also, long options means double-dash-word options like --quiet.
Sponsored Links
    #6  
Old Unix and Linux 01-12-2018   -   Original Discussion by Cody Learner
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 25 May 2018, 9:11 PM EDT
Location: San Jose, CA, USA
Posts: 11,311
Thanks: 637
Thanked 3,936 Times in 3,367 Posts
In addition to what Corona688 already said (although I think he meant getopts instead of getopt), in your example:


Code:
aurt -S <package name>

<package name> is either a -S option option-argument or an aurt operand; not an option. You haven't given us enough information to know which you intended in your example. The getopts utility does not place any length limit on option-argument nor operand strings.

Furthermore with many versions of the Korn shell, the built-in getopts utility can handle long and short options (although some ksh man pages fail to document this feature). The method ksh uses doesn't work on bash version 3.2.57. I don't know if it works on bash 4.x version and, you haven't said what version of bash you're using.

To investigate this further, check the thread ksh parsing arguments in a string rather than from the cmdln.
Sponsored Links
    #7  
Old Unix and Linux 01-15-2018   -   Original Discussion by Cody Learner
drl's Unix or Linux Image
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 21 May 2018, 12:52 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,223
Thanks: 260
Thanked 420 Times in 361 Posts
Hi.

There are many schemes / methods / alternatives to help process command line -- CLI -- options. Here are a few. Some take a bit of effort to learn, but they will be time-savers in future work.

I especially like the ones that create man-like pages, and help displays automatically ... cheers, drl


Code:
Process command-line (CLI) options, arguments

        1) getopts, builtin, bash, ksh, zsh
           http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options
           Also, search for option processing, including:
           http://mywiki.wooledge.org/ComplexOptionParsing

        2) perl: many, including libgetopt-euclid-perl, which
           creates man page, help automatically

        3) getopt, enhanced getopts, part of the util-linux, allows GNU "--"
           Examples: /usr/share/doc/util-linux/examples, 2016.03.27

        4) argp.sh, wrapper for getopt, creates man and help (text, XML), etc.
           Allows mixed options and arguments.
           Compiled argp.c -> argp for faster execution.
           https://sourceforge.net/projects/argpsh/, 2016.03.27

        5) shflags, wrapper for getopt, creates help, allow mixed options
           and arguments
           https://github.com/kward/shflags, 2016.08.01

        6) ksh getopts, enhanced, process GNU "--", creates man, help, etc.
           Examples: Learning the Korn Shell, O'Reilly, 2nd, p 380ff

        7) zsh zparseopts
           man zshmodules, part of zshutil

        8) Suggested option names:
           http://www.shelldorado.com/goodcoding/cmdargs.html#flagnames

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Running a remote script with automatic options Mannu2525 Shell Programming and Scripting 2 12-26-2016 09:10 AM
Reading command line options from bash script kristinu Shell Programming and Scripting 22 03-20-2012 09:12 PM
how to add new options in bash script aphryllyn1 UNIX for Advanced & Expert Users 12 03-12-2012 04:21 AM
Running set options from the command line and bash command Straitsfan UNIX for Dummies Questions & Answers 5 07-28-2011 11:27 AM
Passing options to a bash script DeCoTwc Shell Programming and Scripting 3 05-21-2008 01:43 AM



All times are GMT -4. The time now is 10:32 PM.