- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我要用 C 为我的项目编写一个简单的 shell,它可以实现环境变量。我查阅了如何使用 getenv、setenv、putenv。到目前为止一切顺利,我已经尝试使用 getenv 来显示 shell 变量……好吧……取得了一些成功。但我觉得我的推理是有缺陷的。我有一个 char** argv
,其中包含来自用户输入的解析输入。我现在检查 argv
是否以命令“echo”开头,然后检查以下任何输入是否以 $
符号开头。这是我的代码:
int executeVariables(char** arguments){
int i = 0;
if(strcmp(arguments[i], "echo") == 0){
char *variable;
for(i = 1; arguments[i] != NULL; i++){
char *str = arguments[i];
if( *(str + 0) == '$'){
variable = getenv(str + 1);
}else{
variable = getenv(str);
}
if(!variable){
//puts("not a variable");
printf("%s ", arguments[i]);
}else{
//puts("a variable");
printf("%s ", variable);
}
}
printf("\n");
exit(0);
}
return 1;
}
我认为普通的 linux shell 会找到 $
符号,它会在调用 echo 命令之前扩展变量。我的 shell 没有遵循这个原则,它在 echo 命令本身内部扩展变量。关于如何实现这个的任何想法?谢谢。
编辑:
我遇到的一个问题是:echo $HOME
和 echo HOME
给了我相同的错误结果。
编辑:
经过各种测试,一切正常。但要真正测试它,我需要创建一个局部变量,然后 echo
这个值。我尝试使用 putenv
函数,但它没有创建局部变量。
i = 0;
char** temp = malloc(sizeof (*temp));
if(strstr(userInput, "=") != NULL){
//puts("we got equals");
puts(userInput);
if(putenv(userInput) == 0){
printf("doing putenv(%s)\n", userInput);
exit(0);
}
else{
puts("couldnt putenv");
exit(1);
}
}
userInput:char *userInput
是使用 fgets()
从命令行获取的输入
最佳答案
您特别要求代码为字符串执行 getenv(),即使未找到 $。这就是它会查找 $HOME 或 HOME 的原因。只需删除找不到美元符号的 else 情况,并确保在声明时将变量初始化为 NULL,并将其放入循环中。
像这样:
// First, perform replacements
int executeVariables(char** arguments){
int i = 0;
for(i = 0; arguments[i] != NULL; i++){
char *str = arguments[i];
if(*str == '$'){
// make sure the result isn't NULL, though I'm not sure a real shell does
char *tmp = getenv(str + 1);
if (tmp != NULL) {
arguments[i] = getenv(str + 1); // save off the argument
}
}
}
// Then actually execute the function. This would be like bash's echo builtin
if (strcmp(arguments[0], "echo") == 0) {
int i;
for (i = 1; arguments[i] != NULL; i++) {
printf("%s ", arguments[i]);
}
printf("\n");
}
// Other functions could go here
return 1;
}
编辑:就您的方法而言,为什么要专门检查 echo ?为什么不让它通用,并检查所有参数,包括第一个参数?您实际上可能想要替换所有潜在的环境变量,因此如果您在某处设置了 MYECHO=echo,则以下内容将起作用。为了使它更通用,你需要这个函数,然后它会根据扩展变量执行这些东西。你可以把它做得很好,并有单独的功能,但为了适应这里的一切,我已经相应地更新了上面的代码,尽管我还没有测试它。 ;)
编辑:话虽这么说,werewindle 之前关于这样做的评论确实适用——替换参数,就像这里一样,然后使用更新后的参数数组让一个单独的函数做任何它需要做的事情做。 :)
> $MYVAR totally awesome $HOME
totally awesome /home/user
编辑:至于 putenv() 的情况,您需要如下所示的结构。这样,它将为 shell 以及您在 shell 中运行的任何其他进程设置它。
void do_args_env(char *args[])
{
// do putenv, etc.
}
// inside main loop in shell process
while (1) { // just an example
if (check_args_syntax(args) != 0) {
// error
}
do_args_env(args);
// fork and do other stuff
}
(希望是最终的)编辑:作为解释,进程通常不会(也许不能?)影响其层次结构中位于它们之上的进程的环境;只有相反。因此,如果您将 putenv() 放在一个 child 中,则该 child 的 sibling (即从其父进程派生的其他进程)将不会改变环境。
很高兴我能帮上忙!
关于c - 简单linux shell中的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8328442/
我相信我在子 shell 中调用 exit 会导致我的程序继续: #!/bin/bash grep str file | while read line do exit 0 done
我相信我在子 shell 中调用 exit 会导致我的程序继续: #!/bin/bash grep str file | while read line do exit 0 done
我有几个脚本,它们的第一部分看起来是一样的。这部分的功能是识别脚本在哪台机器上运行并相应地设置几个变量。它看起来像这样: ENV=`echo $LOGNAME | cut -c1-8` if
这是我正在尝试做的事情。我有 4 个 shell 脚本。脚本 1 需要先运行,然后是 2,然后是 3,然后是 4,并且它们必须按此顺序运行。脚本 1 需要运行(并在后台等待)2 才能正常运行,但是脚本
我有一个名为 a.sh 的脚本,其中的内容是: //a.sh: #!/bin/bash temp=0 while [ "$temp" -ne 500 ] do echo `date`
在snakemake中,使用shell()函数执行多个命令的推荐方式是什么? 最佳答案 您可以调用shell()多次内run规则块(规则可以指定 run: 而不是 shell: ): rule pro
我有一个 shell 脚本,我向其中传递了一些参数。Test1.sh -a 1 -b 2 -c“一二三” 在 Test1.sh 中,我按以下方式调用另一个 shell 脚本。Test2.sh $* 我
我有 2 个 shell 脚本。 第二个shell脚本包含以下函数第二个.sh func1 func2 first.sh 将使用一些参数调用第二个 shell 脚本, 将使用特定于该函数的一些其他参数
我有一个 Unix shell 脚本 test.sh。在脚本中,我想调用另一个 shell,然后从子 shell 执行 shell 脚本中的其余命令并退出 说清楚: test.sh #! /bin/b
我想在 shell 脚本中更改路径环境变量。路径变量需要在shell脚本执行后修改。 最佳答案 我知道有两种方法可以做到这一点。第一种是在当前 shell 的上下文中运行脚本: . myscript.
此 shell 脚本按预期运行。 trap 'echo exit' EXIT foo() { exit } echo begin foo echo end 这是输出。 $ sh foo.sh
我正在使用 vimshell在 vim 中执行命令 nnoremap vs :VimShellPop 使用此键映射,我可以打开 vim shell 并执行诸如“捆绑安装”之类的命令,然后 输入 exi
我想连接到不同的 shell(csh、ksh 等)并在每个切换的 shell 中执行命令。 下面是反射(reflect)我的意图的示例程序: #!/bin/bash echo $SHELL csh e
我目前正在尝试使用 BNF 和 LL 解析器在 C 中重新编写 shell。 否则,我需要知道 shell 运算符的优先级是什么| , > , > , & , ; ? 有没有人可以提供给我? 谢谢 最
不幸的是,我没有suspend 命令(busybox/ash)。但是我可以使用 kill -STOP $$ 从后台 shell (sh &) 返回到父 shell(以及 fg 之后)。 但是我不想输入
我需要知道,当用户切换到另一个 shell 时,通过单击它。 我试过 shellListener.shellDeactivated()但是当 shell 失去对它自己的控件的焦点时,会触发此事件,这意
file1.txt aaaa bbbb cccc dddd eeee file2.txt DDDD cccc aaaa 结果 bbbb eeee 如果能不区分大小写就更好了! 谢谢! 最佳答案 gre
我见过解压缩目录中所有 zip 文件的循环。但是,在运行此之前,我宁愿确保我将要运行的内容正常工作: for i in dir; do cd $i; unzip '*.zip'; rm -rf *.z
我对编程还很陌生,但我想知道 vim、emacs、nano 等 shell 文本编辑器如何能够控制命令行窗口。我主要是一名 Windows 程序员,所以可能在 *nix 上有所不同。据我所知,只能将文
我有一个包含第 7 列日期的文件,我的要求是将它与今天的日期进行比较,如果小于它,则删除该完整行。 此外,如果第 7 列中提到的任何日期超过 15 天,则将其修改为最多 15 天 下面的例子- now
我是一名优秀的程序员,十分优秀!