Ask Your Question
0

I want to leave BASH script automatically if it is not executed on a defined hostname

asked 2023-04-14 11:41:55 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-14 11:42:26 +0000

qstack gravatar image

To stop a bash script from running if the hostname is not "x4", you can use the following code snippet at the beginning of your script:

#!/bin/bash

required_hostname="x4"
current_hostname=$(hostname)

if [ "$current_hostname" != "$required_hostname" ]; then
    echo "This script can only be run on the host: $required_hostname"
    exit 1
fi

Rest of your script

This code will compare the current hostname with the required hostname ("x4" in this case). If the hostnames do not match, the script will print an error message and exit with a status code of 1, indicating an error. If the hostnames match, the script will continue executing the following commands.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

1 follower

Stats

Asked: 2023-04-14 11:41:55 +0000

Seen: 13 times

Last updated: Apr 14 '23