if文に整数式が必要です。

if文に整数式が必要です。

Macが24時間以上実行されている場合は、Macを終了する小さな終了スクリプトがあります。私はBashスクリプトの実際の経験はありませんが、私は次のことをしました。

!/bin/bash

#Maximum number of days to be up
max=1

#Get the uptime days and assign it to a variable
uptime_days=`uptime | cut -d " " -f 5`

if [ $uptime_days -ge $max ]
  then 
  shutdown -h now
fi

exit 0

これで、次のエラーメッセージが表示されます。

./shutdown: line 9: (: days,: integer expression expected

誰でも助けることができますか?

ベストアンサー1

これはmacOSで動作します。リリースタイムスタンプを取得し、秒単位の現在のタイムスタンプを取得し、いくつかの計算結果を得ました。

#!/bin/bash
BOOT_TIME=$(sysctl -n kern.boottime | sed -e 's/.* sec = \([0-9]*\).*/\1/')
CURR_TIME=$(date +%s)
MAX_UPDAYS=1 #Days

DAYS_UP=$(( ( $CURR_TIME - $BOOT_TIME) / 86400 ))
if [ $DAYS_UP -ge ${MAX_UPDAYS} ];then
   shutdown -h now
fi

おすすめ記事