I am trying to write a shell script shell that caches information every time a command is called. He only needs to save the first argument without an option. For example, in
$ mycommand -o option1 -f another --spec more arg1 arg2
I want to get "arg1."
How can this be done in bash?
Using getopt is probably the way to go.
If you want to see the argument scanning code in bash, a method without getopt:
realargs="$@" while [ $# -gt 0 ]; do case "$1" in -x | -y | -z) echo recognized one argument option $1 with arg $2 shift ;; -a | -b | -c) echo recognized zero argument option $1, no extra shift ;; *) saveme=$1 break 2 ;; esac shift done set -- $realargs echo saved word: $saveme echo run real command: "$@"
- . bash , , . , , (.. "-f" "--file" ), , .
, DigitalRoss, case , (-), script "*)" , . , , , . "skip", .
, - getopt ( ).
getopt
Perhaps save the entire command line (so that you can pass it to the real tool without changes), then process the arguments with getopt, take the necessary information and run the swap tool.
Source: https://habr.com/ru/post/1718516/More articles:Become a fan of your Facebook page through the API? - apiгде я могу получить дистрибутив spring framework 3.0? - springHow do you use a variable stored in the closure of a forced spirit as an input to the parser of the syntax loop? - c ++Simplified SQL Min statement - sqltype of object references in ruby - javaInvalid Ruby on Rails authentication password - ruby | fooobar.comhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1718518/wpf-itemscontrol-cant-seem-to-get-items-to-display-in-the-view&usg=ALkJrhh7Joz2QaEJgThn8nfnu_UGww4auAHow to prevent css and js from being used by your ISP? - cssVery tricky CSS scroll issue - cssThe problem with makefile is creating .gch files instead of files. - c ++All Articles