bash – 使用重定向创建输出日志
发布时间:2020-12-15 22:08:27 所属栏目:安全 来源:网络整理
导读:我正在使用“Advanced Bash Scripting Guide”创建我的第一个脚本. 其中一个练习要求一个脚本,一旦运行,就将输出保存到日志文件中. 我已经设法创建了我的第一个基本脚本但是在最后一部分遇到了麻烦.该脚本必须包含用于创建日志文件的代码,但我只能在shell中
我正在使用“Advanced
Bash Scripting Guide”创建我的第一个脚本.
其中一个练习要求一个脚本,一旦运行,就将输出保存到日志文件中. 我已经设法创建了我的第一个基本脚本但是在最后一部分遇到了麻烦.该脚本必须包含用于创建日志文件的代码,但我只能在shell中单独执行. 该文件名为myFirstShellScript.txt.当我运行./myFirstShellScript.txt时,脚本会运行.如果我键入./myFirstShellScript.txt>脚本运行后shell中的myFirstShellScriptLog使用输出创建新文件.现在,我尝试在脚本中添加此行,但输出的文件为空. 这是我的第一个剧本,请不要笑. #! /bin/bash # show todays date and time echo "Todays date is $(date +%j)." # Show who is currently logged into the system echo $(who) # show system uptime echo $(uptime) # log output in separate file using redirect exit 我需要做什么(以尽可能简单的英语)让脚本自己创建输出文件,而不是一旦运行就必须在shell中单独执行? 解决方法
通常将部分封装到(),如:
#!/bin/bash ( # show todays date and time echo "Todays date is $(date +%j)." # Show who is currently logged into the system echo $(who) # show system uptime echo $(uptime) ) > myFirstShellScriptLog (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |