Store user input in differ


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Store user input in differ
# 1  
Old 10-10-2011
Store user input in differ

Hello all

Can anyone help me to solve the below issue

I want to take user input with space separated .The number of inputs can be variable

like if user inputs
1 2 3 4

ouput will stored in as array a[i] where i=4 and I can retreive the value like a[3] =3

any thoughts how to do it

Appreciate your kind help
# 2  
Old 10-10-2011
what language/shell/OS ?
# 3  
Old 10-10-2011
Quote:
Originally Posted by Pratik4891
Hello all

Can anyone help me to solve the below issue

I want to take user input with space separated .The number of inputs can be variable

like if user inputs
1 2 3 4

ouput will stored in as array a[i] where i=4 and I can retreive the value like a[3] =3

any thoughts how to do it

Appreciate your kind help
try justdoit codes Smilie
Code:
# ./justdoit
Please give your value(s) [ 1 2 3 4 ] ? 1 3   5
Please only give an input value for [1 2 3 4] with space!!

Please give your value(s) [ 1 2 3 4 ] ? 1      5
Please only give an input value for [1 2 3 4] with space!!

Please give your value(s) [ 1 2 3 4 ] ? 1             2
Enter the values in 'a space' between them

Please give your value(s) [ 1 2 3 4 ] ? 4 1 3
1.val="4"
2.val="1"
3.val="3"

Code:
## justdoit ## unix.com @ygemici
#!/bin/bash
while :; do
read -p "Please give your value(s) [ 1 2 3 4 ] ? " i
if [ -z "$i" ] ; then echo "Please give an input value for [1 2 3 4] with space!! "
elif [[ $(echo "$i"|sed 's/ /\n/g'|grep -wv '[1-4]') ]] ; then
echo "Please only give an input value for [1 2 3 4] with space!! "
elif [[ $(echo "$i"|grep '  ') ]] ; then
echo "Enter the values in 'a space' between them"
else
ttn=$(echo "$i"|sed 's/ /\n/g'|sed -n '$=')
rrc=${#ttn[@]}
case $rrc in
1|2|3|4)a=($(echo "$i"|sed 's/ /" "/g;s/$/"/;s/^/"/'));;
*)echo "incorrect usage!!" && exit 1;;
esac;break
fi;done
for((j=1;j<=${#a[@]};j++));do
aa[j]=${a[j-1]}
echo "$j.val=${aa[j]}"
done


regards
ygemici

Last edited by ygemici; 10-10-2011 at 02:54 PM.. Reason: code simplify
This User Gave Thanks to ygemici For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

3. Shell Programming and Scripting

List the file names that differ

Hello, I have two directories - prev and current . They both have same multiple subdirectories and files. Now the current directory can have some updated files and some new files added that is not in prev. I want to find the list of file names that differ. I am doing this because i can not... (2 Replies)
Discussion started by: jakSun8
2 Replies

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

5. Shell Programming and Scripting

saving all input name and store them as variables

Hi I want to write a script such that when executed, it will store all input as different variable, for eg ./store.sh name1 name2 name3 name4 will result in $1=name1 $2=name2 $3=name3 etc How do I do that? Thanks. (1 Reply)
Discussion started by: piynik
1 Replies

6. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

7. Solaris

How to differ local disks from attached from storage ones?

Hello. I have a solaris box with several local disks and several come from SYMMETRIX storage. Is there any way to tell format (or other util) to show only local disks? (6 Replies)
Discussion started by: urello
6 Replies

8. Shell Programming and Scripting

How to read outlook mails for a particular user and store that mail description in a excel/notepad

My requirements are :- Remote connection to the another server with log in credential. Read the mail from a particular user with date,subject and contents and store these data in XLs with consecutive columns (date,subject and contents). Mail that XLs to multiple user those are in a mailing... (3 Replies)
Discussion started by: ranjan001
3 Replies

9. Solaris

Why do controllers differ between physical and logical names

When I look at format I can see the following info: AVAILABLE DISK SELECTIONS: 0. c1t0d0 <SUN72G cyl 14087 alt 2 hd 24 sec 424> /pci@1c,600000/scsi@2/sd@0,0 1. c1t1d0 <SUN72G cyl 14087 alt 2 hd 24 sec 424> /pci@1c,600000/scsi@2/sd@1,0 Can anyone tell me... (2 Replies)
Discussion started by: James_UK
2 Replies

10. Shell Programming and Scripting

sftp file size differ

Hi, I have one doubt over sftp. I am trnasferring a file from server1 to server2 using sftp. The size of the file shows different in file 1 and file2 after sftp even though it shows same number of byte transferred. I don't understand the problem. For example: I have file1 having size... (3 Replies)
Discussion started by: siba.s.nayak
3 Replies
Login or Register to Ask a Question