Greet User Based on Time

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will create a script that greets the user based on the current time. We will use the date command to get the current time and then use cut and tr to extract the relevant information. We will also learn about bash configuration files and how to run a script as soon as we log-on to the system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/clear("`Screen Clearing`") linux/UserandGroupManagementGroup -.-> linux/whoami("`User Identifying`") linux/SystemInformationandMonitoringGroup -.-> linux/date("`Date/Time Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/time("`Command Timing`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/arrays("`Arrays`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-18287{{"`Greet User Based on Time`"}} linux/echo -.-> lab-18287{{"`Greet User Based on Time`"}} linux/clear -.-> lab-18287{{"`Greet User Based on Time`"}} linux/whoami -.-> lab-18287{{"`Greet User Based on Time`"}} linux/date -.-> lab-18287{{"`Greet User Based on Time`"}} linux/time -.-> lab-18287{{"`Greet User Based on Time`"}} shell/shebang -.-> lab-18287{{"`Greet User Based on Time`"}} shell/comments -.-> lab-18287{{"`Greet User Based on Time`"}} shell/quoting -.-> lab-18287{{"`Greet User Based on Time`"}} shell/variables_decl -.-> lab-18287{{"`Greet User Based on Time`"}} shell/variables_usage -.-> lab-18287{{"`Greet User Based on Time`"}} shell/arrays -.-> lab-18287{{"`Greet User Based on Time`"}} shell/cond_expr -.-> lab-18287{{"`Greet User Based on Time`"}} shell/cmd_substitution -.-> lab-18287{{"`Greet User Based on Time`"}} shell/subshells -.-> lab-18287{{"`Greet User Based on Time`"}} shell/globbing_expansion -.-> lab-18287{{"`Greet User Based on Time`"}} end

Say Hello

In this challenge, you are tasked with creating a script that prints greetings based on the current time.

Tasks

You need to complete the following task:

  • Create a script that dynamically outputs a greeting message based on the current time.

Requirements

You must meet the following requirements:

  • The script should run automatically upon logging in to the system.

Example

labex:project/ $ bash sayHello.sh
labex, Have a nice day!!
This is Sunday 14 in May (10:48:37)

Summary

In this challenge, we learned how to use the date command to get the current time and how to extract relevant information using cut and tr. We also learned about bash configuration files and how to run a script as soon as we log-on to the system. This challenge helped us to improve our bash scripting skills and understand how to automate tasks in Linux.

Other Linux Tutorials you may like