LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 01-22-2010, 06:52 PM   #1
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Rep: Reputation: 25
Shell scripting peculiarity


In this example, why does blacklist end up in the file blacklist and $a end up in stdout?

Code:
#!/bin/sh
test= 'lsmod' | 'awk' '/snd_/{print $1}' > audiodrivers

while a=read line
do 
   echo -n "blacklist $a" >> test.txt
done < /root/testscript/audiodrivers
The desired result is to have a file containing the results of lsmod which had the first word on the line beginning with snd_ copied into another file preceded by the word blacklist


so audiodrivers contains
snd_driver1
snd_driver2
etc

and blacklist will contain
blacklist snd_driver1
blacklist snd_driver2
etc
 
Old 01-22-2010, 07:02 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Dogs View Post
In this example, why does blacklist end up in the file blacklist and $a end up in stdout?

Code:
#!/bin/sh
test= 'lsmod' | 'awk' '/snd_/{print $1}' > audiodrivers

while a=read line
do 
   echo -n "blacklist $a" >> test.txt
done < /root/testscript/audiodrivers
Maybe you need this?
Code:
lsmod | grep 'snd_' | while read a; do
  echo "blacklist $a" >> test.txt
done
Kevin Barry

Last edited by ta0kira; 01-22-2010 at 07:04 PM.
 
Old 01-22-2010, 07:28 PM   #3
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Original Poster
Rep: Reputation: 25
Code:
#!/bin/bash
lsmod | awk '/snd_/{print $1}' | while read a; do
  echo "blacklist $a" >> test.txt
done
That's much better. Thank you =)

I'd still like to know why in the original code the echo command would be split in half..

As if I'd typed
Code:
while a=read line
do 
   echo -n "blacklist " >> test.txt
   echo "$a"         #goes to stdout, with newline

done < /root/testscript/audiodrivers
instead of

Code:
while a=read line
do 
   echo -n "blacklist $a" >> test.txt
done < /root/testscript/audiodrivers

Last edited by Dogs; 01-22-2010 at 07:29 PM.
 
Old 01-22-2010, 07:35 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I'm not even sure what the syntactic meaning of while a=read line is. I'm surprised it does anything at all. Likewise with the test= line.
Kevin Barry

Last edited by ta0kira; 01-22-2010 at 07:36 PM.
 
Old 01-22-2010, 07:51 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Dogs View Post
Code:
#!/bin/sh
test= 'lsmod' | 'awk' '/snd_/{print $1}' > audiodrivers

while a=read line
do 
   echo -n "blacklist $a" >> test.txt
done < /root/testscript/audiodrivers
all the above is redundant

Code:
lsmod | awk '/snd_/{print "blacklist "$1 > "test.txt"}'
 
  


Reply



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
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
screensaver peculiarity, see through... rtreinen Linux - Software 2 04-16-2007 01:04 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM

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

All times are GMT -5. The time now is 02:16 PM.

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