1

I need to convert script:

s#!/bin/bash

if [ $# -ne 1 ]; then
        echo "Usage:"
        echo ${0##*/}" <port>"
        echo
        exit 1
fi

r=`ss -ln6tp | grep -c ":::$1"`
 if [ $r -ne 1 ]; then
        echo "Agent on port $1 is not listening."
        exit_code=2
 else
        echo "Agent listening on port $1"
        exit_code=0
 fi

exit $exit_code

I don't have experience with bash at all and my powershell knowledge is basic. Exit code is the main problem for me here. Any ideas please?

vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • A line with just `$exit_code` will output the exit code. – AdminOfThings Feb 25 '19 at 21:23
  • Please edit the question and explain what the bash script is supposed to do. Are you running the script on Windows or some *nix? Powershell and Windows work pretty different a way from *nix, so re-implementation can be quite different. – vonPryz Feb 25 '19 at 21:25
  • 3
    `$exit_code = 0` assigns a value to the variable `$exit_code` in PowerShell. `exit $exit_code` is identical in PowerShell and bash. Now please go find a bash and a PowerShell tutorial. SO is neither a free translation service, nor a replacement for familiarizing yourself with the language(s) you're using. – Ansgar Wiechers Feb 25 '19 at 21:54
  • 1
    Im assuming that you're hoping to go from running this script on linux to running this script on windows, that is probably going to mean that the various tools this script is expecting (ss, grep) will not be installed. Could you perhaps tell us what you want the script to do? it would be easier to just write the functionality directly rather than try and translate it. – axwr Feb 25 '19 at 22:33
  • [Bash to Powershell script convert](https://social.technet.microsoft.com/Forums/en-US/4c58dfec-7c3e-4ffc-b4dc-9c032792a78f/bash-to-powershell-script-convert?forum=ITCG) – Olaf Feb 26 '19 at 00:00
  • try [Converts bash script to powershell script](https://github.com/nyabkun/bash-to-powershell-converter "nyabkun/bash-to-powershell-converter: Converts bash script to powershell script.") – Ooker Oct 18 '21 at 16:17

1 Answers1

1

The above SO questions should be helpful in solving your problem. They will help you understand what the bash script does as well as adopting it. The exit_code defines the return of your script in both languages. You will have a hard time to actually replace the actual functionality of the script if you want to entirely convert it into PowerShell using the same logic. You could likely replace it with a single line if you change the logic.

Seth
  • 1,215
  • 15
  • 35