初始化脚本'为/ dev / tty的:没有这样的设备或地址“上重定向错误 [英] init script '/dev/tty: No such device or address' error on redirect

查看:214
本文介绍了初始化脚本'为/ dev / tty的:没有这样的设备或地址“上重定向错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将包括在底部参考整个脚本。该脚本会被调用向启动脚本的结束。如果我登录后运行该脚本,它没有错误运行。 30000英尺的观点,试图执行rsync命令,并捕获任何错误到一个变量。我使用bash 3.5.6pl。

下面是罪魁祸首行:

 的LogMessage + = $(rsync的-rpltDzv --delete --progress的/ mnt / WinPrimary到/ mnt / WinBackup 2  - ;&放1 GT;的/ dev / tty的)$的'\\ n'

什么被存储在变量,这并得到写入日志,是:

  /etc/Braums/scripts/Sync1to2.sh:第47行是:/ dev / tty的:没有这样的设备或地址

我GOOGLE的语法来捕获标准错误而不是标准输出的技术。我希望进度显示,而不是捕获所有屏幕的东西。
所以在init调用脚本Sync1to2.sh。具有上述命令。如果我登录之后运行命令行相同的脚本,它运行正常。

下面是整个Sync1to2.sh脚本。我不这样做的bash脚本过于频繁,所以一定要小心,不要伤害自己笑了起来。另外一个说明,下面是当我试图为/ dev /标准输出,它有一定的诊断。我打赌它有东西时,getty正在跑?

 #!/斌/庆典

#变量到如果在过程中的任何点发生错误设定。
ErrorOccured =假
的LogMessage =的LogMessage + = $ 0 $ $(日期)$在指出'的'\\ n'!如果(安装| grep的-cq到/ mnt / WinPrimary)
然后
    的LogMessage + = $'============================================= \\ N'
    的LogMessage + = $'] [未Windows主发现。 ] [\\ N'
    的LogMessage + = $'] [查看驱动器,然后再试一次。 ] [\\ N'
    的LogMessage + = $'============================================= \\ N'
    ErrorOccured =真
科幻
!如果(安装| grep的-cq到/ mnt / WinBackup)
然后
    的LogMessage + = $'============================================= \\ N'
    的LogMessage + = $'] [未Windows备份中。 ] [\\ N'
    的LogMessage + = $'] [查看驱动器,然后再试一次。 ] [\\ N'
    的LogMessage + = $'============================================= \\ N'
    ErrorOccured =真
科幻
如果测试$ ErrorOccured==假
然后
  #Maybe的硬盘以后添加清理
  如果[-f /mnt/WinPrimary/Sync1to2.err]
  然后
    的LogMessage + = $'============================================= \\ N'
    的LogMessage + = $'] [我要Sync1to2.err。 ] [\\ N'
    的LogMessage + = $'] [我们是否正在运行一个循环? ] [\\ N'
    的LogMessage + = $'============================================= \\ N'
    ErrorOccured =真
  科幻
  如果[-f /mnt/WinPrimary/Sync1to2.ok]
  然后
    的LogMessage + = $'============================================= \\ N'
    的LogMessage + = $'] [我要Sync1to2.ok。 ] [\\ N'
    的LogMessage + = $'] [我们是否正在运行一个循环? ] [\\ N'
    的LogMessage + = $'============================================= \\ N'
    ErrorOccured =真
  科幻
  如果[$ ErrorOccured==假]
  然后
    的LogMessage + = $'同步启动'$(日期)$的'\\ n'
    的LogMessage + = $(rsync的-rpltDzv --delete --progress的/ mnt / WinPrimary到/ mnt / WinBackup 2 - ;&放1 GT;的/ dev /标准输出)$的'\\ n'
    ExitValue = $?
    的LogMessage + = $'同步完成$(日期)$的'\\ n'
    的LogMessage + = $'____________________'$(日期)$的'\\ n'
    的LogMessage + = $(LS为/ dev / tty的*)$的'\\ n'
    的LogMessage + = $'____________________'$(日期)$的'\\ n'
    的LogMessage + = $'____________________'$(日期)$的'\\ n'
    的LogMessage + = $(LS为/ dev /标准输出)$的'\\ n'
    的LogMessage + = $'____________________'$(日期)$的'\\ n'    如果[$ ExitValue!= 0]
    然后
      ErrorOccured =真
    科幻
  科幻
科幻
回声$ ErrorOccured
如果[$ ErrorOccured==真]
然后
  回声Sync1to2出错了>> /mnt/WinPrimary/Sync1to2.err
  回声_______________________________________________________>> /mnt/WinPrimary/Sync1to2.err
  回声说完$(日期)>> /mnt/WinPrimary/Sync1to2.err
  回声$的LogMessage>> /mnt/WinPrimary/Sync1to2.err
  回声_______________________________________________________>> /mnt/WinPrimary/Sync1to2.err
其他
  回声Sync1to2 OK>> /mnt/WinPrimary/Sync1to2.ok
  回声_______________________________________________________>> /mnt/WinPrimary/Sync1to2.ok
  回声说完$(日期)>> /mnt/WinPrimary/Sync1to2.ok
  回声$的LogMessage>> /mnt/WinPrimary/Sync1to2.ok
  回声_______________________________________________________>> /mnt/WinPrimary/Sync1to2.ok
  CP /mnt/WinPrimary/menu.default.lst /mnt/WinPrimary/menu.lst
  CP /mnt/WinPrimary/menu.default.lst /mnt/WinBackup/menu.lst
  张树庭呼应向下
科幻
/etc/Braums/scripts/LogPing.pl Sync1to2$的LogMessage
出口254


解决方案

