I am new to Stack Overflow and bash/sh commands. I ran into the following issue when trying to execute a shell command in 2 different ways:
The script is as follows:
For /R C:\\Users\\userid\\Desktop\\my-test\\src\\api-explorer\\ %G IN (*.json) do widdershins "%G" -o "%G".md
The intent of the script is to recursively convert a number of Swagger.json files to Markdown files using a conversion tool called Widdershins.
The script runs fine when executing it from Python like this:
def convertSwaggerToMarkdown():
cmd = 'For /R C:\\Users\\userid\\Desktop\\my-test\\src\\api-explorer\\ %G IN (*.json) do widdershins "%G" -o "%G".md
subprocess.run(cmd, shell=True)
Where it fails, is when I try to execute the script in Bash directly. I've tried the recommendations from other users that encountered a similar error, which suggest appending either the #!/bin/bash
or #!/bin/sh
to the beginning of the command, but when doing this the command does not execute and also does not provide any error.
I also tried suggestions to add " " around the (*json), since this appears to be where the issue resides. Since the script executes in Python when shell=True, I'm certain there is a syntax error which I am overlooking, and also a better needed understanding of how the logic between bash and sh scripts work.
In Bash, this is what it looks like:
What am I missing here?
That script is not bash. My guess is it is PowerShell, a scripting language used in Windows that fills a similar role as Bash. The correct Bash syntax would be
for g in *.json; do
widdershins $g -o $g.md
done
If you want the command to be recursive, best option is to use the find
command, eg
find . -name \*.json -type f -exec widdershins \{\} -o \{\}.md \;
Or as an alternative, launch PowerShell to run your script instead of Bash. PowerShell is actually cross platform these days.
Your original command should work happily if executed directly from the cmd
prompt - although the double- \
are unnecessary.
If you are executing that command as a line in a *.bat
file, then each %G
needs to have the %
doubled %%G
as G
here is a metavariable. Again, the double- \
are unnecessary.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.