我想撰寫一個 bash 腳本,它將兩個引數作為輸入(day hostname)并輸出在當月username當天登錄的所有用戶的.hostname
到目前為止,我已經撰寫了這段代碼:
#!/bin/bash
#parameter correction checking
if [ ! $# -eq 2 ]
then echo "You have input the wrong number of parameters!"
fi
#Current month checking
CurrentMonth=`date | cut -d' ' -f2`
#This will contain the abbreviation of the current month: e.g "Mar"
#$2 -> IP/hostname; $1 -> day (of the current month)
last | grep '$2' | grep '$CurrentMonth $1' | cut -d' ' -f1
#here the two greps will get the lines that check out our requirements, and the cut will return only the username
我做錯了什么?如果我用和替換CurrentMonth變數,代碼運行得很好Mar$2IP address
uj5u.com熱心網友回復:
單引號 ( '') 之間的所有內容都將被視為文字文本,變數不會被擴展。""而是對包含變數的字串使用雙引號 ( )。
請注意,如果您使用 bash 腳本啟動bash -x yourscript或將 shebang 更改為#!/bin/bash -x.
您的腳本的作業版本如下所示:
#!/bin/bash
#parameter correction checking
if [ ! $# -eq 2 ]
then echo "You have input the wrong number of parameters!"
fi
#Current month checking
CurrentMonth=`date | cut -d' ' -f2`
#This will contain the abbreviation of the current month: e.g "Mar"
#$2 -> IP/hostname; $1 -> day (of the current month)
last | grep "$2" | grep "$CurrentMonth $1" | cut -d' ' -f1
#here the two greps will get the lines that check out our requirements, and the cut will return only the username
筆記:
last默認情況下列印一個縮寫的用戶名,您可能需要添加-w標志來列印全名。
另請注意,grep您可以使用lasts 內置函式來按時間過濾:
-s, --since time
Display the state of logins since the specified time. This is useful, e.g., to easily determine who was logged in at a particular time. The option is often combined with --until.
-t, --until time
Display the state of logins until the specified time.
TIME FORMATS
The options that take the time argument understand the following formats:
┌────────────────────┬────────────────────────────────────────────┐
│ │ │
│YYYYMMDDhhmmss │ │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│YYYY-MM-DD hh:mm:ss │ │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│YYYY-MM-DD hh:mm │ (seconds will be set to 00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│YYYY-MM-DD │ (time will be set to 00:00:00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│hh:mm:ss │ (date will be set to today) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│hh:mm │ (date will be set to today, seconds to 00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│now │ │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│yesterday │ (time is set to 00:00:00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│today │ (time is set to 00:00:00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│tomorrow │ (time is set to 00:00:00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│ 5min │ │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│-5days │ │
└────────────────────┴────────────────────────────────────────────┘
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/449997.html
上一篇:Linux合并檔案
下一篇:C中兩個檔案之間全域變數的使用
