How to accept a quoted sentence as argument in shell script [duplicate]


How to accept a quoted sentence as argument in shell script [duplicate]



This question already has an answer here:



I am trying to accept a quoted sentence as a parameter in bash similar to:



git commit -m "your message as a sentence"


git commit -m "your message as a sentence"



I am trying to get this done like this:


if [[ "$2" =~ "-m" ]]; then
if [ -z $3 ]; then
echo "Must have a message"
else
# TODO accept message and check if it is inside quotes
fi
fi



Any ideas?



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Something including getopts 'm:' seems like a good start.
– Benjamin W.
Jun 30 at 6:07



getopts 'm:'





The quotes aren't special, they just make that the argument to the -m option is not split.
– Benjamin W.
Jun 30 at 6:08


-m





-m is just the argument before where I need to parse the complete sentence as an argument
– Carlos Abraham
Jun 30 at 6:10


-m





The problem is the quotes will be handled and removed by the shell prior to you receiving the positional parameters in your script. $3 will contain the multi-word string (including the whitespace), but it will not include quotes. Your script as it sits is not wrong. (aside from quoting) If $3 is filled after the $2 test succeeds, you can use your $3. Make sure You quote "$3" where required. (like in [ -z "$3" ])... (In fact you have your quoting requirements backwards. They are not required in [[ ... ]] but are required with [ ... ])
– David C. Rankin
Jun 30 at 6:20



$3


$3


$2


$3


"$3"


[ -z "$3" ]


[[ ... ]]


[ ... ]




1 Answer
1



Continuing from the comment, what happens on the command line regarding the arguments to your script is handled by your shell. So when you provide command line arguments the normal shell expansions and word-splitting apply. When you quote arguments to your script, your shell will properly avoid word-splitting within quotes, but the actual quotation marks themselves will be removed by your shell.



Within your script, your positional parameters will properly contain the results of your shell's handling of the arguments. So in your case, your script (or git in your example) will receive,


git


$1 : commit
$2 : -m
$3 : your message as a sentence



Your script logic is fine, the problem your are running into in your script is improper quoting. Specifically [[ ... ]] does not require quoting, but [ ... ] does.


[[ ... ]]


[ ... ]



Since you do not quote $3 in [ -z $3 ] you effectively are asking:


$3


[ -z $3 ]


[ -z your message as a sentence ]



which the shell will take as too many arguments. So to remedy the problem, quote when using test or [ ... ] (which are synonymous), e.g.


test


[ ... ]


[ -z "$3" ]



Looks things over and let me know if you have further questions.





Thank you, definitely I needed it to quote "$3"
– Carlos Abraham
Jun 30 at 6:38


"$3"





Sure, it happens to everybody when they start learning scripting. You can never go wrong if you always quote your variables (you can learn the exceptions to that rule as you progress along) Good luck with your scripting.
– David C. Rankin
Jun 30 at 6:39





@CarlosAbraham I recommend running your scripts through shellcheck.net -- shell syntax has a lot of gotchas like this, and shellcheck is good at pointing out the common ones.
– Gordon Davisson
Jun 30 at 8:02

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV