LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Simplified, limited, dumb, atrocious, fake "getopts" -like implementation for bash scripts

Posted 08-05-2023 at 08:17 PM by the dsc
Updated 05-29-2024 at 05:55 PM by the dsc (maybe an "improvement")

Perhaps a less obtuse and less limited version that's likely still not as functional/bug-free or even as handy as proper "getopts":

Code:
[[ $@ =~ (--pt:[^[:space:]]+) ]] &&  size="${BASH_REMATCH[1]#*:}"
[[ $@ =~ (--b:[^[:space:]]+) ]] &&  b="${BASH_REMATCH[1]#*:}"

input=$(echo "${@}" | sed 's/--[^:]*:[^ ]*//g')
That BASH_REMATCH magic is "grepping" patterns along the lines of "--option:value" and stripping out what is not the value (assumes it's ended by a space, there's this limitation at least), if it's there, anywhere, on the input parameters.

Then "input" itself is stripped off "--option:value" patterns.

Assembled it from somewhat more prolix/lenghtier "phrasings" that came up from a chatGPT, for some reason full of less succinct if-thens and this=$(echo $that | sed something).

First time I heard of this BATCH_REMATCH thing, I even duckduck-went a bit to check whether it was a real thing or just some nonsense solution from chatGPT, like being an ordinary variable set elsewhere somehow in some context and being misunderstood by GPT as having such magic powers or something.



__________

Original, more limited/dumber version, that perhaps may have some value in some minor detail I'm missing


Code:
lastarg=${@:$#}
# options should come all together on the last argument, such as:
# $./script.sh file1.foo file2.bar --scr-opts:optA,optC,opt3,opt2

echo "${@}" | sed  's/--scr-opts:.*//' | sed  '/^\s*$/d' > /dev/shm/scripttmpfilelistorwhatever$$
# that strips the files (or something else coming before the options) and adds to a temp file. 
# The second sed deletes empty lines, for some reason. I guess sometimes they may end up there somehow, 
# although right now I can't fathom how. Anyway, the most important was the part before.
# The options themselves are then tackled as such:

opts=$(echo "${@}" | sed 's|.*--scr-opts||') # new tweak to avoid false triggering options
if [[ ${opts} =~ "optA" ]] ; then
echo "that's the option A" ; fi

# besides the limitation that the options should be all clumped together at the end, they can't have
# names/labels/flags that overlap in a way that would cause false positives, such as "optA" and "optAB,"
# as "optAB" will also "trigger" "optA" being there, which may not be wanted. I guess one could then 
# have some set of if/thens that would somehow figure out that it was AB and not A, but then it's 
# probably a better idea to use the proper getops or some other better alternative that's perhaps
# what one should have done from the start anyway.
I ran with a problem of a filename that had the letters "set" and one of the available options is also "set," giving a false positive. Partly solved with an if/then that at least checks if there are options at all, but I guess the corollary is that there's still potential for false positives when there are options and something else on "${@}" coincides with the characters. I thought I had some sed command stripping non-option parts of "${@}," but I guess I don't. Edited this sketch with this sed-stripping idea.
Posted in Uncategorized
Views 186 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 09:41 PM.

Main Menu
Advertisement
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