如何编写一个 bash 脚本,在每天特定时间请求权限后删除我的系统日志文件?
How do I write a bash script that deletes my system log files after asking for permission at a certain time everyday?
我想编写一个 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 循环中如何工作?我如何确保它读取特定模式? for循环进入"Exit"后是否还能正常运行从而关闭while循环,IE,关闭子进程中的父进程会立即中断子进程吗?
#!/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?
如果你想到前台运行ning的含义,你就会明白为什么它需要在后台运行。如果您当时不在 logged-in 怎么办?如果您正在输入文档怎么办?您可能会考虑创建一个 pop-up window (Windows-style),但是在哪里?哪个控制台?
# 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
运行s,在我的系统上,每晚在 3:00.
备份我的 diapositives-administration
touch /Permission.txt
在 root-directory 中创建这样的文件是个坏主意。它可能应该在某处 /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 是一个奇怪的权限。如果您在 multi-user 系统上,它应该(可能)是 0774 并且您将管理员放在正确的组中。
我不明白你的错误逻辑。您不希望通过共享访问修改数据,但您向世界(=系统上的每个人)授予写入权限?
echo "Delete System Logs for: "%d"/"%m"/"%y" ?">/Permission.txt
open /Permission.txt
Input=/Permission.txt</dev/f0
我不确定你想在这里建立什么。无效 bash-scripting。
# 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
下用一个device-file表示。您应该能够执行 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 不是一个好主意。您应该确保脚本 运行 在正确的权限下。回显密码是个坏主意。如果必须,您可以 set-up 无密码 sudo vis visudo
.
fi
if [ $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。
我想编写一个 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 循环中如何工作?我如何确保它读取特定模式? for循环进入"Exit"后是否还能正常运行从而关闭while循环,IE,关闭子进程中的父进程会立即中断子进程吗?
#!/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?
如果你想到前台运行ning的含义,你就会明白为什么它需要在后台运行。如果您当时不在 logged-in 怎么办?如果您正在输入文档怎么办?您可能会考虑创建一个 pop-up window (Windows-style),但是在哪里?哪个控制台?
# 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
运行s,在我的系统上,每晚在 3:00.
备份我的 diapositives-administrationtouch /Permission.txt
在 root-directory 中创建这样的文件是个坏主意。它可能应该在某处 /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 是一个奇怪的权限。如果您在 multi-user 系统上,它应该(可能)是 0774 并且您将管理员放在正确的组中。
我不明白你的错误逻辑。您不希望通过共享访问修改数据,但您向世界(=系统上的每个人)授予写入权限?
echo "Delete System Logs for: "%d"/"%m"/"%y" ?">/Permission.txt
open /Permission.txt
Input=/Permission.txt</dev/f0
我不确定你想在这里建立什么。无效 bash-scripting。
# 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
下用一个device-file表示。您应该能够执行 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 不是一个好主意。您应该确保脚本 运行 在正确的权限下。回显密码是个坏主意。如果必须,您可以 set-up 无密码 sudo vis visudo
.
fi
if [ $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。