Page 1 of 1
How can I read from file? (Solved)
Posted: Wed 21 May 2014, 04:46
by oldyeller
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: Select all
TOPMARGIN=" "1" " 2" "3" "36" "
COMBOLIST2=""
for M in $TOPMARGIN
do
COMBOLIST2="$COMBOLIST2<item>$M</item>"
done
Posted: Wed 21 May 2014, 12:26
by dejan555
Code: Select all
for M in $(cat /path/to/file);do ...
Posted: Wed 21 May 2014, 14:34
by oldyeller
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
Posted: Wed 21 May 2014, 14:49
by dejan555
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:
You can export settings something like
Code: Select all
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.
Posted: Wed 21 May 2014, 17:10
by amigo
Actually, the best way to do that is by redirection:
Code: Select all
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.
Posted: Wed 21 May 2014, 23:27
by oldyeller
amigo wrote:Actually, the best way to do that is by redirection:
Code: Select all
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: Select all
COMBOLIST2=""
for M in $(cat /$WORKDIR/margins);
do
COMBOLIST2="$COMBOLIST2<item>$M</item>"
done
Cheers
Posted: Thu 22 May 2014, 06:52
by dejan555
Instead a for loop it would be a while loop
Code: Select all
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: Select all
while read LINE; do
for M in $LINE;do
COMBOLIST2="$COMBOLIST2<item>$M</item>"
done
done < /$WORKDIR/margins
amigo will correct me if wrong
Posted: Thu 22 May 2014, 18:08
by oldyeller
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
Posted: Thu 22 May 2014, 19:14
by slavvo67
All:
This script has me curious. Can someone explain maybe with a real example as to what it does?
Thanks,
Slavvo67
Posted: Thu 22 May 2014, 19:43
by dejan555
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.
Posted: Thu 22 May 2014, 20:38
by musher0
Hi, guys.
Here's my take on oldyeller's problem:
Code: Select all
#!/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"
of some window on the screen!
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.
Posted: Thu 22 May 2014, 23:43
by oldyeller
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
Posted: Fri 23 May 2014, 00:11
by musher0
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!
musher0
Posted: Fri 23 May 2014, 08:16
by oldyeller
I did some more studying on the for command and this is what I did.
Code: Select all
COMBOLIST2=""
for M in {1..72}
do
COMBOLIST2="$COMBOLIST2<item>$M</item>"
done
Thanks to everyone who helped.
Cheers