首页 文章

在bash脚本中从当前终端运行命令

提问于
浏览
1

我想在我当前的shell(我运行我的bash脚本)中发出一个命令 . 因为我现在将uecap命令放在我的bash脚本中是正确的 . 脚本失败,找不到原因uecap命令 .

虽然如果我直接从我当前的shell发出uecap命令,它工作正常 .

我当前的shell是一个单独的进程,它是自己的进程ID .

这是我的bash shell作为一个例子:

#!/bin/sh
 while read i
 do 
 cid=`echo "$i"  | cut -b1`
 rid=`echo "$i"  | cut -b9-18`
 rm -rf $1_TEMP
 uecap -r rid -c cid
 done < $1ID.log

我运行bash脚本的方法是发出以下命令:

!./bashscript $node.

同样没有,有没有办法使用bash脚本中的另一个进程运行命令?

1 回答

  • 1

    可能你've just answered your own question. Since uecap is an alias, then it'没有导出到你的交互式shell发出的进程,你可以't force them shell to export it, for aliases there'没有 export 命令 .

    你可以做的是创建一个包含该别名定义的文件,然后在脚本中创建一个源文件,如下所示:

    alias uecap > ~/tmp/uecap_alias
    

    并在shell脚本中

    source ~/tmp/uecap_alias
    ...
    uecap -r rid -c cid
    

    您也可以尝试通过环境变量传递uecap别名定义,例如

    UECAP_ALIAS="$(alias uecap)" ./your_shell_script
    

    并在脚本中

    eval "$UECAP_ALIAS"
    ...
    uecap ...
    

    但请注意 eval "$VAR" 在空格,引用和奇怪的符号上非常脆弱,所以它应该特别小心使用,并且只有当你 eval (想想SQL注入等)时才能使用它 .

相关问题