View Issue Details

IDProjectCategoryView StatusLast Update
0002333OpenFOAMBugpublic2016-11-17 22:53
Reporteruser1352Assigned Tohenry  
PrioritylowSeveritytweakReproducibilityalways
Status resolvedResolutionfixed 
PlatformGNU/LinuxOSFedoraOS Version20
Fixed in Versiondev 
Summary0002333: Bad Option -t with zsh
DescriptionMore Info Here: http://www.cfd-online.com/Forums/openfoam-installation/179465-openfoam-4-0-bad-option-t-zsh.html

Basically the type command has not same arguments for zsh:
[ "$(type -t wmRefresh)" = "alias" ] && unalias wmRefresh || unset wmRefresh

I thought about looking for the world "alias" inside the output of the type command. It should work for both shells:

[[ "$(type wmRefresh)" =~ "alias" ]] && unalias wmRefresh || unset wmRefresh

since the output on zsh of type is "<command> is an alias for" while on bash "<command> is aliased to".

Don't know if it will somehow influence other platforms.
And I'm also aware you're not supporting zsh, but I thought that while possible switching to a compatible strategy would be useful.
Steps To Reproduce- Change your shell to zsh
- Source $FOAM_INSTALL_DIR/etc/bashrc
TagsNo tags attached.

Activities

henry

2016-11-15 16:18

manager   ~0007209

Only bash and tcsh are supported with OpenFOAM releases. However the OpenFOAM-??/etc directory is now well-structured and it would be possible to create zsh, fish, eshell etc. versions of the configuration files and install them there. I know configuration files for fish were created a few years ago but not maintained anymore. If you can create and maintain configuration files for zsh and release them publicly I am sure other zsh enthusiasts will benefit.

henry

2016-11-15 16:26

manager   ~0007210

Last edited: 2016-11-15 16:27

In principle I do not have a problem with the change you provide but given it is not the way it would be done in bash it would need an appropriate comment. However, when I tried it in zsh I get the error

.../OpenFOAM-dev/etc/config.sh/aliases:unalias:73: no such hash table element: wmRefresh

henry

2016-11-15 21:59

manager   ~0007217

Your proposed change also causes problems for bash:

-bash: type: wmRefresh: not found

wyldckat

2016-11-15 23:01

updater  

bug2333.patch (543 bytes)   
diff --git a/etc/config.sh/aliases b/etc/config.sh/aliases
index 6079901..d5475f9 100644
--- a/etc/config.sh/aliases
+++ b/etc/config.sh/aliases
@@ -70,7 +70,9 @@ alias run='cd $FOAM_RUN'
 # Refresh the environment
 # ~~~~~~~~~~~~~~~~~~~~~~~
 # For backward-compatibility unalias wmRefresh if it is defined as an alias
