tutorials:bash_scripting:part3
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorials:bash_scripting:part3 [2012/05/29 10:24] – created rmiles | tutorials:bash_scripting:part3 [2017/10/12 21:58] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | //**creating | + | //**3. Creating |
- | + | ---- | |
- | 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 | + | 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 |
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 | + | 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 / | + | |
+ | I have disabled the NVIDIA chip in the BIOS. However, it is still listed in / | ||
+ | < | ||
rick[~]$ cat / | rick[~]$ cat / | ||
0 [NVidia | 0 [NVidia | ||
Line 17: | Line 17: | ||
1 [CMI8738 | 1 [CMI8738 | ||
C-Media CMI8738 (model 55) at 0xa000, irq 16 | C-Media CMI8738 (model 55) at 0xa000, irq 16 | ||
- | + | </ | |
- | 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 I can also use it to set which sound device I want to use |
+ | < | ||
rick[~]$ cat .asoundrc | rick[~]$ cat .asoundrc | ||
defaults.ctl.card 1 | defaults.ctl.card 1 | ||
Line 32: | Line 32: | ||
route_policy copy | route_policy copy | ||
} | } | ||
+ | </ | ||
+ | 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 / | ||
- | I have put together a simple | + | The basic script |
- | + | < | |
- | The following | + | |
#!/bin/bash | #!/bin/bash | ||
Line 49: | Line 49: | ||
echo " | echo " | ||
echo " | echo " | ||
- | + | </ | |
- | The first echo command uses a single ">" | + | The first echo command uses a single ">" |
All the other echo commands use ">>" | All the other echo commands use ">>" | ||
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: | ||
- | + | < | |
- | cat / | + | cat / |
+ | </ | ||
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. | ||
+ | < | ||
bash-4.1$ cat / | bash-4.1$ cat / | ||
0 [CMI8738 | 0 [CMI8738 | ||
C-Media CMI8738 (model 55) at 0xa000, irq 16 | C-Media CMI8738 (model 55) at 0xa000, irq 16 | ||
+ | </ | ||
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. | ||
+ | < | ||
bash-4.1$ cat / | bash-4.1$ cat / | ||
0 [CMI8738 | 0 [CMI8738 | ||
+ | </ | ||
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. | ||
+ | < | ||
bash-4.1$ cat / | bash-4.1$ cat / | ||
0 | 0 | ||
+ | </ | ||
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: | ||
+ | < | ||
#!/bin/bash | #!/bin/bash | ||
# / | # / | ||
Line 101: | Line 101: | ||
# End of scipt | # End of scipt | ||
+ | </ | ||
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. | ||
+ | < | ||
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 | ||
} | } | ||
+ | </ | ||
+ | ---- | ||
+ | **Cheers!** | ||
Line 130: | Line 133: | ||
- |
tutorials/bash_scripting/part3.1338251070.txt.gz · Last modified: 2017/10/12 21:58 (external edit)