Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 24 Oct 2014, 14:19
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How can I read from file? (Solved)
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
oldyeller


Joined: 15 Nov 2011
Posts: 812
Location: Mishawaka IN

PostPosted: Wed 21 May 2014, 00:46    Post_subject:  How can I read from file? (Solved)  

Hello Everyone,

How can I have this read from a file and not have to do a string? I have four margins to do. It would be a lot easier just to do a file instead of doing this for all four.

Code:

TOPMARGIN=" "1" " 2" "3" "36" "
COMBOLIST2=""
for M in $TOPMARGIN
do
 COMBOLIST2="$COMBOLIST2<item>$M</item>"
done

Edited_time_total
Back to top
View user's profile Send_private_message 
dejan555


Joined: 30 Nov 2008
Posts: 2686
Location: Montenegro

PostPosted: Wed 21 May 2014, 08:26    Post_subject:  

Code:
for M in $(cat /path/to/file);do ...

_________________


Back to top
View user's profile Send_private_message Visit_website MSNM 
oldyeller


Joined: 15 Nov 2011
Posts: 812
Location: Mishawaka IN

PostPosted: Wed 21 May 2014, 10:34    Post_subject:  

Hi dejan555,

Thanks that did work. I have been doing a lot of studying on bash just didn't think about that one. I guess the more I do code the more I will learn.

I do have another issue How can I keep the settings once they have been set. Would I need a config file for this and how would I do it?

Cheers
Back to top
View user's profile Send_private_message 
dejan555


Joined: 30 Nov 2008
Posts: 2686
Location: Montenegro

PostPosted: Wed 21 May 2014, 10:49    Post_subject:  

Not sure what exactly do you need to do.
Settings for some variables, paths? That can be changed?
You can define defaults at the begining of your script you can also include config file like this:
Code:
 . $HOME/.someconfigfile


You can export settings something like
Code:
echo "VAR1=\"$VAR1\" " > $HOME/.someconfigfile
echo "VAR2=\"$VAR2\" " >> $HOME/.someconfigfile


And yep, I learn by doing too, never actually studied commands when I didn't need them.

_________________


Back to top
View user's profile Send_private_message Visit_website MSNM 
amigo

Joined: 02 Apr 2007
Posts: 2261

PostPosted: Wed 21 May 2014, 13:10    Post_subject:  

Actually, the best way to do that is by redirection:
Code:
while read LINE ; do
 # your actions here
done < /path/to/file


Using cat inside for is a waste of using cat and if there are lines in the file which have entries with spaces in them, the for loop will break along the spaces and not the lines.
Back to top
View user's profile Send_private_message 
oldyeller


Joined: 15 Nov 2011
Posts: 812
Location: Mishawaka IN

PostPosted: Wed 21 May 2014, 19:27    Post_subject:  

amigo wrote:
Actually, the best way to do that is by redirection:
Code:
while read LINE ; do
 # your actions here
done < /path/to/file


Using cat inside for is a waste of using cat and if there are lines in the file which have entries with spaces in them, the for loop will break along the spaces and not the lines.


for this code how would I apply it?
Code:

COMBOLIST2=""
for M in $(cat /$WORKDIR/margins);
do
 COMBOLIST2="$COMBOLIST2<item>$M</item>"
done


Cheers
Back to top
View user's profile Send_private_message 
dejan555


Joined: 30 Nov 2008
Posts: 2686
Location: Montenegro

PostPosted: Thu 22 May 2014, 02:52    Post_subject:  

Instead a for loop it would be a while loop

Code:
while read LINE; do
COMBOLIST2="$COMBOLIST2<item>$LINE</item>"
done < /$WORKDIR/margins


but instead of 1 2 3 as individual items it would take a whole line as variable using like this
so if you need individual you would need to break $LINE in another loop I guess

Code:
while read LINE; do
for M in $LINE;do
COMBOLIST2="$COMBOLIST2<item>$M</item>"
done
done < /$WORKDIR/margins


amigo will correct me if wrong

_________________


Back to top
View user's profile Send_private_message Visit_website MSNM 
oldyeller


Joined: 15 Nov 2011
Posts: 812
Location: Mishawaka IN

PostPosted: Thu 22 May 2014, 14:08    Post_subject:  

