Computer Hope

Software => Computer programming => Topic started by: Santosh on May 05, 2022, 03:31:38 AM

Title: Convert shell script to python
Post by: Santosh on May 05, 2022, 03:31:38 AM
I have written a shell script and I need the same script in python to execute it for windows.

Code: [Select]
#!/usr/bin/env bash

sonar_token=$1
sonar_server=$2
sonar_project_key=$3


echo "Fetching the Sonar Quality gate status using api"
quality_gatesstatus=$(curl -u $sonar_token: https://$sonar_server/api/qualitygates/project_status?pullRequest=$PullRequestId | grep -Po '"status": *\K"[^"]*"')

echo "the Sonar quality gates for current analysis is: $quality_gatesstatus"
if ("$quality_gatesstatus" != "OK") && ("$quality_gatesstatus" != "NONE")
then
   echo "Check Sonar server and fix the issues"
   echo $quality_gatesstatus
   exit 1
else
   echo "No code violations found"
   exit 0
fi

I am trying but i am failing to do that, please help us on that.
Title: Re: Convert shell script to python
Post by: Geek-9pm on May 06, 2022, 05:00:43 PM
The problem is you are calling is a shell script.  You to tell use it is bash and what version  of bash.
Here is a possible answer:
https://stackoverflow.com/questions/2839810/converting-a-bash-script-to-python-small-script

Sorry I can not do it. I am getting too old.  :'(
Title: Re: Convert shell script to python
Post by: satineeraj on April 07, 2023, 12:28:18 AM
Converting a shell script to Python involves translating the commands and logic from shell syntax to Python syntax.

Shell Script:

#!/bin/sh

echo "Enter your name:"
read name
echo "Hello, $name"

Python Script:

#!/usr/bin/env python

name = input("Enter your name: ")
print("Hello, " + name)

For more reference check this StackOverflow post: https://stackoverflow.com/questions/49982459/how-to-convert-shell-script-into-python-script