[英]How to echo $0 with a custom shell
我正在編寫一個自定義外殼,而echo可與我的外殼變量一起使用:
'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
$
Shell變量的實現如下所示:
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;
}
我應該如何使shell打印echo $0
應該回顯shell名稱? 我應該將其硬編碼到我的shell變量函數中,還是有最佳實踐呢?
該代碼的目的是啟用外殼程序變量。 該項目是https://github.com/montao/openshell
Shell參數(例如$0
, $1
...)作為數組char **argv
作為第二個參數傳遞到main()
函數而接收到您的shell。 如果你不想碰你太多的代碼,你可以將這些變量作為密鑰復制0
, 1
...哈希表呼喚你之前expand_parameter
功能,它應該工作。 但是總有一天,您將不得不執行類似shift
命令,也許您將不得不以不同的方式來管理它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.