I have used the less command in place of cat, I suppose I could also use cut as well. Just thinking of different ways on how to do this. any other Ideas?


Cheers
Back to top
View user's profile Send_private_message 
slavvo67

Joined: 12 Oct 2012
Posts: 442
Location: The other Mr. 305

PostPosted: Thu 22 May 2014, 15:14    Post_subject:  

All:

This script has me curious. Can someone explain maybe with a real example as to what it does?

Thanks,


Slavvo67
Back to top
View user's profile Send_private_message 
dejan555


Joined: 30 Nov 2008
Posts: 2686
Location: Montenegro

PostPosted: Thu 22 May 2014, 15:43    Post_subject:  

This is just a part of his code, seems like he needs to build list of items to choose from in gtk combobox.

oldyeller, do you need separate words/numbers from file as items or whole lines?
Not sure how exactly your file looks or what's the full combobox that you need to get but cat or while loop should do the work depending on what you need.

_________________


Back to top
View user's profile Send_private_message Visit_website MSNM 
musher0


Joined: 04 Jan 2009
Posts: 4256
Location: Gatineau (Qc), Canada

PostPosted: Thu 22 May 2014, 16:38    Post_subject:  

Hi, guys.

Here's my take on oldyeller's problem:
Code:
#!/bin/sh
# essai-oldyeller.sh # Using array instead of string.
####
declare -a TOPMARGIN=(1 2 3 36)
COMBOLIST2=""
M="0"
for i in `seq ${#TOPMARGIN[@]}`;do
   COMBOLIST2="<item>\"${TOPMARGIN[$M]}\"</item>"
   echo $COMBOLIST2
   M="`expr $M + 1`"
done


Result is shown in attached pic.

Judging from the name of the variable, I'd say oldyeller is trying to place the
"top margin" Smile of some window on the screen! Smile

I hope this helps. BFN.

musher0
~~~~~~~~~~
Edit - A couple of remarks.
1) The line < echo $COMBOLIST2 > is for checking during test. Remove when script
checks out ok or when not needed anymore.

2) The seq command is a spoiled little brat: it hates the zero (0). I had to use a
workaround, since arrays are counted starting from zero.
essai-oldyeller.jpg
 Description   
 Filesize   31.07 KB
 Viewed   117 Time(s)

essai-oldyeller.jpg


_________________
"Logical entities must not be multiplied needlessly." / "Il ne faut pas multiplier les êtres logiques inutilement." (Ockham)
Back to top
View user's profile Send_private_message Visit_website 
oldyeller


Joined: 15 Nov 2011
Posts: 812
Location: Mishawaka IN

PostPosted: Thu 22 May 2014, 19:43    Post_subject:  

Hello Everyone,

What this code is going to help me with is printing. I have already got it done, just wanted to see a better way to code the variable. I will try this one out as well musher0.

I have gotten printing setup for the editor that I made a few years ago for Manna Bible Software. When all is done and looking how I see it; it will be available for anyone to use.

Cheers
Back to top
View user's profile Send_private_message 
musher0


Joined: 04 Jan 2009
Posts: 4256
Location: Gatineau (Qc), Canada

PostPosted: Thu 22 May 2014, 20:11    Post_subject:  

oldyeller wrote:
Hello Everyone,

What this code is going to help me with is printing. I have already got it done, just wanted to see a better way to code the variable. I will try this one out as well musher0.

I have gotten printing setup for the editor that I made a few years ago for Manna Bible Software. When all is done and looking how I see it; it will be available for anyone to use.

Cheers


No worries, oldyeller. Best of luck! Smile

musher0

_________________
"Logical entities must not be multiplied needlessly." / "Il ne faut pas multiplier les êtres logiques inutilement." (Ockham)
Back to top
View user's profile Send_private_message Visit_website 
oldyeller


Joined: 15 Nov 2011
Posts: 812
Location: Mishawaka IN

PostPosted: Fri 23 May 2014, 04:16    Post_subject:  

I did some more studying on the for command and this is what I did.

Code:

COMBOLIST2=""
for M in {1..72}
do
 COMBOLIST2="$COMBOLIST2<item>$M</item>"
done


Thanks to everyone who helped.

Cheers
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0831s ][ Queries: 13 (0.0090s) ][ GZIP on ]