How to read a line of text from user input?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to read a line of text from user input?
# 1  
Old 03-12-2010
Network How to read a line of text from user input?

Hiii
I wanna a read a line of text from standard input. The user enter data like this way

name phone_no month1_salary month2_salary

that is user enter the name ,phone no and salary of 2 months in a single line by giving spaces. I wanna add the 3rd and 4th fields ...ie add both months' salary...

Code:
i=0
echo "Enter the data :"
while read data
do
    i=`expr i + 1`
    if [ $i -eq 3]
          x=$i   
    if [ $i -eq 4]
         y= $i
    fi
    fi
done

echo "sum is `expr $x + $y`


Last edited by pludi; 03-12-2010 at 04:43 AM.. Reason: code tags, please...
# 2  
Old 03-12-2010
Try this

Code:
echo -n "Enter the data :"
while read data
do
     name=`echo $data|cut -d ' ' -f 1`
     phone=`echo $data|cut -d ' ' -f 2`
     sal1=`echo $data|cut -d ' ' -f 3`
     sal2=`echo $data|cut -d ' ' -f 4`
     echo -n "$name | $phone |"
     echo `expr $sal1 + $sal2`
echo  -n "Enter the data :"
done

# 3  
Old 03-12-2010
Code:
declare -a array
echo "Enter the data";
read data;
count=1;
array[0]=`echo "$data"| cut -d ' ' -f 3 `;
array[1]=`echo "$data"| cut -d ' ' -f 4 `;
expr ${array[0]} + ${array[1]};

# 4  
Old 03-12-2010
Or in a other way

Code:
echo "Enter the data :"
read data
# echo $data
x=`echo $data |awk -F " " '{print  $3 }'`
y=`echo $data |awk -F " " '{print  $4 }'`
echo "sum is `expr $x + $y`"

# 5  
Old 03-12-2010
Code:
while printf "Enter data (ENTER to quit): " && read n p s1 s2 j; do
  [ "$n" ] || break
  echo "Sum: $((s1 + s2))"
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Perl to read user input

I am creating a bash that uses perl . The below code closes before the input is entered. If I run the perl as a .pl it is fine. What am I doing wrong? Thank you :). #!/bin/bash cd 'C:\Users\cmccabe\Desktop\wget' wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv print... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Using read to prompt for editable user input in Bash 3

Below is a simple script to prompt for user input while suggesting an editable default value at the prompt: shortname=user1 read -e -i $shortname -p "Please enter the username you would like to add: " input USERNAME="${input:-$shortname}" Please enter the username you would like to add:... (3 Replies)
Discussion started by: woodson2
3 Replies

4. Homework & Coursework Questions

How to read user keyboard input inside the case?

I need to Write a shell script that allows some system-administration tasks to be preformed automatically from a menu-driven interface. with automated following tasks: Copy directory tree Delete files or directories Output Information (this part is done ) *Copy directory tree The “Copy... (2 Replies)
Discussion started by: femchi
2 Replies

5. Shell Programming and Scripting

Read text file and use it as input

I need to take a text file that holds a bunch of data and run each the stuff in it as an input for the program. the file would hold stuff like this: thing1.awesomesite.com 80 123.456 thing2.awesomesite.com 80 789.098 thing3.awesomesite.com 80 765.432 ... Now I already know the... (1 Reply)
Discussion started by: shade917
1 Replies

6. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

7. Shell Programming and Scripting

Solaris- Read command from user input

I need to write a bourne shell script (solaris 10) that accepts input from the user. The input will be a command- any command like ls/ pwd/ mv etc. After the input is read, the shell must execute the command supplied by the user. I know we use read to play with user inputs. Just not sure how to... (2 Replies)
Discussion started by: PDManc
2 Replies

8. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

9. UNIX for Dummies Questions & Answers

getline to read input from a user

Hi, The gcc compiler has warned about using gets(), so I've been trying my hand at getline. Problem is that I've been able to read from a file, but what I really need is to read from a user's input. I want to use getline like a scanf() command, but I can't figure what to substitute for the fp... (6 Replies)
Discussion started by: sdsd
6 Replies

10. UNIX for Dummies Questions & Answers

read user input from within a wile loop that is being fed from below

hi! i need to do a ksh script that uses a wile loop that is fed form below while read line do some things done < myfile inside the while loop i need to read user input to ask the user what he wants to do, but "read" reads the file, and not the standard input while read line do ... (2 Replies)
Discussion started by: broli
2 Replies
Login or Register to Ask a Question