Linux Blog

Remotely Changing Windows Volume

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:41 pm on Sunday, October 18, 2009

This is not really “shell scripting” but the end result is some more bash scripts in my bin directory so what the hell? It’s going in the shell script section because its Sunday. So what?

I like to listen to music on my Windows box while I work on my Linux box. Online radio and other sounds, just get in the way too much. One of the things I wanted to do for a while was remotely control my volume so I didn’t have to use my KVM to switch over to change the volume when ever anyone came in my office.

Its actually pretty easy to control your windows volume from Linux.

At first I thought, I’d create a dummy audio device, and some how map it over. Then I figured that was overkill and I’d try something a bit easier. I have SSH via Cygwin, so all I needed was a way to control the volume locally, and I could execute the command with SSH. Having no volume utilities jump at me when I looked through the Cygwin repositories I went to look for something else.

NirCmd is an awesome utility, giving me and other Windows users the ability to do things that Linux users may take for granted, you can read about it here: http://www.nirsoft.net/utils/nircmd.html after installing it, and making sure that my corporate AV didn’t throw a hissy, it was just a matter of dumping some scripts in my bin directory and chmodding them so they would run.

Here is what they look like:

Volume Down Script: ssh windowsbox -l owen -C “nir changesysvolume -2000″

Volume Up Script: ssh windowsbox -l owen -C “nir changesysvolume 2000″

Mute: ssh windowsbox -l owen -C “nir mutesysvolume 1″

Unmute: ssh windowsbox -l owen -c “nir mutesysvolume 0″

Real simple, and the mute/unmute really comes in handy for when some one walks into my office.

Using Bash Scripts in Web Applications

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:22 pm on Sunday, May 25, 2008

Using bash scripts for web applications is not exactly rocket science, nor is it necessarily the best idea in the world but it can be handy to do if you already have a bash script and want to use its functionality on the web. There are a couple of ways to use bash scripts on the web.

The first that I know of is as a CGI. All that you have to do for this one is create a cgi-bin or allow files with the extension .cgi to be executed this is done with apache in your httpd.conf file.

The Second is to use another scripting language to call the script. The easiest way for me is to use PHP. A system call to the script file can my made using the exec() function. Just make sure that the file has execute rights for the user that your web server runs as. Here is an example of using the exec() function in PHP:

$output = exec(‘/usr/local/bin/yourscript.sh’);

The Third method is to use Server Side Includes to include the script. I personally am not familiar with setting up SSI’s but this is how you execute a command from within a SSI:

<!–#exec cmd=”/usr/bin/date” –>

Which ever method you choose precautions have to be taken. Make sure that all inputs are sanitized so that a user cannot escape the command, pipe output to another file or manipulate the system in another way. In PHP it is easy to do this, but I can not speak for CGI’s or SSI’s. I hope this shows some insights as to how you can run bash scripts in your web application. If you have any other methods such as using mod_python or maybe tcl, please post them as a comment!

Bash Scripting Techniques

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:20 pm on Sunday, May 18, 2008

Here are some techniques that you can use in your bash scripts for finding and searching through files. Combined with other shell scripting techniques these can be very powerful.

Find all files in the current directory and print them:

find . -iname “.jpg”

Find all files that you have access to read with matching patern:

find / -iname “pattern”

Normally with grep text is matched and is case sensitive. Heres how to do a case insensitive search with grep:

cat <filename> | grep -i <match>

Finding and replacing text is easily done in bash with sed. This find and replace puts the contents into a new file:

┬ácat <filename> | sed ‘s/FIND/REPLACE/’ > <new filename>

Finding the line number that a particular line of text is on is sometimes useful. Here is how to do it:

 cat <filename> | grep -n <match>

Looping over a file in bash and echoing the output is sometimes useful for the processing of text files. Heres how to do it:

cat <filename> | while read i; do echo $i; done

Thats about all the bash scripting techniques that I can currently think of for finding in files. I know there are a ton more that I use but its hard to write them all down at once. As I come up with them or solve a problem I’ll add them here. If you have any of your own, please leave them in the comments.

Writing Loops

Filed under: The Linux Blog News — TheLinuxBlog.com at 2:34 pm on Friday, March 7, 2008

The act of a loop is doing something over and over until a condition. Like in bash scripts I get stuck in loops while trying to write posts for The Linux Blog. The problem is that when I get stuck in a loop I can write a lot of posts about one topic (for example SSH.) But I don’t want to have all of my front page covered in posts about SSH. This is where my readers (You) come in.

I made a promise last month that I would not ask for any more writers. Since this is a new month I can start over again. Basically any one willing to write short articles or how to’s is welcome to submit an article to me. I will be making sure that the WordPress login script works over this weekend that way if you would like to help me out and gain some exposure you can do so very easily. By writing for me it will help out with the content of this site by not making it all similar topics. Other than that news I’m working on getting some new features for this site worked out. When I do it should be interesting for all of us.

Have a great Friday!

- Owen.

Select Statements in Bash

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:40 pm on Sunday, August 19, 2007

Shell scripts often need a menu so that the user can interact with the script and choose options. An easy way to so this is with the “select” statement.

Syntax:

select selection_var [in list]; do
statements (can use selection_var)
done

Example:

echo “########################################”;
echo “# SELECTS EXAMPLE – TheLinuxBlog.Com #”;
echo “########################################”;
echo “# Please Choose an Option #”;
echo “########################################”;

select selection_var in Number_1 This_is_Choice_2 Exit; do
case $selection_var in
Number_1 )
echo “You picked $selection_var or #$REPLY”
;;
This_is_Choice_2 )
echo “You picked $selection_var or #$REPLY”
;;
Exit )
echo Goodbye!
break
;;
* )
echo “Invalid Selection”
;;
esac
done

This bash select example has three choices. The first two are just examples, the third exits the script. There is a fourth case but it is not a choice, it tells the user that they have entered an invalid selection. I recommend using a case inside of a select because it will make life easier when adding onto a script. The $REPLY variable is returned from the select statement as a means of knowing what number was pressed. The $REPLY variable can be used in the case statement but I avoid doing so as all of the case blocks will have to be rearranged every time a new option is added.

The select statement in bash is very easy to implement and it can add a whole new range of functionality into your scripts. So try them out and look out for them in future shell scripts from The Linux Blog.