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

  • Thread starter Thread starter jameson2
  • Start date Start date
  • Tags Tags
    Shell
AI Thread Summary
A bash shell script can be designed to output a specific line in a calendar file based on user input, where an "enter" key press triggers the output of today's date. The 'read' command in bash captures user input, allowing the script to check if the input is empty. If the input is empty, the script can execute a command to print today's date; otherwise, it can process the entered date. However, determining if the input is a valid date can be complex in bash, leading some users to suggest using other programming languages like Python or Perl for better handling. Overall, the discussion emphasizes the simplicity of checking for empty input while highlighting the challenges of validating date formats in bash.
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...
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top