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

  • Thread starter jameson2
  • Start date
  • Tags
    Shell
In summary, the conversation discusses the need to create a bash shell script that will output a line in a calendar file based on user input. The script is expected to differentiate between a user entering a specific date and simply hitting "enter" without any input. Suggestions for using the 'read' command and other programming languages are also mentioned.
  • #1
jameson2
53
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
  • #2


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.
 
  • #3


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

1. Can I use the "read" command in a bash shell script to wait for user input?

Yes, the "read" command can be used in a bash shell script to wait for user input. It will pause the script and wait for the user to enter something, and then assign that value to a variable for later use.

2. How can I check if the user has hit the enter key in a bash shell script?

You can use the "read" command with the "-t" option to specify a timeout, and then check the exit status of the command. If the user does not enter anything before the timeout, the exit status will be 1, indicating that the user hit the enter key without entering any input.

3. Is there a way to clear the input buffer before waiting for user input in a bash shell script?

Yes, you can use the "read" command with the "-n" option to specify the number of characters to read. Setting this to a high number (e.g. 100) will clear the input buffer before waiting for user input.

4. How can I make my bash shell script do one thing if the user hits enter, and another thing if the user enters any other input?

You can use the "read" command with the "-t" option to specify a timeout, and then use an "if" statement to check the exit status. If the exit status is 1, the user hit the enter key, so you can execute one set of commands. If the exit status is 0, the user entered something else, so you can execute a different set of commands.

5. Is it possible to customize the behavior of the enter key in a bash shell script?

Yes, you can use the "read" command with the "-s" option to make the user's input invisible and the "-p" option to specify a prompt. This allows you to customize the behavior of the enter key, such as using it to confirm an action or move to the next step in the script.

Similar threads

  • Programming and Computer Science
Replies
1
Views
322
  • Programming and Computer Science
Replies
33
Views
2K
Replies
3
Views
222
  • Programming and Computer Science
Replies
4
Views
261
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
9
Views
819
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
Back
Top