Bash -o -v -R operators


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Bash -o -v -R operators
# 1  
Old 02-07-2018
Bash -o -v -R operators

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?

Code:
       -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.

# 2  
Old 02-07-2018
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
Code:
echo "$-"

the variable that displays these settings.

Quote:
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.
# 3  
Old 02-07-2018
How to use

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.
# 4  
Old 02-07-2018
You're right - those are "unary primaries" to be used in "Conditional Expressions". man bash:
Quote:
Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons. Expressions are formed from the following unary or binary primaries.
Use like
Code:
[ -v var ]  && echo "var set" || echo Nope

or in a corresponding if construct.

Last edited by RudiC; 02-07-2018 at 03:37 PM.. Reason: better phrasing
# 5  
Old 02-07-2018
Using -v

Using the following I am getting bash to complain by saying
Code:
../../../../siculet/amvib/day-sort.sh: line 152: syntax error near unexpected token `fi'
../../../../siculet/amvib/day-sort.sh: line 152: `fi'

Code:
if [ -v opt_day ] || [ -v opt_default ]; then
  sort_by_day $val_dir
elif
  sort_by_station $val_dir
fi

# 6  
Old 02-07-2018
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.
# 7  
Old 02-07-2018
Conditional

Quote:
Originally Posted by 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 ----------

Quote:
Originally Posted by 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?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk and Unary Operators in Bash

I am getting the following error /home/hagbard/chaos/resip/ictja/siculet-0.0.0/amvib/tmspan.sh: line 135: BEGIN {printf "%.0f", int((tm-(thh*60.0*60.0))/60.0)}: No such file or directory /home/hagbard/chaos/resip/ictja/siculet-0.0.0/amvib/tmspan.sh: line 138: The code is this ... (3 Replies)
Discussion started by: kristinu
3 Replies

2. Shell Programming and Scripting

operators in if loop

Hi all, I have a variable which is having the value like below, $ echo ${p} 8 15 22 30 $ My requirement is that the variable should return true when it contains only one number like below, $ echo ${p} 15 $ Otherwise, it should return false if it contains more than one number. I... (4 Replies)
Discussion started by: raghu.iv85
4 Replies

3. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

4. Shell Programming and Scripting

Can you use logical operators in a case statement (bash)?

I'm pretty sure I already know the answer to this, but I want to make sure I'm not overlooking anything. I'm working on a log monitoring script and every 10 lines I want to display a summary of events. The thing is, there are a lot of possible events, that likely won't have happened, so I only want... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

5. Homework & Coursework Questions

Operators

I really don't know the meaning of these operators. Could someone explain the meanings? <, <=, ==, !=, >=, >, ||, &&, ! ~ , !~ Thanks! (1 Reply)
Discussion started by: Erjen
1 Replies

6. Shell Programming and Scripting

Operators

I really don't know the meaning of these operators. Could someone explain the meanings so I can make my test for today? <, <=, ==, !=, >=, >, ||, &&, ! ~ , !~ Thanks! (1 Reply)
Discussion started by: Erjen
1 Replies

7. Shell Programming and Scripting

And and OR Operators with If Statement.

Hi All, I have 2 variables. Result1 and Result2. I want to put a condition that if Both are True then echo "All True" Else Show Error. Right now i am doing this and getting error. if ; then echo "All True" else echo "Failed" fi; Error. line 8: ' Solution: Looking for (2 Replies)
Discussion started by: mkashif
2 Replies

8. UNIX for Dummies Questions & Answers

Operators

I am trying to understand Does the following: {tmp+=$10} Mean take $10 and add them all up and call it tmp thanks! (2 Replies)
Discussion started by: llsmr777
2 Replies

9. UNIX for Advanced & Expert Users

If with set operators

Hi guys, I'm trying to run more than one "if" condition at once. What I want is something like if ] or ] or ]; then ... I can't remember the syntax for using this or/and set operators. Can someone please assist/ jog my memory? thanks Khoom (2 Replies)
Discussion started by: Khoomfire
2 Replies
Login or Register to Ask a Question