usually this:
Code:
my_args_in_quote=`echo ${my_args:$my_args_quote_pos}`
is deprecated, not suggested at all.
First, you need to use ", next `echo something` is usually equal to something without echo and ` (backtick). Also backtick makes some tricks on the command in between backticks, so you need to take extra care.
[[ ]] is preferred (instead of [ ] )
So your code should look like this:
Code:
my_args_before_quote="${my_args:0:$((my_args_quote_pos-1))}" #store args before single quote char
my_args_in_quote="${my_args:$my_args_quote_pos}" #store data from first quote to the end of the string
my_args_quote_pos=$(expr index "$my_args_in_quote" "'") #find second single quote char pos
if [[ $my_args_quote_pos -gt 0 ]]; then #if founded then split rest of string after quote char
my_args_in_quote_length=${#my_args_in_quote} #length of string which contains second quote and after it <<< do not need expr length here
if [[ $my_args_in_quote_length -gt $my_args_quote_pos ]]; then #if there is something after quote char
my_args_after_quote="${my_args_in_quote:$my_args_quote_pos:$my_args_in_quote_length}" #store substring from second quote char to the end of the string
fi
my_args_in_quote="${my_args_in_quote:0:$((my_args_quote_pos-1))}" #store substring in single quotes
fi
I would say this will not work the same way as before, so you need to check it
looks like you want to modify the original behaviour (of bash), so just go back to the original question:
Code:
#!/bin/sh
my_args="1 2 3 '4 5' 6"
echo "Count of arguments when using my_args:"
./arguments_count.sh $my_args # result was 6
echo "Count of arguments when using direct string:"
./arguments_count.sh 1 2 3 '4 5' 6 # result was 5
You need to understand how the entered string was evaluated. In the second case it is quite simple, you will have 5 arguments
1
2
3
4 5 << here ' ' will protect the space between 4 and 5 to use as separator
6
in the first case you will have
1
2
3
'4
5'
6
because " will force the interpreter to use ' as is, loosing its special meaning (as it was used in the first case).
Also you may try:
./arguments_count.sh "$my_args"
which will return 1, because the whole string will be used "in one", as a single argument.
Every time you start a command this argument parsing will occur and that's why embedding argument lists into each other is not really trivial (backtick will also do something like this) - although not impossible. You need to keep embedded commands+its arguments in one as long as it is just passed to another command.
The usual way is to protect " against evaluation by escaping it (using \ and/or ').
http://wiki.bash-hackers.org/syntax/quoting
http://stackoverflow.com/questions/9...ring-quotation
http://www.tldp.org/LDP/abs/html/quotingvar.html
http://stackoverflow.com/questions/1...quoted-strings
(and obviously you can find a lot of other examples)
I hope this helps a bit