ash variable substitution, quote slash [may be better solution]

Harald Becker ralda at gmx.de
Fri Sep 3 21:39:15 UTC 2010


 Ding! ... hurrying back to keyboard ... try this! (don't have the time
to test it all)

var="ab/cd"
echo ${var/\\//xxx}
--> abxxxcd
echo "${var/\//xxx}"    (bash compatible!)
-->abxxxcd

allows to replace ? and *

var="ab*cd"
echo "${var/\*/xxx}"
-->abxxxcd

This fixes at least three problems I had in serveral startup scripts.


static char *
parse_sub_pattern(char *arg, int inquotes)
{
        char *idx, *repl = NULL;
        unsigned char c;

        idx = arg;
        while (1) {
                if (!(c = *arg++))
                        break;
                if (c == '\\') {
                        *idx++ = c;
                        if ((c = *arg++) == '\\' && !inquotes)
                                c = *arg++;
                        if (!c)
                                break;
                } else if (c == '/') {
                        /* Only the first '/' seen is our separator */
                        if (!repl) {
                                repl = idx + 1;
                                c = '\0';
                        }
                }
                *idx++ = c;
        }
        *idx = c; /* NUL */

        return repl;
}



More information about the busybox mailing list