Skip to main content
added 126 characters in body
Source Link
Giuseppe
  • 215
  • 2
  • 8

See this answer below for a zsh solution


Let's say I have the following in my ~/.bash_aliases to ask for confirmation before suspending the system:

function suspend()
{ #
    echo "Please confirm/cancel system suspension:"
    select confirmation in "confirm" "cancel"; do
        case ${confirmation} in
        confirm )
            echo "System suspending..."
            systemctl suspend
            break;;
        cancel )
            echo "Canceled suspension."
            break;;
        esac
    done
}

I would like systemctl suspend to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.

I tried the following, with a backgrounded sleep in a subshell:

function suspend()
{ #
    flag_cancel=0
    echo "Please confirm/cancel system suspension:"
    (
    sleep 10 &&
        if [ $flag_cancel -eq 0 ]; then
        echo "System suspending..."
        systemctl suspend
        fi &
    )
    select confirmation in "confirm" "cancel"; do
    case ${confirmation} in
        confirm )
        echo "System suspending..."
        systemctl suspend
        break;;
        cancel )
        flag_cancel=1
        echo "Canceled suspension."
        break;;
    esac
    done
}

but a change of the value of flag_cancel is not taken into account, so the command is always executed after the sleep.

How to achieve what I want?

Let's say I have the following in my ~/.bash_aliases to ask for confirmation before suspending the system:

function suspend()
{ #
    echo "Please confirm/cancel system suspension:"
    select confirmation in "confirm" "cancel"; do
        case ${confirmation} in
        confirm )
            echo "System suspending..."
            systemctl suspend
            break;;
        cancel )
            echo "Canceled suspension."
            break;;
        esac
    done
}

I would like systemctl suspend to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.

I tried the following, with a backgrounded sleep in a subshell:

function suspend()
{ #
    flag_cancel=0
    echo "Please confirm/cancel system suspension:"
    (
    sleep 10 &&
        if [ $flag_cancel -eq 0 ]; then
        echo "System suspending..."
        systemctl suspend
        fi &
    )
    select confirmation in "confirm" "cancel"; do
    case ${confirmation} in
        confirm )
        echo "System suspending..."
        systemctl suspend
        break;;
        cancel )
        flag_cancel=1
        echo "Canceled suspension."
        break;;
    esac
    done
}

but a change of the value of flag_cancel is not taken into account, so the command is always executed after the sleep.

How to achieve what I want?

See this answer below for a zsh solution


Let's say I have the following in my ~/.bash_aliases to ask for confirmation before suspending the system:

function suspend()
{ #
    echo "Please confirm/cancel system suspension:"
    select confirmation in "confirm" "cancel"; do
        case ${confirmation} in
        confirm )
            echo "System suspending..."
            systemctl suspend
            break;;
        cancel )
            echo "Canceled suspension."
            break;;
        esac
    done
}

I would like systemctl suspend to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.

I tried the following, with a backgrounded sleep in a subshell:

function suspend()
{ #
    flag_cancel=0
    echo "Please confirm/cancel system suspension:"
    (
    sleep 10 &&
        if [ $flag_cancel -eq 0 ]; then
        echo "System suspending..."
        systemctl suspend
        fi &
    )
    select confirmation in "confirm" "cancel"; do
    case ${confirmation} in
        confirm )
        echo "System suspending..."
        systemctl suspend
        break;;
        cancel )
        flag_cancel=1
        echo "Canceled suspension."
        break;;
    esac
    done
}

but a change of the value of flag_cancel is not taken into account, so the command is always executed after the sleep.

How to achieve what I want?

Rollback to Revision 2
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

For zsh, see the EDIT section in the accepted answer


Let's say I have the following in my ~/.bash_aliases to ask for confirmation before suspending the system:

function suspend()
{ #
    echo "Please confirm/cancel system suspension:"
    select confirmation in "confirm" "cancel"; do
        case ${confirmation} in
        confirm )
            echo "System suspending..."
            systemctl suspend
            break;;
        cancel )
            echo "Canceled suspension."
            break;;
        esac
    done
}

I would like systemctl suspend to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.

I tried the following, with a backgrounded sleep in a subshell:

function suspend()
{ #
    flag_cancel=0
    echo "Please confirm/cancel system suspension:"
    (
    sleep 10 &&
        if [ $flag_cancel -eq 0 ]; then
        echo "System suspending..."
        systemctl suspend
        fi &
    )
    select confirmation in "confirm" "cancel"; do
    case ${confirmation} in
        confirm )
        echo "System suspending..."
        systemctl suspend
        break;;
        cancel )
        flag_cancel=1
        echo "Canceled suspension."
        break;;
    esac
    done
}

but a change of the value of flag_cancel is not taken into account, so the command is always executed after the sleep.

How to achieve what I want?

For zsh, see the EDIT section in the accepted answer


Let's say I have the following in my ~/.bash_aliases to ask for confirmation before suspending the system:

function suspend()
{ #
    echo "Please confirm/cancel system suspension:"
    select confirmation in "confirm" "cancel"; do
        case ${confirmation} in
        confirm )
            echo "System suspending..."
            systemctl suspend
            break;;
        cancel )
            echo "Canceled suspension."
            break;;
        esac
    done
}

I would like systemctl suspend to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.

I tried the following, with a backgrounded sleep in a subshell:

