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

标签 bash ubuntu arm

我创建了一个简单的 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/

相关文章:

MYSql 配置绑定(bind)地址设置为 0.0.0.0 但 netstat 在 Ubuntu 上显示不同

c - 在 ARM 微 Controller 上延迟打开和关闭 LED

c - 使用GDB时缺少ELF符号 "var"?

c - 段错误处理程序因段错误而崩溃

bash - 如何从脚本本身获取 Bash 脚本所在的目录?

linux - 保留备份

bash - 奇怪的 bash 别名扩展

linux - 查找连接到 Linux 机器的所有存储设备

Linux 路径/依赖关系处理

linux - 在 .zshrc : No such file or directory 中使用 grep 命令