Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

learn linux and unix commands - unix shell scripting

Pressing Enter (bash)

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-07-2009   -   Original Discussion by Yakuzan
Yakuzan's Unix or Linux Image
Yakuzan Yakuzan is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 17 March 2009, 10:49 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Pressing Enter (bash)

Hey, I'm writing this BASH script, I recently started learning BASH after I did Java and I'm pretty new to the syntax.

Anways, what I want to do is simple, I coudn't find the right information though:

Let's say I make a :

read -p "Press ENTER to go back to menu" choice
.....


What is the command that would let me perform this? I need to way for the User to press ENTER, not a silly Y/N. (It's for general accomplishment actually haha).

-Yakuzan

P.S : Example much appreciated!
Sponsored Links
    #2  
Old Unix and Linux 02-07-2009   -   Original Discussion by Yakuzan
ddreggors's Unix or Linux Image
ddreggors ddreggors is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 22 July 2013, 9:20 AM EDT
Location: Florida
Posts: 160
Thanks: 5
Thanked 12 Times in 11 Posts
Try this:
echo -n "Press Enter: "; read MyVar; echo $MyVar

$MyVar will be whatever you type, but just hitting enter will print nothing
Sponsored Links
    #3  
Old Unix and Linux 02-07-2009   -   Original Discussion by Yakuzan
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts
Quote:
Originally Posted by Yakuzan View Post
Hey, I'm writing this BASH script, I recently started learning BASH after I did Java and I'm pretty new to the syntax.

Anways, what I want to do is simple, I coudn't find the right information though:

Let's say I make a :

read -p "Press ENTER to go back to menu" choice
.....


What is the command that would let me perform this? I need to way for the User to press ENTER, not a silly Y/N. (It's for general accomplishment actually haha).

Your code will work as is.

The script will wait until the user presses ENTER. You might add an -s option to prevent any other characters being echoed to the screen, and you don't need a variable name since you aren't going to check it:



Code:
read -sp "Press ENTER to go back to menu"
echo "Thank you!"

    #4  
Old Unix and Linux 02-07-2009   -   Original Discussion by Yakuzan
ddreggors's Unix or Linux Image
ddreggors ddreggors is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 22 July 2013, 9:20 AM EDT
Location: Florida
Posts: 160
Thanks: 5
Thanked 12 Times in 11 Posts
Wasn't sure if Yakuzan needed to take in a Menu Choice or not...

I was assuming that was the point. Take in choice if given, else just do nothing.

You are correct though either way Yakuzan's original code will work as is and will even store the response in choice if needed.
Sponsored Links
    #5  
Old Unix and Linux 02-07-2009   -   Original Discussion by Yakuzan
Yakuzan's Unix or Linux Image
Yakuzan Yakuzan is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 17 March 2009, 10:49 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Il use this thread to ask most of my questions since my Linux book blows. It only covers basic BASH since it's not a BASH guide.

And since my openSUSE doesn't have internet on it I'm stuck going from one system to another.

Btw, to understand my concept:

1- Show date
2- Move a folder
3- See current location

That is the menu which is a script called ./menu
If the script reads 1 then you get shoot to ./date
Then I ask to press enter to show back the menu(Thanks a lot Linux)
Then back to ./menu

Excetera, each number has it's own script.

As to todays question:

Is there a way to force the user to press enter and nothing else? Does it require an if statement? I saw you mention -s, what exactly do you mean?

P.S Huge thanks to you guys, I'm spending a few hours a day on BASH, Il get it sorted out in no time!
Sponsored Links
    #6  
Old Unix and Linux 02-07-2009   -   Original Discussion by Yakuzan
ddreggors's Unix or Linux Image
ddreggors ddreggors is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 22 July 2013, 9:20 AM EDT
Location: Florida
Posts: 160
Thanks: 5
Thanked 12 Times in 11 Posts
The -p flag is prompt (basically use the following text as the prompt)
The -s flag is silent mode (do not echo characters to the screen)

As to forcing the user to press "Enter" only... why?
No matter what they press it will either be passed to a variable ($choice in you example), or completely discarded as in cfajohnson's example.

Either way Enter was pressed and if you do not use $choice (in you example) the rest doesn't matter.
Sponsored Links
    #7  
Old Unix and Linux 02-07-2009   -   Original Discussion by Yakuzan
Yakuzan's Unix or Linux Image
Yakuzan Yakuzan is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 17 March 2009, 10:49 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
How can I write a command right after an echo. It`s not working:

echo "Date : " `date + %d`

Am I writing it wrong?
Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to have ENTER after each symbol. sparkriver Shell Programming and Scripting 5 01-30-2009 01:21 PM
Enter to OBP jess_t03 Solaris 5 02-13-2008 12:14 PM
read the ENTER key sars Shell Programming and Scripting 4 11-27-2006 08:34 AM
Cant enter password possuman72 Solaris 2 06-10-2005 03:44 PM



All times are GMT -4. The time now is 12:46 AM.