Linux Blog

Using wc and How To Count Table Rows

Filed under: Shell Script Sundays — TheLinuxBlog.com at 1:07 pm on Sunday, March 9, 2008

I made this little script to check how many packages were available on the web from the Cygwin Package Repository located at http://www.cygwin.com/packages

Its a one liner but it does its job well.

CYGLIST=$(curl http://www.cygwin.com/packages/ | grep \<tr | grep ball | wc -l); echo $CYGLIST;

All the above is doing is creating a variable called CYGLIST that is the result of grabbing the cygwin.com/packages/ page, grepping all of the TR’s that also have the word “ball” in it (for the image) and then using the wc -l (L) command to count how many results are found. Then the list is echoed out.

wc is a very useful command for printing newline, word and byte counts. This is a good example of how to use wc to count lines in a shell script. wc can also be used to print all of these values in one line of a file.  The syntax is below:

bash-3.1# wc file.txt
9  20 184 file.txt

The above shows the number of lines in the file.txt, it shows how many words are in the file and also how many bytes. In my first example wc uses the -l switch to display the number of lines. This script can also be used with a little bit of bash math to calculate how many items are in an HTML list. I’m working on a script that automatically does this, when its finished I will be sure to post it here on The Linux Blog.

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.

SSH Tunnel

Filed under: Linux Software, Quick Linux Tutorials — TheLinuxBlog.com at 11:23 pm on Thursday, March 6, 2008

Today I had the need to access my development web server at my office network. I have a firewall that runs SSH but doesn’t my firewall does not forward the port for the web server. So, in an emergency situation I was able to use an Linux with SSH and Tunnel into my network on port 80. Since I have a server running on port 80 on this computer I could not use this port. I chose port 8080 to use for the local port and forward it to my web server on the firewalled network. I did this by doing the following:

ssh <SSH HOST> -L 8080:192.168.1.X:80

Using SSH Tunneling I was able to then browse to http://localhost:8080 on this computer and successfully view the contents of my web server. So, any time you need to connect to a port on a computer behind a NAT firewall that you have access to SSH on is just use a SSH Tunnel and then use the -L Switch with the port on your local machine first, then the destination address and port. Simple!

timestamps in the shell

Filed under: Shell Script Sundays — TheLinuxBlog.com at 12:02 pm on Sunday, March 2, 2008

Time and Date functions are very important when writing shell scripts. I mostly use them for logging reasons, for example to know when something was run last. As much as I dislike time stamps they are still used (at least for now) and therefore I am giving and example.

I am unsure of a way to get a time stamp in Bash. If you have PHP installed you can do the following to get a UNIX time stamp (suitable for inserting into a DB):

php -r ‘echo time().”\n”;’

php -r executes PHP code inside quotes. the time() function just creates a time stamp for the current time. If you need to format a string based on a time stamp you can use the date() function. Here is an example of turning a timestamp into a readable string:

bash-3.1$ php -r ‘echo date(”l dS \of F Y h:i:s A”,”1204476759″).”\n”;’
Sunday 02nd of March 2008 11:52:39 AM

Take a look at the date() function page on php.net if you wish to use this method of using time stamps in the shell.

If any one has any other methods of using time stamps in the shell or needs any help as usual leave a comment :)

« Previous Page