1

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.
    )
2
  • please clarify your question with an exact sample of calling your quote_Verify script. Unix shell people don't typically know all of the 'options' that bat cmds provide, especially %~1. Good luck. Commented May 9, 2012 at 14:22
  • I am calling the batch script as follows: test.bat "username" %~1 expands %1 removing any surrounding quotes (") Commented May 9, 2012 at 17:06

2 Answers 2

3

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?

Sign up to request clarification or add additional context in comments.

1 Comment

One use case is to parse the options from shell script command line. Eg. ./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.
1

With bash, try this:

if [[ -z "$1" ]]; then
    echo ERROR ! The username must be within quotes.
fi

1 Comment

it the O.P. meant that $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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.