是否可以打印用于重定向 STDERR 的文件?
Would it be possible to print the file used to redirect STDERR?
是否可以打印用于重定向 STDERR 的文件名,给出下面的示例命令:
command.sh 2>file.err
command.sh中的代码:
#!/bin/sh
ls -l non_existing_file.txt
echo "STDERR file is: $stderrFilename" # variable should print file.err
这有点冒险,但您可以尝试解析 AIX 的 procfiles 输出。它涉及捕获 stderr 设备的主要和次要编号以及 inode 编号,然后查找相应的设备及其挂载点,然后使用 find
查找具有给定 inode 编号的文件:
#!/bin/sh
dev=$(procfiles $$ | awk ' == "2:" { print substr(, 5) }')
inode=$(procfiles $$ | awk ' == "2:" { print substr(, 5) }')
major=${dev%%,*}
minor=${dev##*,}
if [ "$major}" -eq 0 ]
then
echo I give up, the major number is zero
exit 1
fi
for file in /dev/*
do
[ -b "$file" ] || continue
if istat "$file" | grep -q "^Major Device ${major}.*Minor Device ${minor}$"
then
break
fi
done
fs=$(mount | awk ' == "'"${file}"'" { print }')
stderrFilename=$(find "$fs" -inum "$inode")
我用历史做了一个解决方案。不确定是否有更简单的方法(或合适的方法)。
#!/bin/sh
stderrfname=`history | tail -1 | awk '{ print }' | sed "s/.*>//"`
echo "STDERR file is: $stderrfname"
是否可以打印用于重定向 STDERR 的文件名,给出下面的示例命令:
command.sh 2>file.err
command.sh中的代码:
#!/bin/sh
ls -l non_existing_file.txt
echo "STDERR file is: $stderrFilename" # variable should print file.err
这有点冒险,但您可以尝试解析 AIX 的 procfiles 输出。它涉及捕获 stderr 设备的主要和次要编号以及 inode 编号,然后查找相应的设备及其挂载点,然后使用 find
查找具有给定 inode 编号的文件:
#!/bin/sh
dev=$(procfiles $$ | awk ' == "2:" { print substr(, 5) }')
inode=$(procfiles $$ | awk ' == "2:" { print substr(, 5) }')
major=${dev%%,*}
minor=${dev##*,}
if [ "$major}" -eq 0 ]
then
echo I give up, the major number is zero
exit 1
fi
for file in /dev/*
do
[ -b "$file" ] || continue
if istat "$file" | grep -q "^Major Device ${major}.*Minor Device ${minor}$"
then
break
fi
done
fs=$(mount | awk ' == "'"${file}"'" { print }')
stderrFilename=$(find "$fs" -inum "$inode")
我用历史做了一个解决方案。不确定是否有更简单的方法(或合适的方法)。
#!/bin/sh
stderrfname=`history | tail -1 | awk '{ print }' | sed "s/.*>//"`
echo "STDERR file is: $stderrfname"