Hi,<\/p>\n
I have the below script which pulls information a system.<\/p>\n
timeval=5\ntimer=0\nsec=5\n\nwhile [ $timer -lt $timeval ]\n do\n clear\n\n now=$(date +'%d %b %Y | %H:%M:%S')\n while [ $sec -ge 0 ]; do\n echo -ne \"Updating in $sec\\033[0K\\r\" <-------\n let \"sec=sec-1\"\n sleep 1\n done\n\n podname=\"4g-du\"\n kubectl get pods -A | grep $podname | awk 'NR==1 {print \"NAMESPACE\",\"NAME\",\"READY\",\"STATUS\",\"RESTARTS\"} {print $1\" \"$2\" \"$3\" \"$4\" \"$5}' | column -t\n echo \"********************************************\"\n timer=$(( $timer + 1 ))\n sleep 10\ndone\n<\/code><\/pre>\n
Advertisement
This runs in a while loop and the Sleep 10 command at the bottom causes the script to wait 10 seconds before the loop is run again.<\/p>\n
I inserted a countdown timer in the script where the arrow is pointing <-------<\/code> so a timer starts when 5 seconds remain.<\/p>\nHow can I run the countdown timer because what happens is that the script does not continue until the countdown timer has ended.<\/p>\n
Is there a better way of doing this ?<\/p>\n
Thank You<\/p>","upvoteCount":5,"answerCount":5,"datePublished":"2024-09-12T14:47:29.493Z","author":{"@type":"Person","name":"tryllz","url":"https://community.spiceworks.com/u/tryllz"},"acceptedAnswer":{"@type":"Answer","text":"
You messed up the order on where you put the while loop, i think the problem you’re facing is that its waiting 15 seconds instead of 10 right?<\/p>\n
timeval=5\ntimer=0\nsec=5\n\nwhile [ $timer -lt $timeval ]\n do\n clear\n\n now=$(date +'%d %b %Y | %H:%M:%S')\n \n\n podname=\"4g-du\"\n kubectl get pods -A | grep $podname | awk 'NR==1 {print \"NAMESPACE\",\"NAME\",\"READY\",\"STATUS\",\"RESTARTS\"} {print $1\" \"$2\" \"$3\" \"$4\" \"$5}' | column -t\n echo \"********************************************\"\n timer=$(( $timer + 1 ))\n\n sleep 5 # Since the total time you want to wait is 10s, you wait 5 seconds here, then you start the countdown that takes 5 seconds to run\n while [ $sec -ge 0 ]; do\n echo -ne \"Updating in $sec\\033[0K\\r\" # <------- (Commented out the arrow)\n let \"sec=sec-1\"\n sleep 1\n done\ndone<\/code><\/pre>","upvoteCount":1,"datePublished":"2024-09-12T17:27:10.607Z","url":"https://community.spiceworks.com/t/how-to-achieve-this-in-bash/1117007/2","author":{"@type":"Person","name":"plary","url":"https://community.spiceworks.com/u/plary"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi,<\/p>\n
I have the below script which pulls information a system.<\/p>\n
timeval=5\ntimer=0\nsec=5\n\nwhile [ $timer -lt $timeval ]\n do\n clear\n\n now=$(date +'%d %b %Y | %H:%M:%S')\n while [ $sec -ge 0 ]; do\n echo -ne \"Updating in $sec\\033[0K\\r\" <-------\n let \"sec=sec-1\"\n sleep 1\n done\n\n podname=\"4g-du\"\n kubectl get pods -A | grep $podname | awk 'NR==1 {print \"NAMESPACE\",\"NAME\",\"READY\",\"STATUS\",\"RESTARTS\"} {print $1\" \"$2\" \"$3\" \"$4\" \"$5}' | column -t\n echo \"********************************************\"\n timer=$(( $timer + 1 ))\n sleep 10\ndone\n<\/code><\/pre>\nThis runs in a while loop and the Sleep 10 command at the bottom causes the script to wait 10 seconds before the loop is run again.<\/p>\n
I inserted a countdown timer in the script where the arrow is pointing <-------<\/code> so a timer starts when 5 seconds remain.<\/p>\nHow can I run the countdown timer because what happens is that the script does not continue until the countdown timer has ended.<\/p>\n
Is there a better way of doing this ?<\/p>\n
Thank You<\/p>","upvoteCount":5,"datePublished":"2024-09-12T14:47:29.591Z","url":"https://community.spiceworks.com/t/how-to-achieve-this-in-bash/1117007/1","author":{"@type":"Person","name":"tryllz","url":"https://community.spiceworks.com/u/tryllz"}},{"@type":"Answer","text":"
Just to add - representing date by pretty much anything other than largest unit to smallest is confusing and bad practice.
\nIf you are logging data and use grep (or anything like sed, awk, perl, python…), the output is then in non-time order. All the data from the firsts of the month, by month Apr, Aug, Dec, … Oct, Sep. then by year.
\nIf you use<\/p>\n
TZ=America/New_York date -Iseconds\n2024-09-12T13:43:30-04:00\n<\/code><\/pre>\nYou get something that can be more easily used in scripts;<\/p>\n
# Everything from May of this year;\ngrep -E '^2024-05\" SomeLogFile\n<\/code><\/pre>\nIf you need to compare that to sites around the globe, use;<\/p>\n
TZ=UTC date -Iseconds\n2024-09-12T17:33:44+00:00\n<\/code><\/pre>\nIf timezone info isn’t needed, then the lovely;<\/p>\n
date +\"%F %T\"\n2024-09-12 13:34:02\n<\/code><\/pre>\nIs perfect.
\nApologies, this is one of my soap boxes, I’ll see myself out…<\/p>","upvoteCount":4,"datePublished":"2024-09-12T17:59:25.122Z","url":"https://community.spiceworks.com/t/how-to-achieve-this-in-bash/1117007/3","author":{"@type":"Person","name":"stevejones4947","url":"https://community.spiceworks.com/u/stevejones4947"}},{"@type":"Answer","text":"