如何解决如何从我的 bash 脚本创建日志文件
如何从我的 bash 脚本制作日志文件?
我要离开没有选项的脚本,我只想知道如何注册正在使用的脚本并制作一个单独的 .log
文件。
脚本:
#!/bin/bash
trash=~/TRASH
if [ ! -e $trash ]; then
mkdir $trash
elif [ ! -d $trash ]; then
echo "$0: error: $trash is not a directory"; exit 1
fi
while getopts "hr:t:s:u:" options; do
case $options in
#ALL THE OPTIONS AREN'T HERE FOR THE PURPOSE OF KEEPING IT SHORTER
shift $((OPTIND-1))
while [ $# -gt 0 ]; do
if [ ! -e $1 ]; then
echo "$0: error: tried to delete file that does not exist: $1"
shift
continue
fi
tarname="$1.tar"
tar -cf "$tarname" "$1"
mv "$tarname" $trash
rm -rf "$1"
shift
done
解决方法
一种方式...
#!/bin/bash
(
blah code
) > file.log
,
将 stdout 和 stderr 显示到控制台和日志,并附加到日志,可能是这样的:
#!/bin/bash
(
blah code
) 2>&1 | tee -a file.log
地点:
-
2>&1
- 将标准错误重定向到标准输出 -
| tee -a file.log
- 将 stdout 发送到控制台,但也“tee”了一个副本以-a
附加到file.log
工作示例:
$ cat blah
(
echo "hello" # will print to stdout
dirxys # will send message to stderr
) 2>&1 | tee -a blah.log
# both stdout/stderr show up on console:
$ blah
hello
./blah: line 3: dirxys: command not found
# both stdout/stderr also show up in log:
$ cat blah.log
hello
./blah: line 3: dirxys: command not found
,
只需调用脚本并重定向其输出:
./script.sh > script.log
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。