MESAHub/mesa

shmesa GNU sed not compatible with macOS default BSD sed

gautam-404 opened this issue · 3 comments

Describe the bug
All shmesa functions that use the sed utility do not work on macOS. Mac default is the FreeBSD sed which functions slightly differently. Functions affected by this are shmesa_extras, shmesa_defaults and shmesa_change.

Since perl is available on most systems, it can potentially replace the use of sed.

To Reproduce
Steps to reproduce the behavior:

  1. On MacOS try the shmesa extras command
  2. The error:
sed: illegal option -- z
usage: sed script [-Ealnru] [-i extension] [file ...]
        sed [-Ealnu] [-i extension] [-e script] ... [-f script_file] ... [file ...]

A trial implementation using perl that works on macOS:

shmesa_extras () {
    if shmesa_check_h_flag "$@"; then
        echo "Usage: shmesa extras [src/run_star_extras.f90]"
        echo "replaces run_star_extras.f90 with the full template"
        return 0
    fi

    local rse='src/run_star_extras.f90'
    if [[ ! -z $1 ]]; then
        rse=$1
    fi

    local contents=$(cat "$MESA_DIR/include/standard_run_star_extras.inc")
    if [[ -z $contents ]]; then
        echo "Cannot find $MESA_DIR/include/standard_run_star_extras.inc"
        return 1
    fi
    contents=$(perl -pe 's/:/\\:/g; s/\n/\\n/g' "$MESA_DIR/include/standard_run_star_extras.inc")

    backup_copy "$rse" "$rse".bak
    perl -i -pe "s|      include 'standard_run_star_extras.inc'|$contents|g" "$rse"
}
shmesa_change () {
    if shmesa_check_h_flag "$@"; then
        echo "Usage: shmesa change inlist parameter value [parameter value [parameter value]]"
        echo "Modifies one or more parameters in the supplied inlist."
        echo "Uncomments the parameter if it's commented out."
        echo "Creates a backup of the inlist in the corresponding .bak file."
        echo ""
        echo "Examples:"
        echo "  shmesa change inlist_project initial_mass 1.3"
        echo "  shmesa change inlist_project log_directory 'LOGS_MS'"
        echo "  shmesa change inlist_project do_element_diffusion .true."
        echo "  shmesa change inlist_project initial_mass 1.3 do_element_diffusion .true."
        return 0
    fi
    
    if [[ -z $1 || -z $2 || -z $3 ]]; then
        echo "Error: Missing arguments."
        return 1
    fi
    
    local filename=$1
    shift
    
    if [[ ! -f "$filename" ]]; then
        echo "Error: '$filename' does not exist or is not a file."
        exit 1
    fi
    
    # Create a backup of the inlist before making any changes
    backup_copy "$filename" "${filename}.bak"

    while [[ -n $1 && -n $2 ]]; do
        local param=$1
        local newval=$2
        shift 2
        perl -i -pe "s/^\s*(#\s*)?($param)\s*=.+/    $param = $newval/g" "$filename"
    done
}

(Have not tested this on Linux yet. And I could not get shmesa_defaults function to work with Perl)

Thanks for the bug report! @JMombarg also recently spotted that this has troubles on a mac. Unfortunately I don't have access to a mac so it's difficult for me to debug. If you or someone else with access to a mac can help resolve this I will be very grateful!

It seems conceivable to me that this could be fixed with a change to the flags of sed, e.g. as given here:
https://unix.stackexchange.com/questions/13711/differences-between-sed-on-mac-osx-and-other-standard-sed

Your suggestion of using perl over sed also seems quite good as I think perl should be available on most devices.

having the same issue on my mac pro running 14.5 sonoma!

This is fixed in #663 as authored by gautam-404. You can get the patched version by replacing $MESA_DIR/scripts/shmesa/shmesa with the contents of this file: https://github.com/MESAHub/mesa/blob/main/scripts/shmesa/shmesa. It will also be in the next release.