Getting bash shell script to do one thing if user hits enter .

  • Thread starter Thread starter jameson2
  • Start date Start date
  • Tags Tags
    Shell
Click For Summary
SUMMARY

The discussion focuses on creating a Bash shell script that outputs a line in a calendar file based on user input. If a date is entered, the script should print that date; if the user simply hits "enter," it should output today's date. The 'read' command in Bash is essential for capturing user input, and checking for an empty input is straightforward. However, validating date formats in Bash is complex, leading some users to suggest using languages like Python or Perl for better handling of date inputs.

PREREQUISITES
  • Understanding of Bash scripting and syntax
  • Familiarity with the 'read' command in Bash
  • Basic knowledge of conditional statements in programming
  • Awareness of date handling in programming languages
NEXT STEPS
  • Learn how to implement conditional statements in Bash scripts
  • Research the 'read' command and its options in Bash
  • Explore date validation techniques in Python
  • Investigate alternative scripting languages like Perl or Tcl for date handling
USEFUL FOR

Bash script developers, programmers looking to handle user input effectively, and anyone interested in automating date-related tasks in shell scripts.

jameson2
Messages
42
Reaction score
0
Getting bash shell script to do one thing if user hits "enter".

I need to write a program that will output a line in a calendar file if a date is entered, or if no date is entered and the user just hits "enter" to ouput the line corresponding to today's date.
I think that it must take the form of an if statement, along the lines of if(date is entered) then print the line, else print today's line. Or maybe if date is entered do such and such, if user hits enter do something else. But I don't know how to translate "user hits enter" into the shell language, or user enters date now that I think about it. Any help would be great. User hit's enter would be the most useful in my opiniono, thanks.
 
Technology news on Phys.org


The bash command 'read' reads from standard input into zero or more shell script variables specified as the targets of the read command. If you specify one variable it will contain the contents of the line entered by the user. Testing whether the entered data is empty (the user just hit return) is easy. Testing whether it is a date, in bash? Do you like to torture yourself? You might want to rethink that and use something like python, perl, tcl, ... -- anything but bash.
 


Well even just something to distinguish between whether the user actually enters any information or just hits enter. I'm fairly new to this...
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
6K
Replies
33
Views
3K
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
7
Views
2K
Replies
1
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K