Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在shell脚本中切换用户后无法调用Unix函数_Linux_Unix - Fatal编程技术网

Linux 在shell脚本中切换用户后无法调用Unix函数

Linux 在shell脚本中切换用户后无法调用Unix函数,linux,unix,Linux,Unix,下面的脚本与根用户一起执行。切换后用户Unix函数显示错误 test.sh #/bin/bash fn_测试() { echo“这是函数” } 哇 fn_试验 su-oracle您对su的实际功能感到困惑:您希望它在启动新流程时只与用户切换。您可以在交互式会话中使用ps控制它:您可以看到原始shell、su命令、由su启动的新shell以及当前的ps命令 由于shell函数是shell的本地函数,因此不能在(大)子shell中使用 你所能希望的最好的方法就是通过环境传递一些东西。我知道别名可以放

下面的脚本与根用户一起执行。切换后用户Unix函数显示错误

test.sh
#/bin/bash
fn_测试()
{
echo“这是函数”
}
哇
fn_试验

su-oracle您对
su
的实际功能感到困惑:您希望它在启动新流程时只与用户切换。您可以在交互式会话中使用
ps
控制它:您可以看到原始shell、
su
命令、由
su
启动的新shell以及当前的
ps
命令

由于shell函数是shell的本地函数,因此不能在(大)子shell中使用

你所能希望的最好的方法就是通过环境传递一些东西。我知道别名可以放在函数中。此外,这完全依赖于shell,可能无法移植

#!/bin/bash
fn_test()
{
echo "This is function"
}
whoami
fn_test
su - oracle<<EOF
whoami
fn_test #This function not called
EOF
exit 0

O/P
root $ ./test.sh
root
This is function
oracle
-ksh[2]: fn_test: not found [No such file or directory]