是否可以打印用于重定向STDERR的文件?

时间:2018-10-15 14:50:36

标签: shell aix

给出以下示例命令,是否可以打印用于重定向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

2 个答案:

答案 0 :(得分:1)

这有点冒险,但是您可以尝试解析AIX的procfiles输出。它涉及捕获stderr设备的主要和次要编号以及inode编号,然后查找相应的设备及其挂载点,然后使用find查找具有给定inode编号的文件:

#!/bin/sh
dev=$(procfiles $$ | awk '$1 == "2:" { print substr($4, 5) }')
inode=$(procfiles $$ | awk '$1 == "2:" { print substr($5, 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 '$1 == "'"${file}"'" { print $2 }')
stderrFilename=$(find "$fs" -inum "$inode")

答案 1 :(得分:1)

我使用历史记录提出了解决方案。不确定是否有更简便的方法(或适当的方法)。

#!/bin/sh
stderrfname=`history | tail -1 | awk '{ print $3 }' | sed "s/.*>//"`
echo "STDERR file is: $stderrfname"
相关问题