• Unset or empty variable test in (t)csh

    From Hauke Fath@21:1/5 to All on Fri Nov 19 18:05:34 2021
    Hi,

    what is the proper {t,}csh way to test if a variable is either unset, or
    empty?

    The naive

    if ( ! ${?TERM} || ${TERM} == "" ) ...

    errors out in the second expression, for obvious reasons. While tcsh
    does do short circuit expressions, it attempts to expand variables
    beforehand.

    Cheerio,
    Hauke


    ObHint: " | sed '/csh.*harmful/d'"

    --
    Now without signature.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to Hauke Fath on Fri Nov 19 17:29:35 2021
    In article <1piwk1d.1qnfocmjw81h7N%dont.spam.usenet@googlemail.com>,
    Hauke Fath <hauke@Espresso.Rhein-Neckar.DE> wrote:
    Hi,

    what is the proper {t,}csh way to test if a variable is either unset, or >empty?

    The naive

    if ( ! ${?TERM} || ${TERM} == "" ) ...

    errors out in the second expression, for obvious reasons. While tcsh
    does do short circuit expressions, it attempts to expand variables >beforehand.

    You have to do it step by step. For example, the following works for me:

    if $?TERM then
    if $TERM == "" then
    echo "TERM is set but empty"
    else
    echo "TERM is set to: $TERM"
    endif
    else
    echo "TERM is unset"
    endif


    ObHint: " | sed '/csh.*harmful/d'"

    Well done!

    But, yeah, the syntax of csh/tcsh is kinda creaky, but that's mainly because
    it hasn't been maintained (i.e., enhanced, built up, etc) in decades. It pretty much is what it was in the 1980s. But the fact is, if you use tcsh
    as your interactive shell, as I still do, you still need to be able to edit your startup files and stuff. So, you still need to keeping de old skills alive!

    --
    In politics and in life, ignorance is not a virtue.
    -- Barack Obama --

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hauke Fath@21:1/5 to Kenny McCormack on Fri Nov 19 19:01:16 2021
    Kenny McCormack <gazelle@shell.xmission.com> wrote:

    Hauke Fath <hauke@Espresso.Rhein-Neckar.DE> wrote:
    if ( ! ${?TERM} || ${TERM} == "" ) ...

    errors out in the second expression, [...]

    You have to do it step by step. For example, the following works for me:

    if $?TERM then
    if $TERM == "" then
    echo "TERM is set but empty"
    else
    echo "TERM is set to: $TERM"
    endif
    else
    echo "TERM is unset"
    endif

    The problem is, this does not give you 'TERM is either unset, or empty'
    in one branch.

    For the background, the test is from csh.login where I want to run
    tset(1) only if TERM is not sane, to avoid side-effects. Since I just
    learned that "eval `tset -QIs ...`" will only produce a string, and not re-initialize the terminal, I am now running tset unconditionally.

    ObHint: " | sed '/csh.*harmful/d'"

    Well done!

    From the grab bag of "The [t]csh user's hundred helpful sed one-liners"

    [1] ;)

    But, yeah, the syntax of csh/tcsh is kinda creaky, but that's mainly because it hasn't been maintained (i.e., enhanced, built up, etc) in decades.

    AFAIK csh has a handwritten parser, no defined grammar. Many things are
    just as irregular as they are, take 'em or leave 'em.

    Cheerio,
    Hauke


    [1] Also: <https://meyerweb.com/eric/comment/chech.html>

    --
    Now without signature.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to Hauke Fath on Fri Nov 19 18:32:44 2021
    In article <1piwodk.rj170lk47a9sN%dont.spam.usenet@googlemail.com>,
    Hauke Fath <hauke@Espresso.Rhein-Neckar.DE> wrote:
    ...
    FTR, I just came across [1] whose

    if ( ! ${?TERM} || { eval `if ( ${TERM} == "" ) exit 1` } ) ...

    fits my bill.

    Wow. I had no idea...

    --
    People often ask what is the difference between liberals and conservatives.
    It is this. Libs see the government helping them and are OK with the government also helping other people. Cons see the government screwing them and are OK with
    that as long as the government is also screwing other people.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to Hauke Fath on Fri Nov 19 18:30:40 2021
    In article <1piwmwf.1pl1i9s16nqowbN%dont.spam.usenet@googlemail.com>,
    Hauke Fath <hauke@Espresso.Rhein-Neckar.DE> wrote:
    ...
    The problem is, this does not give you 'TERM is either unset, or empty'
    in one branch.

    For the background, the test is from csh.login where I want to run
    tset(1) only if TERM is not sane, to avoid side-effects. Since I just
    learned that "eval `tset -QIs ...`" will only produce a string, and not >re-initialize the terminal, I am now running tset unconditionally.

    How about:

    if ! $?TERM set TERM=""
    if $TERM == "" echo "TERM is unset or empty"

    ObCrabbingAboutCSH: One thing that does annoy me is that "set" requires an equals sign, but "setenv" does not. And by "requires", I mean it only
    works one way. You have to always remember to use an "=" with "set" and to
    NOT use one with "setenv".

    --
    Alice was something of a handful to her father, Theodore Roosevelt. He was once asked by a visiting dignitary about parenting his spitfire of a daughter and he replied, "I can be President of the United States, or I can control Alice. I cannot possibly do both."

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to Hauke Fath on Fri Nov 19 18:38:29 2021
    In article <1piwoyj.v7briefpmumsN%dont.spam.usenet@googlemail.com>,
    Hauke Fath <hauke@Espresso.Rhein-Neckar.DE> wrote:
    Hauke Fath <dont.spam.usenet@googlemail.com> wrote:

    if ( ! ${?TERM} || { eval `if ( ${TERM} == "" ) exit 1` } ) ...

    Alternatively[1],

    if ( ! ${?TERM} ) set TERM = ""

    then continue to check for an empty string.

    I take it, you posted that w/o having read my most recent post on this
    thread?

    --
    Q: How much do dead batteries cost?

    A: Nothing. They are free of charge.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hauke Fath@21:1/5 to Hauke Fath on Fri Nov 19 19:35:51 2021
    Hauke Fath <dont.spam.usenet@googlemail.com> wrote:

    if ( ! ${?TERM} || { eval `if ( ${TERM} == "" ) exit 1` } ) ...

    Alternatively[1],

    if ( ! ${?TERM} ) set TERM = ""

    then continue to check for an empty string.

    So many ways to skin a cat...

    Cheerio,
    Hauke


    [1] <https://unix.stackexchange.com/questions/197497/how-to-check-if-string-is-blank-in-tcsh>

    --
    Now without signature.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hauke Fath@21:1/5 to Hauke Fath on Fri Nov 19 19:30:21 2021
    Hauke Fath <dont.spam.usenet@googlemail.com> wrote:

    The naive

    if ( ! ${?TERM} || ${TERM} == "" ) ...

    errors out in the second expression

    FTR, I just came across [1] whose

    if ( ! ${?TERM} || { eval `if ( ${TERM} == "" ) exit 1` } ) ...

    fits my bill.

    Cheerio,
    Hauke


    [1] <https://stackoverflow.com/questions/13343392/how-to-check-if-an-environment-variable-is-either-unset-or-set-to-the-empty-stri>


    --
    Now without signature.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hauke Fath@21:1/5 to Kenny McCormack on Fri Nov 19 21:41:05 2021
    Kenny McCormack <gazelle@shell.xmission.com> wrote:

    if ( ! ${?TERM} ) set TERM = ""

    then continue to check for an empty string.

    I take it, you posted that w/o having read my most recent post on this thread?

    Right - our postings crossed each other. :)

    USENET was not designed for chat mode...

    Cheerio,
    Hauke

    --
    Now without signature.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to Hauke Fath on Fri Nov 19 21:31:46 2021
    In article <1piwuuz.o88pa8a90bmmN%dont.spam.usenet@googlemail.com>,
    Hauke Fath <hauke@Espresso.Rhein-Neckar.DE> wrote:
    Kenny McCormack <gazelle@shell.xmission.com> wrote:

    if ( ! ${?TERM} ) set TERM = ""

    then continue to check for an empty string.

    I take it, you posted that w/o having read my most recent post on this
    thread?

    Right - our postings crossed each other. :)

    USENET was not designed for chat mode...

    Yup. So true.

    --
    Atheism:
    It's like being the only sober person in the car, and nobody will let you drive.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)