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.
tmpname=$!
if [ -e $tmpname ] then
rm $tmpname
fi
mysql translator -u root --password=******** -e "SELECT word FROM tagalog ORDER BY RAND() LIMIT 1" | while read line; do echo $line >> $tmpname
tail -n 1 $tmpname
In other words just the last line will be output.
"$!" is the pid of the running process.
Should work. Post again if not .
The extquote shell option must be enabled in order to use the $'string' quoting pattern inside parameter substitution. It should be enabled by default.
Edit: By the way, be careful about falling prey to bash pitfall #8 in your code above.
Last edited by David the H.; 02-20-2011 at 03:08 PM.
Reason: As stated
Actually, looking at it again more carefully, read works on a per-line basis (at least by default), so your problem isn't that the variable holds two lines, it's that the loop is running twice, echoing each line separately. So you'll have to devise some test or filter, or otherwise rewrite your script to avoid the unwanted echo.
Actually, looking at it again more carefully, read works on a per-line basis (at least by default), so your problem isn't that the variable holds two lines, it's that the loop is running twice, echoing each line separately. So you'll have to devise some test or filter, or otherwise rewrite your script to avoid the unwanted echo.
That's why I suggested using the cut command, to try to cut out the characters or columns needed.
Yes, and there are certainly many other options available too, such as grep -v, tail, or even sed or awk. The main thing is that either the line has to be eliminated somehow before the output gets passed to the loop, or some kind of test has to be done to the variable inside the loop to catch it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.