Bash Commands - the diff of > and >> # Use I/O Redirection to generate a log file

1.   >
# Redirect stdout to a file.
# Creates the file if not present, otherwise overwrites it.

2.   >>
# Redirect stdout to a file.
# Creates the file if not present, otherwise appends to it.

3. Usage

&>filename         # Redirect both stdout and stderr to file "filename."

                               # This operator is now functional, as of Bash 4, final release.

1>filename          # Redirect stdout to file "filename."

1>>filename       # Redirect and append stdout to file "filename."

2>filename         # Redirect stderr to file "filename."

2>>filename       # Redirect and append stderr to file "filename."

: > filename        # Clear the file,  create it if not present.


For example:

Write a script that upon invocation shows the time and date, lists all logged-in users, and gives the
system uptime. The script then saves this information to a logfile.


#!/bin/bash

LOGFILE=script.log
ERRORFILE=script.errors

date  1>$LOGFILE
logname  1>>$LOGFILE
uptime  1>>$LOGFILE

bad_command1 2>>$ERRORFILE
bad_command2 2>>$ERRORFILE
bad_command3

date  &>$LOGFILE
logname  &>>$LOGFILE
uptime  &>>$LOGFILE

bad_command4 &>>$LOGFILE


# end of file

Note: ERRORFILE=script.errors        # Right format.

          ERRORFILE  =  script.errors    # Wrong format! It will generate a error message.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值