Read input in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read input in shell script
# 1  
Old 06-17-2015
Read input in shell script

I want to write a script that read the input to variable.
I want the input screen to have 2 lines, where the values already input will appear above the input line
for example if I want to input the words below:
Code:
like
love
live
life

The screen will display like this:
1. Before any input
Code:
Input words:

2. after entering
Code:
like

:
Code:
like 
Input words:

3. after entering
Code:
life

:
Code:
like love live life
Input words:

I have tried the following but it keeps giving multiple lines, any ideas?
Code:
#!/usr/bin/ksh
n=2
p=""
while [[ $n != "x" ]]
do
    echo "Input words: \c"
    read n
    p=$p" "$n
echo $p
done


Last edited by aydj; 06-17-2015 at 09:29 AM..
# 2  
Old 06-17-2015
You'll need man console_codes to position the cursor arbitrarily on the screen, but be aware that not all systems and terminal emulators accept these. Alternatively, try man termcap / terminfo.
You should prefer printf over echo, btw.
# 3  
Old 06-17-2015
Hiya

Code:
output=""
input=""
while
    echo "$output"
    read input
do output+=" $input"
    # Well this goes forever until you hit CTRL+C
done

hth
# 4  
Old 06-17-2015
Thanks you for your effort, but I dont want multiple lines on the screen.
# 5  
Old 06-17-2015
OSX 10.7.5, default bash terminal...
Code:
#!/bin/bash
n=2
p=""
while [[ $n != "x" ]]
do
    read -p "Input words:- " n
    clear
    p=$p" "$n
    echo "$p "
done

This will print on the top line...

EDIT:
I have just noticed it is calling ksh, apologies chaps...
The bash version works...

Last edited by wisecracker; 06-17-2015 at 09:22 AM.. Reason: See above...
# 6  
Old 06-17-2015
I dont want my screen to look like this:
Code:
Input words: like
like
Input words: love
like love
Input words: live
like love live
Input words: life
like love live life
Input words:

I want it to look like this after :
Code:
ike love live life
Input words:

# 7  
Old 06-17-2015
I thought you said the bash version worked?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script does not read input file

Hi everyone, I have problems with this script. This script should check for a folder for each server in the list of the list.txt file. The script only checks the first host, and then exits, why? #!/bin/bash file='/etc/list.txt' while read line; do echo $line if ssh root@$line "stat /var >... (2 Replies)
Discussion started by: nashrik
2 Replies

2. Shell Programming and Scripting

How to get the shell script to read the .txt file as an input/data?

i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt... (11 Replies)
Discussion started by: Gurdza32
11 Replies

3. Shell Programming and Scripting

Shell read command is not waiting for user input

Hi, i am working on one automation , for that i have writing one shell program that take user input in "while read line" block. but read command is taking value that is readed by While block. while read line; do command 1; command 2 echo -n "Do you want to continute > " read rsp... (2 Replies)
Discussion started by: ranvijaidba
2 Replies

4. Shell Programming and Scripting

Read input from file and pass it to sub shell

i have a scenario where in i have to monitor jobs which run in different servers, The job details(job name, host server, etc) are present in a dat file. I have written a script a script which reads the details from the dat file and calls a local method where the job monitoring logic is present.... (2 Replies)
Discussion started by: abubucker0
2 Replies

5. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

6. Shell Programming and Scripting

Read input and output redirection filename within a script

Hello everyone, My requirement is that within a script I need to construct the command line exactly that it was invoked with. For example : sh a.sh arg1 arg2 arg3 < input.txt > output.txt Now within a.sh, I construct a file which has these contents " sh a.sh arg1 arg2 arg3 < input.txt >... (8 Replies)
Discussion started by: hedonist12
8 Replies

7. Shell Programming and Scripting

need shell or Perl script to read multiple input

I need shell 0r Perl script to read multiple input and do something and come out example: echo “ enter the host names separated by space “ read servers foreach @servers { do do something done} Here host names like host1 host2 host3 . . . . . . . so on Please help me... (8 Replies)
Discussion started by: sreedhargouda
8 Replies

8. Shell Programming and Scripting

Need a script that will read from 2 input file

Hi Everyone. I am new in this scripting world. I need to know about script that I can use on linux/sun operating systems. I have 2 input file. File 1: Its has an header information and then data in 2 different columns. File 2 : It has data in 9 different columns/ I need an script that... (3 Replies)
Discussion started by: syahmed
3 Replies

9. Shell Programming and Scripting

How can I send the input of a read line command through a shell script

Hi All, I wish to automate the unix command 'su' through a shell script. I would like to pass the content of a file as password to 'su' command. My script is as below, #! /bin/sh su userA while read line do rpm -ivh $line done < pwd.txt where pwd.txt contains the password of... (6 Replies)
Discussion started by: little_wonder
6 Replies

10. Shell Programming and Scripting

read a file as input and pass each line to another script

Hi, I am trying to write a ftp script which will read a file for filenames and ftp those files to another server. Here's my ftp script, but it's scanning the current directory for file names. My question is how can I pass multiple files (these files will have the name of data files that need to... (0 Replies)
Discussion started by: sajjad02
0 Replies
Login or Register to Ask a Question