function suspend()
{ #
    flag_cancel=0
    echo "Please confirm/cancel system suspension:"
    (
    sleep 10 &&
        if [ $flag_cancel -eq 0 ]; then
        echo "System suspending..."
        systemctl suspend
        fi &
    )
    select confirmation in "confirm" "cancel"; do
    case ${confirmation} in
        confirm )
        echo "System suspending..."
        systemctl suspend
        break;;
        cancel )
        flag_cancel=1
        echo "Canceled suspension."
        break;;
    esac
    done
}

but a change of the value of flag_cancel is not taken into account, so the command is always executed after the sleep.

How to achieve what I want?

Let's say I have the following in my ~/.bash_aliases to ask for confirmation before suspending the system:

function suspend()
{ #
    echo "Please confirm/cancel system suspension:"
    select confirmation in "confirm" "cancel"; do
        case ${confirmation} in
        confirm )
            echo "System suspending..."
            systemctl suspend
            break;;
        cancel )
            echo "Canceled suspension."
            break;;
        esac
    done
}

I would like systemctl suspend to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.

I tried the following, with a backgrounded sleep in a subshell:

function suspend()
{ #
    flag_cancel=0
    echo "Please confirm/cancel system suspension:"
    (
    sleep 10 &&
        if [ $flag_cancel -eq 0 ]; then
        echo "System suspending..."
        systemctl suspend
        fi &
    )
    select confirmation in "confirm" "cancel"; do
    case ${confirmation} in
        confirm )
        echo "System suspending..."
        systemctl suspend
        break;;
        cancel )
        flag_cancel=1
        echo "Canceled suspension."
        break;;
    esac
    done
}

but a change of the value of flag_cancel is not taken into account, so the command is always executed after the sleep.

How to achieve what I want?

Reference to an edit to the accepted answer
Source Link
Giuseppe
  • 215
  • 2
  • 8

For zsh, see the EDIT section in the accepted answer


Let's say I have the following in my ~/.bash_aliases to ask for confirmation before suspending the system:

function suspend()
{ #
    echo "Please confirm/cancel system suspension:"
    select confirmation in "confirm" "cancel"; do
        case ${confirmation} in
        confirm )
            echo "System suspending..."
            systemctl suspend
            break;;
        cancel )
            echo "Canceled suspension."
            break;;
        esac
    done
}

I would like systemctl suspend to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.

I tried the following, with a backgrounded sleep in a subshell:

function suspend()
{ #
    flag_cancel=0
    echo "Please confirm/cancel system suspension:"
    (
    sleep 10 &&
        if [ $flag_cancel -eq 0 ]; then
        echo "System suspending..."
        systemctl suspend
        fi &
    )
    select confirmation in "confirm" "cancel"; do
    case ${confirmation} in
        confirm )
        echo "System suspending..."
        systemctl suspend
        break;;
        cancel )
        flag_cancel=1
        echo "Canceled suspension."
        break;;
    esac
    done
}

but a change of the value of flag_cancel is not taken into account, so the command is always executed after the sleep.

How to achieve what I want?

Let's say I have the following in my ~/.bash_aliases to ask for confirmation before suspending the system:

function suspend()
{ #
    echo "Please confirm/cancel system suspension:"
    select confirmation in "confirm" "cancel"; do
        case ${confirmation} in
        confirm )
            echo "System suspending..."
            systemctl suspend
            break;;
        cancel )
            echo "Canceled suspension."
            break;;
        esac
    done
}

I would like systemctl suspend to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.

I tried the following, with a backgrounded sleep in a subshell:

function suspend()
{ #
    flag_cancel=0
    echo "Please confirm/cancel system suspension:"
    (
    sleep 10 &&
        if [ $flag_cancel -eq 0 ]; then
        echo "System suspending..."
        systemctl suspend
        fi &
    )
    select confirmation in "confirm" "cancel"; do
    case ${confirmation} in
        confirm )
        echo "System suspending..."
        systemctl suspend
        break;;
        cancel )
        flag_cancel=1
        echo "Canceled suspension."
        break;;
    esac
    done
}

but a change of the value of flag_cancel is not taken into account, so the command is always executed after the sleep.

How to achieve what I want?

For zsh, see the EDIT section in the accepted answer


Let's say I have the following in my ~/.bash_aliases to ask for confirmation before suspending the system:

function suspend()
{ #
    echo "Please confirm/cancel system suspension:"
    select confirmation in "confirm" "cancel"; do
        case ${confirmation} in
        confirm )
            echo "System suspending..."
            systemctl suspend
            break;;
        cancel )
            echo "Canceled suspension."
            break;;
        esac
    done
}

I would like systemctl suspend to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.

I tried the following, with a backgrounded sleep in a subshell:

function suspend()
{ #
    flag_cancel=0
    echo "Please confirm/cancel system suspension:"
    (
    sleep 10 &&
        if [ $flag_cancel -eq 0 ]; then
        echo "System suspending..."
        systemctl suspend
        fi &
    )
    select confirmation in "confirm" "cancel"; do
    case ${confirmation} in
        confirm )
        echo "System suspending..."
        systemctl suspend
        break;;
        cancel )
        flag_cancel=1
        echo "Canceled suspension."
        break;;
    esac
    done
}

but a change of the value of flag_cancel is not taken into account, so the command is always executed after the sleep.

How to achieve what I want?

Became Hot Network Question
edited body
Source Link
Giuseppe
  • 215
  • 2
  • 8
Loading
Source Link
Giuseppe
  • 215
  • 2
  • 8
Loading