Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-10-2011, 01:32 AM
|
#1
|
Member
Registered: Jan 2011
Posts: 32
Rep:
|
load default value , shell script
i wrote an script that configure files..am thinking to add option that load an default values in file , procedure :
1.user enter file name
2.check if file is exist
3.check if file readable.
3.read file line by line, and assign each line in variable , so i can used 4 seting up the configuration.
any idea would be fine !
|
|
|
03-10-2011, 01:40 AM
|
#2
|
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
|
Hello,
Any idea about what? On how to proceed? You say you wrote a script so how about posting what you have and commenting what you're missing or where you're in doubt? The more information you provide, the better we can help you.
Kind regards,
Eric
|
|
1 members found this post helpful.
|
03-10-2011, 01:44 AM
|
#3
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
Sounds like some echoes, reads and at least one loop. If this answer is a little too vague it might have something to do with how much or little information you have provided.
|
|
|
03-10-2011, 01:44 AM
|
#4
|
Member
Registered: Jan 2011
Posts: 32
Original Poster
Rep:
|
code
###What is the way for assigning each line for variable.
Quote:
process (){
#(2)# then process each variable.
}
FILE=''
read -p "Please enter name of default file : " FILE
if [ ! -f $FILE ]; then
echo "$FILE : does not exists "
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE : can not read "
fi
exec 0<"$FILE"
while read -r line
do
##(2)#how can i assign each line for an variable
done
|
Last edited by moata_u; 03-10-2011 at 02:00 AM.
|
|
|
03-10-2011, 01:57 AM
|
#5
|
LQ Guru
Registered: Apr 2005
Location: /dev/null
Posts: 5,818
|
Quote:
Originally Posted by moata_u
process (){
#(2)# then process each variable.
}
FILE=''
read -p "Please enter name of default file : " FILE
if [ ! -f $FILE ]; then
echo "$FILE : does not exists "
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE : can not read "
fi
exec 0<"$FILE"
while read -r line
do
##(2)#how can i assign each line for an variable
done
|
???
|
|
|
03-10-2011, 01:58 AM
|
#6
|
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
|
Hello,
Would you mind putting your code in CODE tags for readability please? And what other variables do you want/need/have? You're not making any sense to me. The only variable I can see is for the filename '$FILE'.
Kind regards,
Eric
|
|
1 members found this post helpful.
|
03-10-2011, 02:04 AM
|
#7
|
Member
Registered: Jan 2011
Posts: 32
Original Poster
Rep:
|
my question is how to assign each line to an variable thus , variable i can use it in my code...
ex :
file.txt contain :
192.168.0.20
billz
billing
..etc
* than read file and each line assign to an variable ex:
${variable[0]} = 192.168.0.20
${variable[1]} = billz
${variable[2]} = billing
.
|
|
|
03-10-2011, 02:20 AM
|
#8
|
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
|
Hi,
If you want to loop through a file you can use structures like this:
Code:
cat file.txt | while read line;
do
your commands
done
or another one:
Code:
for i in $(cat file.txt)
do
your commands
done
The 'your commands' part is where the work will be done. Since this 'smells' like some type of homework, let me point you to some good documentation on the subject you need.
Bash Arrays
More directly related to your questions
Kind regards,
Eric
|
|
1 members found this post helpful.
|
03-10-2011, 09:05 AM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
I don't see a reason why your example would not work? Each line of the file will be stored in the parameter 'line' which then can be acted on. Or did I miss something?
|
|
|
All times are GMT -5. The time now is 09:36 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|