User Tools

Site Tools


tutorials:bash_scripting:part3

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorials:bash_scripting:part3 [2012/05/29 10:25] rmilestutorials:bash_scripting:part3 [2017/10/12 21:58] (current) – external edit 127.0.0.1
Line 1: Line 1:
 //**3. Creating a configuration file on the fly and stream editing**// //**3. Creating a configuration file on the fly and stream editing**//
  
- +---- 
-Someone on the list asked If I'd do something with stream editors. In this tutorial I'll use pipes, grep, sed and awk to help set a variable on the file when I log into a session. Grep in this case will be used to print lines matching a given pattern to stdout. Sed will be used to print only one line piped from grep to stdout. Awk will be used to print just one field of from the line piped from sed to stdout.+Someone on the list asked If I'd do something with stream editors. In this tutorial I'll use pipes, grep, sed and awk to help set a variable in a configuration file when I log into a session. Grep in this case will be used to print lines matching a given pattern to stdout. Sed will be used to print only one line piped from grep to stdout. Awk will be used to print just one field from the line piped in from sed.
  
 Grep is a pattern matcher, sed is a non interactive editor that acts on streams piped into it and awk is actually a pattern matching programming language that is quite handy. Some of the things you can do with sed you can do with grep and some of the things you can do with awk you can do with sed and there are even other applications that could have been used but his is how I solved this problem.  Grep is a pattern matcher, sed is a non interactive editor that acts on streams piped into it and awk is actually a pattern matching programming language that is quite handy. Some of the things you can do with sed you can do with grep and some of the things you can do with awk you can do with sed and there are even other applications that could have been used but his is how I solved this problem. 
Line 8: Line 8:
 Sound confusing? There are more ways than one to stream edit a cat. Sound confusing? There are more ways than one to stream edit a cat.
  
-I have a C-Media PCI sound card installed on my desktop box which also has onboard sound via an NVidia chip+I have a C-Media PCI sound card installed on my desktop box which also has on board sound via an NVidia chip.
- +
-I have disabled the NVIDIA chip in the BIOS. However, it is still listed in /proc/asound/cards and sometimes the order of listing changes and it is listed first which results in no audio.+
  
 +I have disabled the NVIDIA chip in the BIOS. However, it is still listed in /proc/asound/cards and sometimes the order of listing changes. The NVIDIA card is listed first which results in no audio.
 +<code>
 rick[~]$ cat /proc/asound/cards rick[~]$ cat /proc/asound/cards
  0 [NVidia         ]: HDA-Intel - HDA NVidia  0 [NVidia         ]: HDA-Intel - HDA NVidia
Line 17: Line 17:
  1 [CMI8738        ]: CMI8738-MC6 - C-Media CMI8738  1 [CMI8738        ]: CMI8738-MC6 - C-Media CMI8738
                       C-Media CMI8738 (model 55) at 0xa000, irq 16                       C-Media CMI8738 (model 55) at 0xa000, irq 16
- +</code> 
-I have a configuration file called ~/.asoundrc to provide settings required for recording off my sound card and can also use it to set which sound device I want to use +I have a configuration file called ~/.asoundrc to provide settings required for recording off my sound card and can also use it to set which sound device I want to use 
 +<code>
 rick[~]$ cat .asoundrc rick[~]$ cat .asoundrc
 defaults.ctl.card 1 defaults.ctl.card 1
Line 32: Line 32:
  route_policy copy  route_policy copy
 } }
 +</code>
 +In order to have the correct sound card setting I will put together a script that runs when I start up a session. It will check the number of my sound card in /proc/asound/cards then re-write my ~/.asoundrc using the correct card number for my C-Media card.
  
-I have put together a simple script that runs when I start up a session. It checks the number of my souncard in /proc/asound/cards then re-writes my ~/.asoundrc using the correct card number for my C-Media card. +The basic script will will create an ~/.asoundrc on the fly. The output of each echo command is redirected to the file ~/.asoundrc when the script runs.  
- +<code>
-The following will create an ~/.asoundrc on the fly. The output of each echo command is redirected to the file ~/.asoundrc when the script runs.  +
 #!/bin/bash #!/bin/bash
  
Line 49: Line 49:
 echo "    route_policy copy" >> ~/.asoundrc echo "    route_policy copy" >> ~/.asoundrc
 echo "}" >> ~/.asoundrc echo "}" >> ~/.asoundrc
