Open In App

Shell Scripting – Talk Command

Last Updated : 08 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The use of Shell scripting can simplify repetitive tasks and enable more complex operations with minimal code. Shell commands vary in syntax, with “talk” being a command to facilitate communication among users within the same network. Proper knowledge of the shell, OS, and available commands is necessary for effective shell scripting. Familiarity with regular expressions, which are used to match and manipulate text, is also essential. Shell scripting is often utilized with other tools and technologies, such as programming languages, web servers, and databases. In Unix-based systems, the talk command initiates two-way conversations between users.

Dependency For Ubuntu

The “talk” command was last available in Ubuntu 14.04 LTS, which was released in April 2014. In Ubuntu 14.04, the “talk” command could still be installed and used from the repository. Starting from Ubuntu 16.04 LTS (Xenial Xerus) and subsequent versions, the “talk” command is no longer included in the default installation, and it has been replaced by more modern communication tools.

Dependency For RED HAT

In Red Hat Enterprise Linux 6, the “talk” command could still be installed and used from the repository. However, starting from Red Hat Enterprise Linux 7 and subsequent versions, including the latest version RHEL 8, the “talk” command is not included in the default installation and is no longer available in the official repositories. If you are using a recent version of Red Hat Enterprise Linux, such as RHEL 7 or RHEL 8, or RHEL 9 the “talk” command is not available by default.

What is Talk Command

The talk command comes in handy when we need to communicate with different users on the same network. by using them in shell scripts, we can automate tasks like sending notifications for work or any remainder for submission to a bunch of users at the same time. Whether you’re using it for simple conversations or more advanced shell scripts, the talk command is an essential tool for any Unix-based system administrator. Shell scripts are very useful in automating tasks. we can also do some projects based on shell scripts and talk commands.

Syntax of `talk` command in shell scripting

talk user@host

Where “user” is the username of the person you want to talk to and “host” is the hostname or IP address of the computer they are logged into.

Usage of Talk Command

For example, to talk with someone named John who is logged in with the hostname example.com, you can use the command mentioned below:

talk john@example.com

 

Once the command is executed, a new window will open, allowing you to communicate with the other user in real-time. The conversation will continue until one of the users closes the window or types “exit”. Talk command can also be used with some options, some of which are mentioned below:

Options Description
-f used to initiate the conversation in full-screen mode.
-n used to prevent the other user from being notified when the conversation is initiated.
-w used to set the width of the conversation window.
-W used to set the width of the conversation window to the entire width of the screen.

For example, if we want to initiate a full-screen conversation with a user named “John” without sending them a notification, we would use the following command:

talk -fn john@example.com

 

Talk Command in Shell Script

The talk command can also be used in shell scripts to perform automated tasks, such as sending notifications to other users or initiating conversations with specific users. For example, the following script can be used to send a notification to all users on a network.

Send a notification to all users on the same network

This script uses an array to store a list of users and then uses a loop to iterate through the array, sending a notification to each user using the talk command.

Script:

This script will send a notification message, “This is a notification from the system.” to the email addresses user1@example.com, user2@example.com, and user3@example.com using the ‘talk’ command. The script will not produce any output on the command line.

// below line is called a shebang, and it tells the shell that this script should be run using the Bash interpreter.

#!/bin/bash        

// below line defines an array called users with three elements: “user1”, “user2”, and “user3”.                

users=( “user1” “user2” “user3” )    

// below line starts a for loop that will iterate over each element in the user’s array. The “${users[@]}” part specifies that the loop should iterate over each element in the array.

for user in “${users[@]}”              

 // below line starts the block of code that should be executed for each iteration of the for loop.

do                      

// below line uses the talk command to send a message to each user’s email address. The -n option tells talk not to wait for a response, and the <<< operator is used to pass the message as input to the talk command. The $user variable is replaced with the current user’s name.                      

talk -n $user@example.com <<< “This is a notification from the system.”  

done

Output:

 

Send a notification to a specific user on a Network

Here is a simple script that uses the “talk” command to send a message to a user named “john” on a machine with the hostname “example.com”:

Script:

The following script initiates a dialogue with a specified user. It first defines a variable, “message,” containing the message you want to convey. Next, it uses the “talk” function along with the user’s username and hostname to begin a dialogue with them. The <<< operator is used to redirect the message variable to the talk function.

#!/bin/bash
# Storing message in variable
message="Hello, this is a message sent using the talk command."
# Use to send the message
talk john@example.com <<< $message

Output:

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads