willdashwood
willdashwood

Reputation: 33

Pause for user input but continue after X seconds

I've knocked up a quick bash script to rsync backups from one server to another and put them in the correct folder structure on the destination server.

#!/usr/bin/env bash
fullnaspaths=$(ls -d /poolz1/server.example.com/accounts/*/jbm.*/*/)
total_paths=$(wc -l <<< "$fullnaspaths")
for path in $fullnaspaths
do
    let COUNTER=COUNTER+1
    user=$(basename $path)
    echo [$COUNTER of $total_paths] $user
    rsync -ia $path [email protected]:/backup/2017-07-11/accounts/$user/
done

But what I'd like to do is add a breakpoint immediately before the rsync command which prompts the user to exit the script or continue. The bit I'm struggling with is how I might also add a timeout so that it continues regardless of any key input, the script continues in the loop after let's say 60 seconds.

Does anyone know if and how that could be achieved?

Many thanks.

Upvotes: 0

Views: 561

Answers (2)

123
123

Reputation: 11216

You could use read -t

read -t5 -n1 -p"press any key in 5 seconds to cancel" && exit

printf "\n%s\n" "continuing"

Upvotes: 5

Nidhoegger
Nidhoegger

Reputation: 5232

My suggestion is to do it like the gentoo portage. It counts from 5 to 1 and says you should press Ctrl+C to cancel. Like this:

#!/bin/bash

echo "Press Ctrl+C to cancel"
for i in {5..1}; do
    echo -n "$i "
    sleep 1
done
echo
echo "Continuing..."

Upvotes: 2

Related Questions