ProgrammingThis 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.
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.
Hi all,
I'm trying to write a [really] simple bash script that reads in a series of words, one per line, from a file "Submissions" and loads them into an array which I can then iterate through.
So far the script looks mainly like
Code:
rawfile=`cat Submissions`
array=__do something to $rawfile, possibly involving magic__
for subid in $array; do
x=`db2 "select count(*) from table where uniquekey='$subid'"
if [ "x" == "0" ]; then
echo "No submissions here!"
elif [ "x" == SQL* ]; then
echo "Fix your SQL, silly."
fi
done
I've seen a website which advocates using awk, but the example given is a hard-coded string and only has 2 entities in it when split, so I can't tell if what's done there is what I'm supposed to be doing here. It certainly didn't work when I tried it Does anyone have a good way to do this?
Do your really need to store it in an array?
If not, you can do it faster and saving some memory:
Code:
#!/bin/bash
while read subid ; do
x=`db2 "select count(*) from table where uniquekey='$subid'"
if [ "x" == "0" ]; then
echo "No submissions here!"
elif [ "x" == SQL* ]; then
echo "Fix your SQL, silly."
fi
done <Submissions
Hmm. Well, I suppose that I don't, really. Doing it that way, how does one open the file so that it knows to read one token per line? Does it default to reading a line at a time?
I assume that the "<Submissions" has something to do with it?
the read command stores input in a variable until the enter or return key is pressed
so here the read command store input from file Submissions (<Submissions) and
stop to store in subid variable each time it reads a end of line (as a return or enter key)
Basically the code posted by hko should use less memory as it doesn't store all the
Submission lines before work with it, instead it stores line per line in memory
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.