#!/bin/bash
who>log.txt
top>>log.txt
EMAIL="[email protected]"
LOG="/home/projekt/skrypt/log.txt"
mail -s "skrypt_logi" ${EMAIL}<${LOG}
rm logi.txt
我安装了使用电子邮件的软件包,编写了创建 log.txt 的脚本,在 log.txt 中放入了一些信息,但没有发送电子邮件并且它挂起了,所以我不得不中断它。
答案1
你可以创建“log.txt”当前的目录是,但是当您想要从中读取时,您需要指定完整路径。
您应该在整个脚本中使用变量:
#!/bin/bash
email="[email protected]"
log="/home/projekt/skrypt/log.txt"
who > "$log"
top -n1 >> "$log"
mail -s "skrypt_logi" "$email" < "$log"
rm "$log"
读忘记在 bash/POSIX shell 中引用变量的安全隐患关于引用变量的重要性。
答案2
脚本因为命令 'top' 而卡住了。它应该是这样的:
top -b -n1 >> log.tx
http://manpages.ubuntu.com/manpages/xenial/man1/top.1.html
-b :Batch-mode operation
Starts top in Batch mode, which could be useful for sending
output from top to other programs or to a file. In this
mode, top will not accept input and runs until the iterations
limit you've set with the `-n' command-line option or until
killed.