LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-24-2015, 01:08 PM   #1
dr.x
Member
 
Registered: Jan 2013
Posts: 167

Rep: Reputation: Disabled
bash script to get userinput and save it line by line to variables


Hi
i had my own script that do partial scripting

but i will need a userinput values as below :
the user will paste IPS one shot like :

1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4

then press enter

i want a scipt adaptable so that it get all input from user and save each ip in a variable such as ip1 , ip2 ip3 ......and so on based on the lines (ip per line)


agian , the user will paste all ips one shot.

i hope to get help

cheers
 
Old 04-24-2015, 01:35 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,606

Rep: Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494
so what have you done so far? where is your script? The user did copy&paste those four lines, that means he did copy&paste the enters at the end of lines too (so that comes together with the data).
 
1 members found this post helpful.
Old 04-24-2015, 02:04 PM   #3
dr.x
Member
 
Registered: Jan 2013
Posts: 167

Original Poster
Rep: Reputation: Disabled
I SKPEED THE USER INPUT
and will depend on file.txt

my scipt is :


#!/bin/sh
#echo Hello, who am I talking to?
#read varname
#echo It\'s nice to meet you $varname
#####################
while read line;
do
(( COUNTER++ ))
X=echo $(line)
D"$COUNTER"=$(X)
done < file.txt

but i have error when run it

[root@squid ~]# bash abc.sh
abc.sh: line 9: line: command not found
abc.sh: line 10: X: command not found
abc.sh: line 10: D1=: command not found
abc.sh: line 9: line: command not found
abc.sh: line 10: X: command not found
abc.sh: line 10: D2=: command not found
abc.sh: line 9: line: command not found
abc.sh: line 10: X: command not found
abc.sh: line 10: D3=: command not found
abc.sh: line 9: line: command not found
abc.sh: line 10: X: command not found
abc.sh: line 10: D4=: command not found
abc.sh: line 9: line: command not found
abc.sh: line 10: X: command not found
abc.sh: line 10: D5=: command not found
abc.sh: line 9: line: command not found
abc.sh: line 10: X: command not found
abc.sh: line 10: D6=: command not found
abc.sh: line 9: line: command not found
abc.sh: line 10: X: command not found
abc.sh: line 10: D7=: command not




# cat file.txt
1.1.1.1
2.2.2.2
1.1.1.1
2.2.2.2
2.1.1.2
1.2.1.2
4.4.5.2





ANY HELP ???
as u will D1 , D2 , D3 will be my variables .......

cheers
 
Old 04-24-2015, 03:31 PM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,606

Rep: Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494Reputation: 3494
ok, some help:
use ${NAME} instead of $(NAME)

echo is not required, you can simply write in line 10:
X="$line"

line 1, use bash instead of sh (sh does not understand that syntax):
#!/bin/bash

handling of D${COUNTER} as a new variable (like D1, D2 or similar) will not work that way, you need to use another syntax, you can try to use an array.

Here is a link to explain other possibilities:
http://stackoverflow.com/questions/1...-array-in-bash
 
1 members found this post helpful.
Old 04-24-2015, 06:18 PM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,657

Rep: Reputation: 661Reputation: 661Reputation: 661Reputation: 661Reputation: 661Reputation: 661
Use array like this:
Code:
u0_a149@android:/ $ d[1]=1111
u0_a149@android:/ $ d[2]=2222
u0_a149@android:/ $ echo ${d[1]}
1111
u0_a149@android:/ $ echo ${d[2]}
2222
u0_a149@android:/ $ cnt=123
u0_a149@android:/ $ d[$cnt]=nnnnn
u0_a149@android:/ $ echo ${d[$cnt]}
nnnnn
Also please use code tags around your code to make it easier to read
 
1 members found this post helpful.
Old 04-25-2015, 03:19 AM   #6
dr.x
Member
 
Registered: Jan 2013
Posts: 167

Original Poster
Rep: Reputation: Disabled
thankx ,
but agian wts wrong i have ?


#!/bin/bash
while read line;
do
(( COUNTER++ ))
X=${line};
${Unix_Array[${counter}]}=$line;
done < file.txt



[root@squid ~]# bash abc.sh
abc.sh: line 11: =1.1.1.1: command not found
abc.sh: line 11: =2.2.2.2: command not found
abc.sh: line 11: =1.1.1.1: command not found
abc.sh: line 11: =2.2.2.2: command not found
abc.sh: line 11: =2.1.1.2: command not found
abc.sh: line 11: =1.2.1.2: command not found
abc.sh: line 11: =4.4.5.2: command not found



??


cheers
 
Old 04-25-2015, 05:02 AM   #7
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
${Unix_Array[${counter}]}=$line;
Change this to:

Code:
Unix_Array[${COUNTER}]=$line;
Remember variables are case sensitive.
 
1 members found this post helpful.
Old 04-25-2015, 08:47 AM   #8
dr.x
Member
 
Registered: Jan 2013
Posts: 167

Original Poster
Rep: Reputation: Disabled
Thankx All Guys
you were so helpful

appreciate your time
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 07:07 PM
[SOLVED] (BASH) How to assign 2 variables to same line? Dick Dastardly Programming 10 01-15-2012 10:17 AM
[SOLVED] Bash script to read line by line and execute commands Striketh Programming 4 11-06-2011 12:38 PM
[SOLVED] Simple Modify of Variables in bash file + Inject one line. Dimitriy Programming 18 05-03-2011 04:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration