如何从我的 bash 脚本创建日志文件
How to create log file from my bash script
如何从我的 bash 脚本创建日志文件?
我要离开没有选项的脚本,我只想知道如何注册正在使用的脚本并制作一个单独的 .log
文件。
脚本:
#!/bin/bash
trash=~/TRASH
if [ ! -e $trash ]; then
mkdir $trash
elif [ ! -d $trash ]; then
echo "[=11=]: 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 ]; then
echo "[=11=]: error: tried to delete file that does not exist: "
shift
continue
fi
tarname=".tar"
tar -cf "$tarname" ""
mv "$tarname" $trash
rm -rf ""
shift
done
一种方式...
#!/bin/bash
(
blah code
) > file.log
只需调用脚本并重定向其输出:
./script.sh > script.log
要在控制台和日志中显示 stdout 和 stderr,并附加到日志,可能类似于:
#!/bin/bash
(
blah code
) 2>&1 | tee -a file.log
其中:
2>&1
- 将 stderr 重定向到 stdout
| tee -a file.log
- 将标准输出发送到控制台,同时 '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
如何从我的 bash 脚本创建日志文件?
我要离开没有选项的脚本,我只想知道如何注册正在使用的脚本并制作一个单独的 .log
文件。
脚本:
#!/bin/bash
trash=~/TRASH
if [ ! -e $trash ]; then
mkdir $trash
elif [ ! -d $trash ]; then
echo "[=11=]: 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 ]; then
echo "[=11=]: error: tried to delete file that does not exist: "
shift
continue
fi
tarname=".tar"
tar -cf "$tarname" ""
mv "$tarname" $trash
rm -rf ""
shift
done
一种方式...
#!/bin/bash
(
blah code
) > file.log
只需调用脚本并重定向其输出:
./script.sh > script.log
要在控制台和日志中显示 stdout 和 stderr,并附加到日志,可能类似于:
#!/bin/bash
(
blah code
) 2>&1 | tee -a file.log
其中:
2>&1
- 将 stderr 重定向到 stdout| tee -a file.log
- 将标准输出发送到控制台,同时 '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