Carrying on from my previous post about using scripts created in Windows on Linux, here are some comparisons to show the syntax differences between Windows batch scripts and bash scripts.
(%1 in batch and $1 in bash refer to the first argument passed in when running the script. E.g. running myscript.sh ./mydir, then in the script $1 would have the value of ./mydir.)
Setting a variable to a number and then printing the variable
(GEQ and -ge is the greater than or equal to comparison operator, both bash and windows batch scripts will let you use a range of comparison operators.)
# do something that increases the value of $somevar
done
(If you’re wondering why I used !somevar! instead of %somevar% in the windows script, this is because in a while loop you will be wanting to update the variable. See this article for more info: Windows batch scripting: EnableDelayedExpansion.)
Check last command executed OK, if not print error message and exit
What batch & power shell statements / control structures are you having trouble finding the shell equivalents for? And are you converting to bourne shell or bash?
Good description and nice explanation.
I’m a beginner with shell and batch scripting, and this is an awesome resource!
Concise and relevant. Thanks so much.
Thanks for the writing!
I think you missed an “/F” option in this command:
FOR %%x IN (‘command to be executed’) DO SET result=%%x
Hi B
Thanks for the correction, I’ve updated that line now.
Cheers!
Dave
i have some batch and power shell scripts which i need to convert to shell scripts. plz help…….
Hi archanish
What batch & power shell statements / control structures are you having trouble finding the shell equivalents for? And are you converting to bourne shell or bash?
Dave