I do not know the use of the -o -v -R operators.
This is what the info says and I am confused of what optname and varname
mean, are they just normal variable?
-o optname
True if the shell option optname is enabled. See the list of
options under the description of the -o option to the set
builtin below.
-v varname
True if the shell variable varname is set (has been assigned a
value).
-R varname
True if the shell variable varname is set and is a name refer
ence.
I think you are looking at the help for set
. They change the behavior of bash.
Here is a better chunk - note that options for set show up via different options.
These settings control bash's behavior. They are not ordinary variables.
Example: -x turns on a kind of running debug display, great for fixing problems, but not for production code.
For starters try: try
echo "$-"
the variable that displays these settings.
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
Set or unset values of shell options and positional parameters.
Change the value of shell attributes and positional parameters, or
display the names and values of shell variables.
Options:
-a Mark variables which are modified or created for export.
-b Notify of job termination immediately.
-e Exit immediately if a command exits with a non-zero status.
-f Disable file name generation (globbing).
-h Remember the location of commands as they are looked up.
-k All assignment arguments are placed in the environment for a
command, not just those that precede the command name.
-m Job control is enabled.
-n Read commands but do not execute them.
-o option-name
Set the variable corresponding to option-name:
allexport same as -a
braceexpand same as -B
emacs use an emacs-style line editing interface
errexit same as -e
errtrace same as -E
functrace same as -T
hashall same as -h
histexpand same as -H
history enable command history
igncr on Cygwin, ignore \r in line endings
ignoreeof the shell will not exit upon reading EOF
interactive-comments
allow comments to appear in interactive commands
keyword same as -k
monitor same as -m
noclobber same as -C
noexec same as -n
noglob same as -f
nolog currently accepted but ignored
notify same as -b
nounset same as -u
onecmd same as -t
physical same as -P
pipefail the return value of a pipeline is the status of
the last command to exit with a non-zero status,
or zero if no command exited with a non-zero status
posix change the behavior of bash where the default
operation differs from the Posix standard to
match the standard
privileged same as -p
verbose same as -v
vi use a vi-style line editing interface
xtrace same as -x
-p Turned on whenever the real and effective user ids do not match.
Disables processing of the $ENV file and importing of shell
functions. Turning this option off causes the effective uid and
gid to be set to the real uid and gid.
-t Exit after reading and executing one command.
-u Treat unset variables as an error when substituting.
-v Print shell input lines as they are read.
-x Print commands and their arguments as they are executed.
-B the shell will perform brace expansion
-C If set, disallow existing regular files to be overwritten
by redirection of output.
-E If set, the ERR trap is inherited by shell functions.
-H Enable ! style history substitution. This flag is on
by default when the shell is interactive.
-P If set, do not resolve symbolic links when executing commands
such as cd which change the current directory.
-T If set, the DEBUG and RETURN traps are inherited by shell functions.
-- Assign any remaining arguments to the positional parameters.
If there are no remaining arguments, the positional parameters
are unset.
Assign any remaining arguments to the positional parameters.
The -x and -v options are turned off.
Using + rather than - causes these flags to be turned off. The
flags can also be used upon invocation of the shell. The current
set of flags may be found in $-. The remaining n ARGs are positional
parameters and are assigned, in order, to $1, $2, .. $n. If no
ARGs are given, all shell variables are printed.
Exit Status:
Returns success unless an invalid option is given.
They are in the section Conditional Expressions when I do info bash.
How can one use them. I have tried searching around but have not found examples.
RudiC
February 7, 2018, 2:04pm
4
You're right - those are "unary primaries" to be used in "Conditional Expressions". man bash
:
Use like
[ -v var ] && echo "var set" || echo Nope
or in a corresponding if
construct.
Using the following I am getting bash to complain by saying
../../../../siculet/amvib/day-sort.sh: line 152: syntax error near unexpected token `fi'
../../../../siculet/amvib/day-sort.sh: line 152: `fi'
if [ -v opt_day ] || [ -v opt_default ]; then
sort_by_day $val_dir
elif
sort_by_station $val_dir
fi
RudiC
February 7, 2018, 2:35pm
6
How about testing / checking / verifying what you learned in small steps, e.g. by running the small snippet posted? And then only proceeding to a more complex example?
For your above problem (totally different story!): elif
needs an additional test and a then
keyword... c.f. man bash
.
rudic:
How about testing / checking / verifying what you learned in small steps, e.g. by running the small snippet posted? And then only proceeding to a more complex example?
For your above problem (totally different story!): elif
needs an additional test and a then
keyword... c.f. man bash
.
I have tried the example and now need to use in an if condition. Do not understand how I need an additional test and a then
keyword. How does that work out?
---------- Post updated at 02:49 PM ---------- Previous update was at 02:43 PM ----------
rudic:
How about testing / checking / verifying what you learned in small steps, e.g. by running the small snippet posted? And then only proceeding to a more complex example?
For your above problem (totally different story!): elif
needs an additional test and a then
keyword... c.f. man bash
.
I understand now
---------- Post updated at 03:56 PM ---------- Previous update was at 02:49 PM ----------
How about -o optname? What is a shell option optname
And for -R optname, what is a name reference?