而不是写的/ dev / tty的,如何简单地交换输出和错误?

  yourcommand 3 GT;和2 2  - ;&放大器; 1 1 GT;及3 3 GT;&安培;  - 

这是重定向相当于使用第三临时变量交换两个变量的内容:

  3 GT;和2#TMP =标准错误
2 - ;&放大器; 1#标准错误=标准输出
1>及3#标准输出= TMP
3>&安培; - #接近(TMP)

I will include the entire script at the bottom for reference. This script gets called toward the end of the init scripts. If I run the script after logging in, it runs with no error. 30000 foot view, trying to execute a rsync command and capture any errors into a variable. I am using bash 3.5.6pl.

Here is the line that is the culprit:

LogMessage+=$(rsync -rpltDzv --delete --progress /mnt/WinPrimary /mnt/WinBackup 2>&1 >/dev/tty)$'\n'

What is stored in the variable, which does get written to a log, is:

/etc/Braums/scripts/Sync1to2.sh: line 47: /dev/tty: No such device or address

I googled the technique for the syntax to capture stderr but not stdout. I want the progress to show and not capture all the screen stuff. So the init calls the script "Sync1to2.sh". Which has the above command. If I run the same script from the command line after logging in, it runs as expected.

Here is the entire Sync1to2.sh script. I don’t do bash scripts too often, so be careful and don’t hurt yourself laughing. One other note, below is when I tried /dev/stdout and it has some diagnostic. I betting it has something to when getty is ran?

#!/bin/bash
#
#Variable to set if an error occurs at any point during the process.
ErrorOccured="False"
LogMessage=""

LogMessage+=$0$' was stated at '$(date)$'\n'

if !(mount | grep -cq "/mnt/WinPrimary")
then
    LogMessage+=$'=============================================\n'
    LogMessage+=$'][ No Windows Primary found.               ][\n'
    LogMessage+=$'][  Check drives and try again.            ][\n'
    LogMessage+=$'=============================================\n'
    ErrorOccured="True"
fi
if !(mount | grep -cq "/mnt/WinBackup")
then
    LogMessage+=$'=============================================\n'
    LogMessage+=$'][ No Windows Backup found.                ][\n'
    LogMessage+=$'][  Check drives and try again.            ][\n'
    LogMessage+=$'=============================================\n'
    ErrorOccured="True"
fi
if test "$ErrorOccured" == "False"
then
  #Maybe add cleanup the harddrive later
  if [ -f /mnt/WinPrimary/Sync1to2.err ]
  then
    LogMessage+=$'=============================================\n'
    LogMessage+=$'][ Found a Sync1to2.err.                   ][\n'
    LogMessage+=$'][  Are we being run in a loop?            ][\n'
    LogMessage+=$'=============================================\n'
    ErrorOccured="True"
  fi
  if [ -f /mnt/WinPrimary/Sync1to2.ok ]
  then
    LogMessage+=$'=============================================\n'
    LogMessage+=$'][ Found a Sync1to2.ok.                    ][\n'
    LogMessage+=$'][  Are we being run in a loop?            ][\n'
    LogMessage+=$'=============================================\n'
    ErrorOccured="True"
  fi
  if [ "$ErrorOccured" == "False" ]
  then
    LogMessage+=$'Sync Started '$(date)$'\n'
    LogMessage+=$(rsync -rpltDzv --delete --progress /mnt/WinPrimary /mnt/WinBackup 2>&1 >/dev/stdout)$'\n'
    ExitValue=$?
    LogMessage+=$'Sync Finished '$(date)$'\n'
    LogMessage+=$'____________________ '$(date)$'\n'
    LogMessage+=$(ls /dev/tty*)$'\n'
    LogMessage+=$'____________________ '$(date)$'\n'
    LogMessage+=$'____________________ '$(date)$'\n'
    LogMessage+=$(ls /dev/stdout)$'\n'
    LogMessage+=$'____________________ '$(date)$'\n'

    if [ $ExitValue != 0 ]
    then 
      ErrorOccured="True"
    fi
  fi
fi
echo $ErrorOccured
if [ "$ErrorOccured" == "True" ]
then
  echo "Sync1to2 errored out" >> /mnt/WinPrimary/Sync1to2.err
  echo "_______________________________________________________" >> /mnt/WinPrimary/Sync1to2.err
  echo "Finished $(date)" >> /mnt/WinPrimary/Sync1to2.err
  echo "$LogMessage" >> /mnt/WinPrimary/Sync1to2.err
  echo "_______________________________________________________" >> /mnt/WinPrimary/Sync1to2.err
else
  echo "Sync1to2 ok" >> /mnt/WinPrimary/Sync1to2.ok
  echo "_______________________________________________________" >> /mnt/WinPrimary/Sync1to2.ok
  echo "Finished $(date)" >> /mnt/WinPrimary/Sync1to2.ok
  echo "$LogMessage" >> /mnt/WinPrimary/Sync1to2.ok
  echo "_______________________________________________________" >> /mnt/WinPrimary/Sync1to2.ok
  cp /mnt/WinPrimary/menu.default.lst /mnt/WinPrimary/menu.lst
  cp /mnt/WinPrimary/menu.default.lst /mnt/WinBackup/menu.lst
  echo Shuting down
fi
/etc/Braums/scripts/LogPing.pl Sync1to2 "$LogMessage"
exit 254

解决方案

Rather than writing to /dev/tty, how about simply swapping stdout and stderr?

yourcommand 3>&2 2>&1 1>&3 3>&-

This is the redirection equivalent of swapping the contents of two variables using a third temporary variable:

3>&2  # tmp = stderr 
2>&1  # stderr = stdout
1>&3  # stdout = tmp
3>&-  # close(tmp)

这篇关于初始化脚本'为/ dev / tty的:没有这样的设备或地址“上重定向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