-[ "$(type -t wmRefresh)" = "alias" ] && unalias wmRefresh || unset wmRefresh
+type wmRefresh 2>/dev/null | grep -q " alias" && \
+    unalias wmRefresh || unset wmRefresh
+
 wmRefresh()
 {
     wmProjectDir=$WM_PROJECT_DIR
bug2333.patch (543 bytes)   

wyldckat

2016-11-15 23:01

updater  

aliases (3,292 bytes)   
#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# File
#     etc/config.sh/aliases
#
# Description
#     Aliases for working with OpenFOAM
#     Sourced from OpenFOAM-<VERSION>/etc/bashrc and/or ~/.bashrc
#
#------------------------------------------------------------------------------

# Change compiled version aliases
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alias wmSet='. $WM_PROJECT_DIR/etc/bashrc'
alias wm64='wmSet WM_ARCH_OPTION=64'
alias wm32='wmSet WM_ARCH_OPTION=32'
alias wmSP='wmSet WM_PRECISION_OPTION=SP'
alias wmDP='wmSet WM_PRECISION_OPTION=DP'

# Clear env
alias wmUnset='. $WM_PROJECT_DIR/etc/config.sh/unset'

# Toggle wmakeScheduler on/off
#  - also need to set WM_HOSTS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alias wmSchedOn='export WM_SCHEDULER=$WM_PROJECT_DIR/wmake/wmakeScheduler'
alias wmSchedOff='unset WM_SCHEDULER'

# Change directory aliases
# ~~~~~~~~~~~~~~~~~~~~~~~~
alias foam='cd $WM_PROJECT_DIR'

if [ -n "$WM_PROJECT_SITE" ]
then
    alias foamSite='cd $WM_PROJECT_SITE'
else
    alias foamSite='cd $WM_PROJECT_INST_DIR/site'
fi

alias src='cd $FOAM_SRC'
alias lib='cd $FOAM_LIBBIN'
alias app='cd $FOAM_APP'
alias sol='cd $FOAM_SOLVERS'
alias util='cd $FOAM_UTILITIES'
alias tut='cd $FOAM_TUTORIALS'
alias run='cd $FOAM_RUN'


# Refresh the environment
# ~~~~~~~~~~~~~~~~~~~~~~~
# For backward-compatibility unalias wmRefresh if it is defined as an alias
type wmRefresh 2>/dev/null | grep -q " alias" && \
    unalias wmRefresh || unset wmRefresh

wmRefresh()
{
    wmProjectDir=$WM_PROJECT_DIR
    foamSettings=$FOAM_SETTINGS
    wmUnset
    . $wmProjectDir/etc/bashrc $foamSettings
}


# Change OpenFOAM version
# ~~~~~~~~~~~~~~~~~~~~~~~
unset foamVersion
foamVersion()
{
    if [ "$1" ]; then
        foamInstDir=$FOAM_INST_DIR
        wmUnset
        . $foamInstDir/OpenFOAM-$1/etc/bashrc
        foam
        echo "Changed to OpenFOAM-$1" 1>&2
    else
        echo "OpenFOAM-$WM_PROJECT_VERSION" 1>&2
    fi
}


# Change ParaView version
# ~~~~~~~~~~~~~~~~~~~~~~~
unset foamPV
foamPV()
{
    . $WM_PROJECT_DIR/etc/config.sh/paraview ParaView_VERSION=$1
    echo "paraview-$ParaView_VERSION  (major: $ParaView_MAJOR)" 1>&2
}


#------------------------------------------------------------------------------
aliases (3,292 bytes)   

wyldckat

2016-11-15 23:15

updater   ~0007218

I've attached two files:

  - "bug2333.patch", which demonstrates a general purpose alternative, although it will possibly fail horribly some other way, e.g. if the command line language is something other than English. I haven't managed to test it yet.

  - "aliases" for "etc/config.sh/aliases" is the patch already applied, for OpenFOAM-dev.


Essentially it relies on using grep to look for a space followed by the word "alias", while also hiding any error messages that may come from "type". This is because when it's of type function, it will tell us the path to the origin of the function in some cases, e.g. on "zsh":

   wmRefresh is a shell function from /home/ofuser/OpenFOAM/OpenFOAM-dev/etc/config.sh/aliases


This seemed to work with "bash", "zsh" and "dash", because they emitted messages like this (used OpenFOAM 3.0.x to help me out):

  - bash:

     wmREFRESH is aliased to `wmSET $FOAM_SETTINGS'

  - zsh:

     wmREFRESH is an alias for wmSET $FOAM_SETTINGS

  - dash:

     wmREFRESH is an alias for wmSET $FOAM_SETTINGS

And yes, "dash" somehow gave the same message as "zsh", I triple checked it... although "dash" is only interesting to use for special high-speed shell script execution, which is relatively rare nowadays.


By the way, "unset" will not work with functions on "dash", it simply doesn't unset nor does it complain. Therefore it's not to be expected that dash will work at 100%, but at least it will work 99.9% of the cases.

henry

2016-11-16 09:24

manager   ~0007219

How about using 'declare' instead of 'test' and 'grep':
 
declare -f wmRefresh > /dev/null; echo $?

This returns 0 if wmRefresh is a defined function otherwise 1 and works in bash and zsh.

henry

2016-11-16 09:27

manager   ~0007220

'declare' is not available in 'dash' but is anyone using 'dash' for interactive use?

user1352

2016-11-16 15:18

  ~0007221

Ok, I'm sorry. On bash I had a module auto-loading OpenFOAM so wmRefresh was always available (as a function), not throwing errors...

This works for me on zsh 5.2 (OpenFOAM 4.x and OpenFOAM-dev) and GNU bash 3.1.17 (OpenFOAM-4.x):

[[ "$(type wmRefresh 2>/dev/null)" =~ "alias" ]] && unalias wmRefresh || unset wmRefresh

With "it works" I mean that sourcing etc/bashrc when OpenFOAM is not loaded does not throw errors and wmRefresh is available in the namespace of both shells as a function.

# zsh
$type wmRefresh
wmRefresh is a shell function

# bash (after sourcing)
$ type wmRefresh
wmRefresh is a function
wmRefresh ()
{
    wmProjectDir=$WM_PROJECT_DIR;
    foamSettings=$FOAM_SETTINGS;
    . $WM_PROJECT_DIR/etc/config.sh/unset;
    . $wmProjectDir/etc/bashrc $foamSettings
}

###
.../OpenFOAM-dev/etc/config.sh/aliases:unalias:73: no such hash table element: wmRefresh

If you run "unalias" on wmRefresh that is a shell function on zsh it throws the error henry reported (since it's not an alias). But it should not run unalias, but unset instead. In facts I get no errors. Pretty strange.

Thanks to wyldckat for the more deep investigation. I'm not expert of dash so I cannot contribute more on this frame.

wyldckat

2016-11-16 21:22

updater   ~0007222

@Henry:
> 'declare' is not available in 'dash' but is anyone using 'dash' for interactive use?

I haven't used 'dash' interactively, only through scripts with "/bin/sh" on Ubuntu that would build OpenFOAM for multiple MPI toolboxes.
Therefore, if used in scripts only, then defining "declare" as a shell function would be pretty simple:

  declare() { exit 0; }

before sourcing "etc/bashrc", therefore muting any error messages on 'dash'.


And I do prefer using 'declare' instead of 'grep': it doesn't require any string parsing nor bracketed condition checking, therefore it has the plus side of it being the optimal solution!


@TiD91: I don't know what it is specifically, but Bash's double-square brackets gets a bit on my nerves... it was probably due to a traumatic experience with Mathematica... or because it broke a lot of scripts for me on Ubuntu's 'dash'. So I'd rather stick with using 'declare' and be able to easily circumvent it with an user-side auxiliary function.

henry

2016-11-16 22:37

manager   ~0007223

This should resolve the issue:
OpenFOAM-dev: commit cbad2dac5e12e39facff8c63a54e7748d8b78332

henry

2016-11-17 22:53

manager   ~0007235

Resolved in OpenFOAM-dev by commit cbad2dac5e12e39facff8c63a54e7748d8b78332

Issue History

Date Modified Username Field Change
2016-11-15 16:07 user1352 New Issue
2016-11-15 16:18 henry Assigned To => henry
2016-11-15 16:18 henry Status new => closed
2016-11-15 16:18 henry Resolution open => fixed
2016-11-15 16:18 henry Note Added: 0007209
2016-11-15 16:26 henry Status closed => feedback
2016-11-15 16:26 henry Resolution fixed => reopened
2016-11-15 16:26 henry Note Added: 0007210
2016-11-15 16:27 henry Note Edited: 0007210
2016-11-15 21:59 henry Note Added: 0007217
2016-11-15 23:01 wyldckat File Added: bug2333.patch
2016-11-15 23:01 wyldckat File Added: aliases
2016-11-15 23:15 wyldckat Note Added: 0007218
2016-11-16 09:24 henry Note Added: 0007219
2016-11-16 09:27 henry Note Added: 0007220
2016-11-16 15:18 user1352 Note Added: 0007221
2016-11-16 15:18 user1352 Status feedback => assigned
2016-11-16 21:22 wyldckat Note Added: 0007222
2016-11-16 22:37 henry Note Added: 0007223
2016-11-17 22:53 henry Status assigned => resolved
2016-11-17 22:53 henry Resolution reopened => fixed
2016-11-17 22:53 henry Fixed in Version => dev
2016-11-17 22:53 henry Note Added: 0007235