How to I get a corresponding shell command for the following batch command :
if %1 EQU %~1 (
echo ERROR ! The username must be within quotes.
)
Quotes in Bash are syntactic, not literal, so they are not seen at all in the script. AFAIK there is absolutely no way for a script to know whether or how the parameters were quoted, because any quotes are effectively gone by the time the script receives the parameters.
If you want to check whether the parameter contains whitespace or other special characters which would make it "amenable" to quoting in Bash, you can check whether the "Bash-quoted" string is equal to the original string:
[[ "$1" = "$(printf %q "$1")" ]]
If you want to check whether the parameter was literally quoted, you could do a simple check like
[[ "$1" =~ ^\".*\"|\'.*\'$ ]]
That said, why would you ever need this?
./test.sh -u <username> -b <path> -c <abc> If the user fails to specify a username, the script would incorrectly take the next option flag as the username. I could use getopts but I wanted it to be as consistent as possible with the corresponding batch script. Another possible solution would be to make sure that the user is not equal to any of the option flags, but that seemed like a more tedious approach.With bash, try this:
if [[ -z "$1" ]]; then
echo ERROR ! The username must be within quotes.
fi
$1=\"AString\", your test didn't work for me. It will accept any string as correct, and only an empty $1 as the error. Good luck to all!
%~1. Good luck.test.bat "username"%~1 expands %1 removing any surrounding quotes (")