Ich bin eine benutzerdefinierte Shell und Echo Arbeiten mit meinen Shell-Variablen zu schreiben:
'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin.
$ str=FOO
$ echo $str
19163: executing echo
FOO
19163: executed
$ str=BAR
$ echo $str
19170: executing echo
BAR
19170: executed
$
Die Implementierung von Shell-Variablen wie folgt aussieht:
static bool expand_parameter(char *shellcommand, hashtable_t *hashtable) {
char mystring[CMD_LEN];
char *cp;
char *ep;
strcpy(mystring, shellcommand);
cp = strstr(mystring, "$");
int position = cp - mystring;
int quote = isBetweenQuotes(position, mystring);
if (cp) {
*cp = '\0';
strcpy(shellcommand, mystring);
ep = ++cp;
while (*ep && (*ep != ' ')) {
ep++;
}
if (!quote)
strcat(shellcommand, ht_get(hashtable, cp));
else {
strcat(shellcommand, "$");
strcat(shellcommand, cp);
strcpy(mystring, shellcommand);
return false;
}
}
strcpy(mystring, shellcommand);
return true;
}
Wie soll Ich mache die Shell-Print echo $0
, die den Shell-Namen widerhallen sollte? Sollte ich es nur fest in meine Shell-Variable-Funktion einprogrammieren oder gibt es einen Best-Practice-Weg?
Der Zweck des Codes besteht darin, Shell-Variablen zu aktivieren. Das Projekt ist https://github.com/montao/openshell
Wie stellen Sie sicher, dass all jene 'strcat'-s, die die Erweiterung des Parameters bewirken, nicht das Array 'mystring' überlaufen lassen? Warum "strumpst" du irgendetwas in "mystring" als die letzte Aktion vor einem 'return', wenn das eine lokale Variable ist, die sofort verschwindet? – Kaz