当使用Bash陷阱功能中内置的调用者时,结果会caller 0给出错误的行号,总是给出1.例如:
#!/bin/bash
function foo {
exit 1
}
function bar {
foo
}
function err {
(( i = 0 ))
while caller $i; do
(( ++i ))
done
}
trap err EXIT
bar
Run Code Online (Sandbox Code Playgroud)
给出以下输出:
1 foo ./test.sh
6 bar ./test.sh
15 main ./test.sh
Run Code Online (Sandbox Code Playgroud)
虽然输出i > 0是正确的,但caller 0在陷阱处理程序中使用时,它似乎总是1作为行号给出.有没有办法从陷阱处理程序中获取失败函数的实际行号?
在Bash中,可以对变量进行字符串操作,例如在Linux机器上获取当前运行级别:
current_runlevel=$(runlevel) #sample output: 'N 2'
current_runlevel=${current_runlevel#* }
echo $current_runlevel #sample output: '2'
Run Code Online (Sandbox Code Playgroud)
但是,是否可以组合两条线,因此不需要中间变量?使用相同的示例,我希望它看起来像:
current_runlevel=${$(runlevel)#* }
Run Code Online (Sandbox Code Playgroud)
这不起作用,给出错误
${$(runlevel)#* }: bad substitution
Run Code Online (Sandbox Code Playgroud)
关于如何在Bash字符串操作表达式中使用文字字符串的任何想法?
我正在为Linux PAM编写一个模块,该模块用于setuid()删除正在进行身份验证的用户的权限.当然,这只有在EUID是root的情况下才有效.那么,PAM堆栈是否始终以root身份运行,无论应用程序使用它?