Menu

mc-wrapper.sh

pjc42
2018-07-23
2025-04-11
  • pjc42

    pjc42 - 2018-07-23

    hi, I'd like to write a wrapper script of some kind similar to mc-wrapper: ( mc/contrib/mc-wrapper.sh.in ) so that when I exit mc it exits in current dir of mc not dir mc started in. can you suggest env variables or tmp files you are using to that I can pick up the mc current dir to do this?

    thanks

     
  • pjc42

    pjc42 - 2018-07-23

    I have dug into this a little more and it is relatively straightforward to add myself as a powershell or batch script but relies critically on the "-P --printpwd" commandline option when launching mc. Is this option implemented in windows version? If it is then I couldn't get it work; seemed to be ignored.


    -P file, --printwd=file

    Print the last working directory to the specified file.  This option
    is not meant to be used directly.  Instead, it's used from a special
    shell script that automatically changes the current directory of the
    shell to the last directory Midnight Commander was in. Source the file
    /usr/libexec/mc/mc.sh (bash and zsh users) or /usr/libexec/mc.csh
    (tcsh users) respectively to define mc as an alias to the appropriate
    shell script.
    
     

    Last edit: pjc42 2018-07-23
  • pjc42

    pjc42 - 2018-07-25

    this was user error. mc will not overwrite --printwd=file so file must not exist.

    here is a simple powershell equivalent of the mc_wrapper.sh scripts:

    # mc-wrapper.ps1
    #  powershell version of mc_wrapper.sh
    # create alias for mc.exe as:
    #  set-alias mc mc-wrapper.ps1
    # mc-wrapper.ps1 assumes:
    #  - it is on the path
    #  - it is in the same dir as mc.exe
    
    $ErrorActionPreference = 'Stop'
    $mc_exe_dir =  "${PSScriptRoot}"
    $mc_exe_path = "${mc_exe_dir}/mc.exe"
    
    # get a random tmp file for mc --printwd 
    # mc stores exit path in this file 
    # make sure that it doesn't exist, mc will not overwrite
    $mc_printwd_path = [System.IO.Path]::GetTempFileName()
    Remove-Item "${mc_printwd_path}"
    
    # run mc with --printwd and pass any additional cmdline args 
    & "${mc_exe_path}" "--printwd=${mc_printwd_path}" "${args}"
    
    # mc has exited, get mc_exitpath and cd 
    try {
      $mc_exitpath = get-content -Path "${mc_printwd_path}"
      cd "${mc_exitpath}"
      remove-item -Force -Path "${mc_printwd_path}"
    } catch {
      # catch everything prevents unecessary error msgs
      # some $args result in mc_printwd_path not being created 
      # e.g. -V, --help
    } 
    
     

    Last edit: pjc42 2018-07-25
    • sNop

      sNop - 2021-04-02

      here is a simple powershell equivalent of the mc_wrapper.sh scripts

      I want to thank you for this 🙏♥🔥🚀 you saved me 2 hours, if I had to write this script myself, thx @pjc42

       

      Last edit: sNop 2021-04-02
  • adamy

    adamy - 2025-04-11

    Bundled with build-232

     
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.