LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-10-2011, 01:32 AM   #1
moata_u
Member
 
Registered: Jan 2011
Posts: 32

Rep: Reputation: 0
Smile 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 !
 
Old 03-10-2011, 01:40 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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.
Old 03-10-2011, 01:44 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
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.
 
Old 03-10-2011, 01:44 AM   #4
moata_u
Member
 
Registered: Jan 2011
Posts: 32

Original Poster
Rep: Reputation: 0
Smile 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.
 
Old 03-10-2011, 01:57 AM   #5
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by moata_u View Post
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
???
 
Old 03-10-2011, 01:58 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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.
Old 03-10-2011, 02:04 AM   #7
moata_u
Member
 
Registered: Jan 2011
Posts: 32

Original Poster
Rep: Reputation: 0
Cool

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
.
 
Old 03-10-2011, 02:20 AM   #8
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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.
Old 03-10-2011, 09:05 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
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?
 
  


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
[SOLVED] Where does the shell script run by default? timdvtemp Linux - Software 3 03-03-2011 08:49 AM
default value in shell script RudraB Programming 5 03-20-2008 06:58 AM
Shell script to load data to oracle database divyahm Linux - Newbie 2 11-08-2007 09:15 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Opening file with default application from shell script? bourbon_beast Programming 4 04-13-2004 01:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 AM.

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
Open Source Consulting | Domain Registration