Linux 在shell脚本中切换用户后无法调用Unix函数
下面的脚本与根用户一起执行。切换后用户Unix函数显示错误 test.shLinux 在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中使用 你所能希望的最好的方法就是通过环境传递一些东西。我知道别名可以放
#/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]