LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 12-19-2021, 10:11 AM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Handling default values when using getopts t::


I have written the following function and want to set a default if user does not supply a value to the option "-t". Haw can I handle this?


Code:
myfunc ()
{
 local nt ng
 local OPTIND OPTARG
 local shortopts=":t::g:"
 while getopts "$shortopts" arg; do
  case $arg in
   ("t") nt=${OPTARG} ;;
   ("g") ng=${OPTARG} ;;
   (*)
      printf "getopts: Error Reporting Mode | Silent "
      printf "arg: $arg"
      case $arg in
       (":")
         printf " No value provided to -${OPTARG} option."
         printf " Give a value, e.g. \"-${OPTARG} 3\"."
         break
         ;;
       ("?")
         printf "User option not dufined in shortopts."
         printf "Invalid -${OPTARG} option provided."
         break
         ;;
      esac
      break
      ;;
  esac    
 done
 printf '%s\n' "nt: $nt | ng: $ng"
}

Last edited by Faki; 12-19-2021 at 12:32 PM.
 
Old 12-19-2021, 10:44 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Set the variable up in advance and let parsing of options set it, if they use it.

Or check for existence of the variable and use a default if it wasn't defined.
 
Old 12-19-2021, 10:50 AM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Would the default value have to be set after the "while getopts" construct? What happens with "nt=${OPTARG}" if there in no "arg"?
 
Old 12-19-2021, 10:58 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by Faki View Post
Would the default value have to be set after the "while getopts" construct?
default should be set before the loop
Quote:
Originally Posted by Faki View Post
What happens with "nt=${OPTARG}" if there in no "arg"?
getopts will set OPTARG if : was specified (or will report an error if that argument was missing). But you can check it yourself. Or see (for example) here: https://www.golinuxcloud.com/bash-getopts/
 
Old 12-19-2021, 12:02 PM   #5
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I am still quite unsure about this. I want the default to be nt=0 if the "-t" option is not specified. If it is specified only with "-t" I want to have "nt=1", or use the user defined one if supplied with (e.g. "-t3").
 
Old 12-19-2021, 12:27 PM   #6
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Why does this

Code:
myfunc -t -g23 gungadin
produce

Code:
nt: -g23 | ng: 0

Last edited by Faki; 12-19-2021 at 12:31 PM.
 
Old 12-19-2021, 12:32 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
because you specified that. -t requires an argument, so -g23 will be taken as an argument to -t, not an option.
 
Old 12-19-2021, 12:45 PM   #8
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I have used "t::". Would that not mean that the value is optional?
 
Old 12-19-2021, 01:55 PM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
there is no [reliable] way to decide if the string after -t is the next option or an argument to -t.
 
Old 12-19-2021, 02:00 PM   #10
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Does getopts support double colon "::" though? I think there is some program that accepts "::" for optional values but cannot remember if it is "getopts" or not.
 
Old 12-19-2021, 02:05 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
:: not supported.
https://unix.stackexchange.com/quest...ts-be-optional
and again: https://www.golinuxcloud.com/bash-getopts/
and again: there is no [reliable] way to decide if the string after an option is the next option or an argument.
 
  


Reply

Tags
bash, getopts



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
Handling long options with getopts Faki Linux - Software 6 10-31-2021 09:44 AM
cron jobs: handling failure and return values sneakyimp Linux - Newbie 1 03-08-2017 07:22 PM
[SOLVED] using getopts with flags to backup cronjobs linuxnewbie0101 Linux - Newbie 86 07-14-2016 01:27 PM
wrapper script using getopts to pass all arguments except one threezerous Linux - Newbie 1 04-14-2014 06:08 PM
Using getopts in Shell properly?? khandu Programming 2 01-12-2012 07:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:13 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
Open Source Consulting | Domain Registration