簡體   English   中英

Unix 腳本外殼

[英]Unix Script Shell

任何人都可以幫助我編寫腳本

我正在嘗試制作一個腳本外殼,如果這些文件中的任何一個被修改並且腳本必須自動例行,它將向根發出警報/通過電子郵件發送,因此腳本必須始終運行

我嘗試做一些事情開始,但我需要一些人來糾正我或給我想法

#!/bin/bash

if [`find . -mtime -0` -eq date];then
    mail -s "modified files" root
    find  /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/sudoers /etc/hosts /etc/sysconfig/network /etc/resolv.conf /etc/bashrc /etc/fstab -type f -mtime -50
else
    mail -s "nothing" root
fi

您可以將if語句放在一個循環中,該循環會在迭代之間休眠一段時間:

#!/bin/bash

while sleep 10; do
    # Your "if" goes here 
    # it will be executed with 10 second sleep between iterations.
    if [...]
       ...
    fi
done

這是腳本(a.sh)

#!/bin/bash

date=1
file=$(find . -mtime -${date})

if [  "${file}X" != "X" ]; then
  echo "$file" |mail -s "modified files" root
fi

將腳本放在cronjob中,例如每10分鍾運行一次:

0,10,20,30,40,50 * * * * bash a.sh

玉米作業將繼續監視文件更新狀態,您無需在 while 循環中使用更多腳本來關心它。

用指向資源的指針來補充特定的答案:

一些開始使用bash資源:

這里有兩個有用的實驗資源

  • http://shellcheck.net可以幫助您查找 shell 代碼中的語法錯誤- 雖然這是一個很好的資源,但它無法檢測您代碼中的所有問題。 但是,它會檢測您的[ ... ]條件的一個問題。

  • http://explainshell.com是一個很棒的站點,它根據man解釋給定的命令行 例如,這里是find . -mtime -0的解釋find . -mtime -0 find . -mtime -0 : http://explainshell.com/explain?cmd=find+.+-mtime+-0 - 請注意,底層man頁是 Ubuntu 的,因此它們涵蓋GNU實用程序,許多但並非所有 Unix 發行版都使用它; GNU 實用程序通常具有有用但非標准的擴展,在某些平台(例如 OSX)上可能不可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM