gpt4 book ai didi

bash - Ubuntu Server (ARM) Bash 脚本未写入文件

转载 作者:行者123 更新时间:2023-12-04 18:58:45 26 4
gpt4 key购买 nike

我创建了一个简单的 bash 脚本来更新运行 PiHole(在 Ubuntu Server ARM 上)的树莓派。
我有一个 cron 设置,但我想让脚本将它运行的日期写入日志文件,这样我就可以确保它在应该运行的时候运行。我遇到问题的命令是

date >> ./update.log
我已经手动运行了几次脚本,但是 update.log 文件仍然是空的。
整个脚本如下
#!/bin/bash

# This script will update the system

## This command updated the raspberry pi
echo " Updating OS..."
echo ""
apt-get update -y
echo ""
echo " Upgrading OS..."
echo ""
apt-get upgrade -y

## This command updates Pi-Hole
echo ""
echo " Updating PiHole..."
echo ""
pihole -up

## This command updates DNS
echo ""
echo " Updating DNS..."
echo ""
wget https:/www.internic.net/domain/named.root -qO- | sudo tee /var/lib/unbound/root.hints

## This command logs when this script is ran
echo ""
echo " Entering date in log..."
echo ""
date >> ./update_log

## This command reboots the system
echo ""
echo " Rebooting..."
echo ""
reboot -fn
如果你能告诉我输入新行的正确方法,那么我就不必使用额外的 echo ""命令了。 (我不是 bash 脚本编写者,习惯于 PowerShell)

最佳答案

目前您正在写入文件 update.log在一些未定义的目录中。
您可能需要指定绝对路径,例如 /home/user/update.log .

如需打印打印换行符,您需要调用echo-e标志:echo -e "\n\n\n\n"来自 man echo :

       -n     do not output the trailing newline

-e enable interpretation of backslash escapes

-E disable interpretation of backslash escapes (default)
...

If -e is in effect, the following sequences are recognized:
...
\n new line

\r carriage return
...

关于bash - Ubuntu Server (ARM) Bash 脚本未写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71641904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com