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

  • Thread starter Thread starter jameson2
  • Start date Start date
  • Tags Tags
    Shell
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 4K views
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.
 
Physics 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...