- +</code> 
-The first echo command uses a single ">". This will truncate the existing ~/.asoundrc to zero length before starting a new line with the stdout from echo. +The first echo command uses a single ">". This will truncate the existing ~/.asoundrc to zero length (read delete) before starting with a new line with the redirected stdout from echo. 
  
 All the other echo commands use ">>" for redirection so that the stdout from those commands will be appended to the file.  All the other echo commands use ">>" for redirection so that the stdout from those commands will be appended to the file. 
Line 59: Line 59:
  
 This is where stream editing comes in. I can use a variable for the card number and create the variable each time the script is run using the following: This is where stream editing comes in. I can use a variable for the card number and create the variable each time the script is run using the following:
- +<code> 
-cat /proc/asound/cards | grep CMI | sed q | awk -F " " '{ print $}' +cat /proc/asound/cards | grep CMI | sed q | awk -F " " '{ print $}' 
 +</code>
 The stdout from each command is piped to the next command until just the card number remains. The stdout from each command is piped to the next command until just the card number remains.
  
 Cat is used to pipe the text of ~/.asoundrc to grep and grep is used to list just the lines relevant to the C-Media card. Cat is used to pipe the text of ~/.asoundrc to grep and grep is used to list just the lines relevant to the C-Media card.
 +<code>
 bash-4.1$ cat /proc/asound/cards | grep CMI bash-4.1$ cat /proc/asound/cards | grep CMI
  0 [CMI8738        ]: CMI8738-MC6 - C-Media CMI8738  0 [CMI8738        ]: CMI8738-MC6 - C-Media CMI8738
                       C-Media CMI8738 (model 55) at 0xa000, irq 16                       C-Media CMI8738 (model 55) at 0xa000, irq 16
 +</code>
 Grep outputs two lines and I use sed to print just the first line which provides the card number. Grep outputs two lines and I use sed to print just the first line which provides the card number.
 +<code>
 bash-4.1$ cat /proc/asound/cards | grep CMI | sed q bash-4.1$ cat /proc/asound/cards | grep CMI | sed q
  0 [CMI8738        ]: CMI8738-MC6 - C-Media CMI8738  0 [CMI8738        ]: CMI8738-MC6 - C-Media CMI8738
 +</code>
 Awk is finally used to print (to stdout) the contents of the first field which in this case is 0.  Awk is finally used to print (to stdout) the contents of the first field which in this case is 0. 
 +<code>
 bash-4.1$ cat /proc/asound/cards | grep CMI | sed q | awk -F " " '{ print $1 }' bash-4.1$ cat /proc/asound/cards | grep CMI | sed q | awk -F " " '{ print $1 }'
 0 0
 +</code>
 Note that -F " " tells awk that the field separator is whitespace and '{ print $1 }' is used to print the first field (only) to stdout. Note that -F " " tells awk that the field separator is whitespace and '{ print $1 }' is used to print the first field (only) to stdout.
  
 Here's the final script: Here's the final script:
 +<code>
 #!/bin/bash #!/bin/bash
 # /usr/local/set-soundcard rm20120529 # /usr/local/set-soundcard rm20120529
Line 101: Line 101:
  
 # End of scipt # End of scipt
 +</code>
 I use XFCE and to run this script each time I log in all I have to do is add the script to the list in Settings > Sessions and Startup > Application Autostart. You may have noted that this tutorial started with my C-Media card listed as the second card (1). Actually it was set as the first card (0) when I booted up this morning. After writing this script and setting XFCE to run it when I start a session, I logged out and then back in.  I use XFCE and to run this script each time I log in all I have to do is add the script to the list in Settings > Sessions and Startup > Application Autostart. You may have noted that this tutorial started with my C-Media card listed as the second card (1). Actually it was set as the first card (0) when I booted up this morning. After writing this script and setting XFCE to run it when I start a session, I logged out and then back in. 
 +<code>
 bash-4.1$ cat ~/.asoundrc bash-4.1$ cat ~/.asoundrc
 defaults.ctl.card 0 defaults.ctl.card 0
Line 116: Line 116:
     route_policy copy     route_policy copy
 } }
 +</code>
  
 +----
  
 +**Cheers!**
  
  
Line 130: Line 133:
  
  
- \npcm.copy { 
tutorials/bash_scripting/part3.1338251139.txt.gz · Last modified: 2017/10/12 21:58 (external edit)