Prompting user twice on the same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prompting user twice on the same line
# 1  
Old 01-12-2015
Prompting user twice on the same line

Hi,

I'm trying to prompt twice on the same line but can't make it so far. I can prompt only once per line.

i.e.
Code:
read -p "Enter the two digits of your sum: " NUM1; read -p "+" NUM2

the result of above is:
Code:
Enter the two digits of your sum: 5
+ 7

But I would like it to look something like:
Code:
Enter the two digits of your sum: 5 + 7


Is there a way to accomplish this?


Thanks!

Last edited by rbatte1; 01-13-2015 at 06:25 AM.. Reason: Added CODE tags for output
# 2  
Old 01-12-2015
As a practice example, try (copy paste line by line, not all together):
Code:
read -p "Enter an operation: " NUM1 OP NUM2
echo "$NUM1 $OP $NUM2"
echo $(( $NUM1 $OP $NUM2  ))

Hope this helps
This User Gave Thanks to sea For This Post:
# 3  
Old 01-12-2015
Thanks sea, It gave me some ideas but couldn't use them for what I'm looking for.

What I'm trying to do is to enter automatically the plus sign + after typing NUM1 and pressing enter. So the user would enter just NUM1 and NUM2.

This way, user will enter NUM1, press enter, and with this last action the plus sign + will be placed automat. right after it and at the same time will allow the user to enter the second number NUM2.

Hope I made myself clear.


Thanks Smilie
# 4  
Old 01-12-2015
This is a bit of a mess, but it works here. Might need some tweaking.
Code:
printf "Enter the two digits of your sum: "
## get cursor position
echo -en "\E[6n"
read -sdR posn
posn=${posn#*[}
col=${posn#*;}    # extract column
read NUM1
tput cuu1         # move up, then right
tput cuf $(( $col + 1 ))
printf "+ "
read NUM2
echo "$NUM1 + $NUM2"


Last edited by ongoto; 01-12-2015 at 04:58 PM.. Reason: extraneous ;
These 2 Users Gave Thanks to ongoto For This Post:
# 5  
Old 01-12-2015
Thanks Ongoto, it worked as expected Smilie
# 6  
Old 01-12-2015
If you don't mind typing the first number blind, you could try something like:
Code:
#!/bin/bash
stty -echo
read -p "Enter the two digits of your sum: " NUM1
stty echo
read -p "$NUM1 + " NUM2
echo "The sum of $NUM1 + $NUM2 is $((NUM1 + NUM2))."

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-12-2015
Try also
Code:
echo -en "enter num: \e7"; read N1; echo -en "\e8$N1 + \e7"; read N2; echo -e "\e8$N2 =" $((N1 + N2))
enter num:  12 + 13 = 25

On the last line the terminal will scroll and destroy the effect...
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

SFTP without prompting password

Dear unix experts, i have a requirement as below. i need to use SFTP as FTP. ftp -n -v << ENDFTP open test_ftp.server user ftp_user_name ftp_password quit ENDFTP if i use this in a shell script, it's not asking for password. But i want the similar thing achived using... (5 Replies)
Discussion started by: AraR87
5 Replies

2. Shell Programming and Scripting

Reading ls -l output line by line awk the user name and su user to run commands

Using ksh on AIX what I am trying to do is to read the ls -l output from a file in a do while loop line by line. Extract the user name(3rd field) and the directory/file name(9th field) using awk and save them into variables. su -c to the user and change directory/file permisions to 777. Script I... (13 Replies)
Discussion started by: zubairom
13 Replies

3. Shell Programming and Scripting

Ssh is prompting for password

Hi, When i am trying to connect to other server using ssh coomand, it is prompting for password. But i want to hardcode it with username so that it should not prompt for password. And i dont want to use "ssh-keygen" method as it is not allowed. Please help me. Regards, Mukta (7 Replies)
Discussion started by: Mukta
7 Replies

4. Shell Programming and Scripting

Prompting for password

Hi, I have SVN installed in my UNIX solaris server. I actually automated the process that downloads code from SVN server to UNIX solaris server in script. When i run the script, its asking for password to download every element. Its really difficult to type password for every element when... (3 Replies)
Discussion started by: gthangav
3 Replies

5. Shell Programming and Scripting

su through normal user prompting for password.

I have two users on linux box, say user1 user2.Both the users are having passwords. Now I would like to run the script from user1 and switch to another user i.e., user2 from the script itself. ** I do have limited access and I am running from the normal user account.Not from the root... (2 Replies)
Discussion started by: giridhar276
2 Replies

6. Shell Programming and Scripting

Prompting for file deletion?

I got help in another forum but now I need further help so I figured I'd ask here. I had to write a script to delete certain filenames of certain size. I got this far.. find . -size 110c -name "*testing*" -print | xargs -n 1 rm -i It finds the correct files, but the prompts to delete are all... (2 Replies)
Discussion started by: NycUnxer
2 Replies

7. UNIX for Dummies Questions & Answers

sftp prompting for password

I have the problem with SFTP; BELOW IS the entry from my ssh_config file It's prompting me for password all the time when using SFTP. pLEASE help. (1 Reply)
Discussion started by: dsravan
1 Replies

8. UNIX for Advanced & Expert Users

SSH - Prompting for password

Hi, Can anybody tell me a way to do ssh , without prompting for password from keyboard, Using RSA. The requirement is I need to create the key , using passphrase also..... Is there any way to do it in UNIX ? I am doing it from AIX machine , but remote machine is Linux I tried... (8 Replies)
Discussion started by: shihabvk
8 Replies

9. UNIX for Dummies Questions & Answers

prompting for account that expired

I have this problem. Two accounts in an aix. Account A expired and it would auto prompt for new password when the user failed to log in, but Account B would not prompt for the new password. Instead it will only display "your account is expired. Please contact your administrator". I would like to... (1 Reply)
Discussion started by: mayyap
1 Replies

10. UNIX for Dummies Questions & Answers

not script prompting but,.............

Hi, I am writig this script and usually I have prompts like: echo "Enter file name: " read "$filename" So later when I want verify the name to something I can: if then echo "Hello $filename" Anyways how is this done without prompting for example: I have a script that manages... (3 Replies)
Discussion started by: Astudent
3 Replies
Login or Register to Ask a Question