How to automatically create variables from user input in ksh?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to automatically create variables from user input in ksh?
# 1  
Old 10-11-2011
How to automatically create variables from user input in ksh?

I need some help to write a ksh script.

My code so far (pretty bad, sorry):
Code:
#! /bin/ksh
echo "Calculate average"
UserDecision=y
while test $UserDecision = y
do
echo "Enter a number: " 
read Number1
echo "Enter a number: "
read Number2
echo "Do you want to enter another number? (Y/N)"
read UserDecision
case $UserDecision in
"y" ) echo "Enter a number: "
                 read $Number ;;

"n" )  Total=$((Number1 + Number2))
        Average=$($Total / 3" | bc) ;;      
echo "The average is $Average" ;;
esac
done


Last edited by johnagar; 10-12-2011 at 09:25 AM..
# 2  
Old 10-11-2011
Welcome to the forum.

You are writing the script. So you must know how many times you want the user to enter the data. You would have to handle that inside the script anyway.

The script wont proceed further until it receive the expected number of "newlines" mentioned by read "variables".
# 3  
Old 10-11-2011
If all you're doing is calculating the average then you don't need each input once it has been processed - just the running total and the number of inputs.

A quick (untested!) example:
Code:
Total=0
Inputs=0
while true
do
   echo -n "Number or q :"
   read UserDecision
   case $UserDecision in
      [0-9]*) Total=`expr $Total + $UserDecision`; : $((Inputs++));;
      *) break;;
   esac
done

Average=`expr $Total / $Inputs`
echo $Inputs $Average

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automaticaly create function based off user input

I am trying to create a bash script that will create new function by using the user input. The below will create the necessary files in the correct format, however when it comes to the # create function I am at a loss. If the name entered was NEWNAME and the genes were GENE1,GENE2 then two files... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

Doing math using user defined input and system environment variables

Hi, I need some help to setup some environmental variables. for example...Get A -> userdefined/user input B -> a number. c -> system variable...for example $GETCONF PAGE_SIZE E = do some math using bc display a message "The value is E" setup the system/kernel paramter sysctl -p... (3 Replies)
Discussion started by: saravanapandi
3 Replies

5. UNIX for Advanced & Expert Users

Help with ksh script to list, then cp files from a user input date range

Hi, I'm quite new to ksh scripting, can someone help me with this. Requirements: I need to create a script that list the files from a user input date range. e. g. format of file: *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00*... (1 Reply)
Discussion started by: chococrunch6
1 Replies

6. Shell Programming and Scripting

create an array which can store the strings from the user input in shell script

I want to create an array which can store the strings from the user input in shell script . example :- I want to store the 5 fruits name in a single array which the user provides . (1 Reply)
Discussion started by: Pkast
1 Replies

7. Shell Programming and Scripting

no chance to input passwd when create new user in loop

Hi Dears, I have one script to create new users with information in one plain text file. This script will read all lines in the file and create one users for one line. Sample file: #action;login,full name title,expire date,project +;gmwen,Bruce Wen QA,04/01/2012,BT +;xxdeng,Shown Deng... (4 Replies)
Discussion started by: crest.boy
4 Replies

8. Shell Programming and Scripting

every time user input create array perl

Hi, How to create array every time user input and store user input and display all array print " Enter input " my @input = split(' ', $input) chmop($input = <STDIN>; foreach ($input) { @array= @input; } print @array"\n"; (1 Reply)
Discussion started by: guidely
1 Replies

9. Shell Programming and Scripting

Create a multi user input form

Hi All, Please ignore if terminology used is incorrect as I am new to Unix. I want to create a Multi user input form which looks something like this: ABCD TOOL Logged User: abcd12 ... (4 Replies)
Discussion started by: vidhu0007
4 Replies

10. UNIX for Advanced & Expert Users

Expanding Variables in User Input

If have var='$variable' how can I expand $variable. I have tried many thing like duble quotes/braces etc, but nothing worked. I need the solution ASAP. (2 Replies)
Discussion started by: Bsk
2 Replies
Login or Register to Ask a Question