Bash script in powershell script

Ivaylo Stefanov 136 Reputation points
2021-05-27T14:00:01.047+00:00

Hi,

i installed a Cygwin (https://www.cygwin.com/) in my windows server and now I can execute bash script in windows. I want to write script that implement bash script in my powershell script.
When I start this command in powershell
100266-%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5.png

I see that it's a few command that are not recognize from powershell - rm et.c. When i start the script in cygwin terminal, i can execute without problem. I send too the bash script:

!/bin/bash  
  
  
# define general stuff  
host=.  
certdir=/cygdrive/l/$host.  
certdir_local=L:/$host..  
outcert_cygwin=/cygdrive/c/certbot  
storepass=.  
  
  # rm old pfx file  
  rm $outcert_cygwin/letsencrypt.pfx  
  
  # convert the cert to some tomcat shizzle  
  openssl.exe pkcs12 -export -out $outcert_cygwin/letsencrypt.pfx -inkey $certdir/privkey.pem -in $certdir/cert.pem -certfile $certdir/chain.pem -password pass:$storepass  
  
  # change permission of the cert  
  chmod 644 $outcert_cygwin/letsencrypt.pfx  
  
  # additional info, for server.xml  
  echo  
  echo "Make sure the contents of c:\certbot\tomcat_server_config.txt are"  
  echo "added to the tomcats server.xml"  
  echo  
  echo "Exiting."  
  
  exit 0  
    
# else  
#   echo "Exiting."  
#   exit 0  
# fi  
  
# exit 0  
  

Can anybody help me how this command would be recognized from powershell? For example the command rm is available. See you screenshot:
100304-%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5.png

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,544 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,801 Reputation points
    2021-05-31T21:28:49.353+00:00

    Try this:

    Open Window's File Explorer. Find a file that uses the ".sh" extension. Right-click it and select "Open with". Select "sh.exe" and "Always use...".

    Assuming that your PATH variable points to when you have Cygwin installed you should be able to run your shell scripts without running bash.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Ian Xue 37,536 Reputation points Microsoft Vendor
    2021-05-28T01:31:57.467+00:00

    Hi,

    You could add C:\cygwin64\bin to the PATH environment variable.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Ivaylo Stefanov 136 Reputation points
    2021-05-28T07:28:45.393+00:00

    Hi IanXue,

    Unfortunately this not work for me. See picture:
    100502-image.png


  3. Rich Matheisen 46,801 Reputation points
    2021-06-04T01:27:00.053+00:00

    . . . One other thing: the 1st line of your shell script is "!/bin/bash". Shouldn't that be "#!/bin/bash"

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.