我想写一个bash脚本,每天在东部标准时间晚上9点删除我的系统日志。
我的经验相对较少。
#!/bin/sh
# I would want it to self-execute at some time every day.
# Would that mean that the shell needs to always run in the background?
# Is there a method by which I can add this execution to a daemon or other
# background-process's code-content? (If so please give me details)
touch /Permission.txt
#^^Process creates a file in root directory
chflags nouchg /Permission.txt
#^^Just in case the "no changes" flag is on, preventing me from changing
# permissions
chmod 0747 /Permission.txt
#^^Sets permissions so that I can write to the file without being root.
# I also set group permissions to read-only, my logic being that I want
# raw data to go into this file without it being modified by shared
# Ownership processes..?
echo "Delete System Logs for: "%d"/"%m"/"%y" ?">/Permission.txt
open /Permission.txt
Input=/Permission.txt</dev/f0
#^^Not just for the purposes of this script but for future scripts-I want
# to capture raw binary data from peripheral devices. Is the output
# of the keyboard interpreted by the OS or any other process before saving
# to the text file?
# How do I get raw binary-output to a file from hardware devices? How can
# I go about understanding the opcode syntax-whether it uses even or odd
# parity; whether it is big or small endian; and other binary nuances.
while [ $Input != "Exit" ]
do
read /Permission.txt
if [ $Input == "Yes"|"yes" ]; then
echo "Exit">/Permission.txt
echo "sudo rm -rf /private/var/log/* ;">ttys000 #See 1 below
echo "********" #The Password
fi
if [ $Input == "No"|"no" ]; then
echo "Exit">/Permission.txt
fi
end
# 1 not sure where the process is in the system and whether or not sending
# that string to tty will result in the command being executed as if terminal
# were running, will the ";" be seen as the return button being pressed?我想让它监听模式"Yes“或"No”或它的二进制等价物。"read“命令在while循环中是如何工作的?如何确保它读取特定的模式?在进入"Exit“从而关闭while循环后,for循环是否正常工作,IE,关闭子进程内的父进程是否会立即中断子进程?
发布于 2018-02-23 01:13:35
#!/bin/sh
# I would want it to self-execute at some time every day.
# Would that mean that the shell needs to always run in the background?如果你考虑到在前台运行的含义,你就会理解为什么它需要在后台运行。如果您没有在那个确切的时间登录怎么办?如果您正在键入文档,该怎么办?您可能会考虑创建一个弹出窗口(Windows风格),但是在哪里呢?哪个控制台?
# Is there a method by which I can add this execution to a daemon or other
# background-process's code-content? (If so please give me details)试试man crontab。在crontab中,您可以指定作业必须运行的频率(例如:
# m h D M dow cmd
0 3 * * * /usr/local/bin/backup.diaadm 在我的系统上,每晚3:00运行我的备份-管理。
touch /Permission.txt在根目录中创建这样的文件不是一个好主意。它可能应该在/var/local下的某个目录中。
# Process creates a file in root directory
chmod 0747 /Permission.txt
# Sets permissions so that I can write to the file without being root.
# I also set group permissions to read-only, my logic being that I want direct
# and raw data to go into this file without it being modified by shared
# Ownership processes..? 747是一个奇怪的权限。如果您在多用户系统上,它应该(可能)是0774,并且您将管理员放在正确的组中。
我不明白你那有缺陷的逻辑。您不希望通过共享访问来修改数据,但您却将写权限授予了世界(=系统中的每个人)?
echo "Delete System Logs for: "%d"/"%m"/"%y" ?">/Permission.txt
open /Permission.txt
Input=/Permission.txt</dev/f0我不确定您在这里试图建立的是什么。它不是有效的bash脚本。
# Not just for the purposes of this script but for future scripts-I want to be
# able to capture raw binary data from peripheral devices. Is the output
# of the keyboard interpreted by the OS or any other process before saving
# to the text file?
# How do I get raw binary-output to a file from hardware devices? How can
# I go about understanding the opcode syntax-whether it uses even or odd
# parity; whether it is big or small endian; and other binary nuances. 通常,Unix下的外围设备由/dev下的设备文件表示。您应该能够执行cat /dev/devicename来读取来自设备的输入。但是,如果您想使用来自设备的原始输入,bash可能不是合适的工具。Perl和Python对这类事情有更好的支持,而C或C++最适合这类工作。
while read Input ; do
if [ $Input == "Yes"|"yes" ]; then
echo "Exit">/Permission.txt在这里,您将覆盖正在读取的文件。我不明白为什么,这似乎是如此不合逻辑,以至于我怀疑你是否想要这样。
echo "sudo rm -rf /private/var/log/*"
echo "********" #The Password在脚本中使用sudo不是一个好主意。您应该确保脚本在正确的权限下运行。回显密码不是一个好主意。如果必须,您可以设置无密码的sudo vis visudo。
fi如果$Input == "No"|"no“;则echo "Exit">/Permission.txt
请看我之前对此的评论。
fi结束
结束?
# 1 not sure where the process is in the system and whether or not sending
# that string to tty will result in the command being executed as if terminal
# were running, will the ";" be seen as the return button being pressed?不是的。为什么会这样呢?回声到tty回声到tty。
你可能正在尝试这样的东西:
while read Input ; do
if [ $Input = "Yes" ]; then
sudo rm -rf /private/var/log/* ;
elif [ $Input = "No" ]; then
exit
fi
done < /Permission.txt你也应该,至少让你的代码通过shellcheck。
https://stackoverflow.com/questions/48932186
复制相似问题