1

VALUE=${VAR| |o}

I found this variable assignment in shell script. I can't understand why || sign is used here.

EDIT

Code given in a comment:

#!/bin/bash
source /opt/cpf/bin/cpf_logging_helper.sh
STATE=$1
DNS_MASTER=${TEST.DNS.DNS_MASTER:-o}
service status dns > /dev/null
rval=$?
if [ $rval -eq 0 ]
then
    if [ $DNS_MASTER == `hostname -s` ]
    then
        echo "1"
    else
        echo "2"
    fi
fi

closed as off-topic by user79743, Anthon, Jeff Schaller, MelBurslan, don_crissti Jun 3 '16 at 15:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Community, Anthon, Jeff Schaller, MelBurslan, don_crissti
If this question can be reworded to fit the rules in the help center, please edit the question.

  • In which shell is this supposed to be run? It is a syntactical error in bash. – DopeGhoti Jun 3 '16 at 6:37
  • Its in bash. So , i changed it to VALUE=${VAR} ,but i wwanted to know the intention of coder. :) – Gaurav KS Jun 3 '16 at 6:39
  • You should ask the coder then)). Is it obvious from the script what kind of replace/suffix/prefix it could be? – Serge Jun 3 '16 at 6:41
  • So would I. I tried it in bash and got a syntax error, and there is no mention of this syntax in the manual page. Inline replacement syntax is ${var/foo/bar} to change foo to bar. – DopeGhoti Jun 3 '16 at 6:41
  • The construct about which you ask is not present in the sample code in the question. – DopeGhoti Jun 3 '16 at 18:07
1

It seems that the original coder meant to use the value of $VAR or o if it is unset. In bash this can be achieved by using:

VALUE=${VAR:-o}

See also man bash and search for Parameter Expansion or :-

  • VALUE=${VAR:-o} and VAR is neither defined in the script nor in the file sourced, i tried and its giving syntax error. – Gaurav KS Jun 3 '16 at 6:58
  • What is the version of bash you are using? – Lambert Jun 3 '16 at 7:07
  • GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) – Gaurav KS Jun 3 '16 at 7:11
  • #!/bin/bash source /opt/cpf/bin/cpf_logging_helper.sh STATE=$1 DNS_MASTER=${TEST.DNS.DNS_MASTER:-o} service status dns > /dev/null rval=$? if [ $rval -eq 0 ] then if [ $DNS_MASTER == hostname -s ] then echo "1" else echo "2" fi fi – Gaurav KS Jun 3 '16 at 7:15
  • It helps if you edit your question and put the script lines in a code block for formatting and readability. From the output given I can quickly see that the test [ $DNS_MASTER == hostname -s ] will not succeed (result in error: to many arguments). You'd better use [ "$DNS_MASTER" == "$(hostname -s)" ] – Lambert Jun 3 '16 at 7:30
1

The structure: VALUE=${VAR| |o} is invalid in dash, bash, ksh and zsh.

If the | | is actually a / / then it may(?) make sense in bash.

It would mean: replace the first occurrence of an space by an o.

Whether that is a reasonable replacement is another matter. I don't believe it is.

In the code added in the edit, the dots are not valid in a variable name neither in dash, bash or zsh and under very specific conditions in ksh.
This will fail in bash:

DNS_MASTER=${TEST.DNS.DNS_MASTER:-o}

Also, this test is incorrect, because of white space and because the hostname command is not being executed:

if [ $DNS_MASTER == hostname -s ]

Use:

if [ "$DNS_MASTER" == "$(hostname -s)" ]

In all, the code you present could not work correctly in bash, and is puzzling in most shells. Please edit your question to make it relevant.

0

The closest I can think of a shell-operator that looks like that, is the bash patch that William Park was maintaining in the years 2004-2008 that came with a few more or less useful extensions to bash3.

Among them was a series a parameter expansion operators that all started with ${var|...} some of them reminiscent of zsh's parameter flags (albeit with a completely different syntax).

In that shell, ${var| |o} would have been parsed as ${var|cmd}, (meant to be more or less the same as $(cmd "$var"}) but would have resulted in a invalid command error as that |o would not be seen as a valid command name (even if you created a command by that name).

Not the answer you're looking for? Browse other questions tagged or ask your own question.