bash - Shell Script to run command every 5secs for x number of secs -
i'm learning bash scripting , want call function every 5 seconds x number of seconds. x determined command line argument when script run. i've tried watch seems go on forever, don't seem have ability return after x seconds. there might sleep way seems clumsy , have deal local drift. there elegant solution?
my code:
#!/bin/bash if [ $# -ne 1 ]; # 1 command line arg allowed echo "incorrect arguments" exit 1 elif ! [[ $1 =~ ^[0-9]+$ ]]; # arg must digit echo "argument must positive number" exit 1 fi ask() { output=$(snmpwalk -v2c -c community server oib) cleanoutput="${output:32}%" echo $cleanoutput } #export -f ask #watch -n5 ask
the seconds variable counts number of seconds since bash started:
#!/bin/bash while (( seconds <= 20 )) echo "running something" sleep 5 || break done echo "done" this simplistic method sleep 5 after last run when know it'll go on 20 seconds, , not try account run time of command (if command runs 2 seconds, it'll end starting once every 7 seconds).
Comments
Post a Comment