Loading...

Category: Languages

  • The SourceBench 1:53 pm on February 1, 2010 Permalink | Reply
    Tags: jobs, multiple, parallel, process, threading, wait   Posted in Bash/shell

    Simple parallel processing with Bash using “wait” and “jobs” 

    #!/bin/bash
    # this is a simple helper script that can be used when you are in need of running multiple processes at once
    # simply run your command with an ampersand at the end and after this
    # execute forky with the number of parallel processes as argument.
    
    function forky() {
    	local num_par_procs
    	if [[ -z $1 ]] ; then
    		num_par_procs=3
    	else
    		num_par_procs=$1
    	fi
    
    	while [[ $(jobs | wc -l) -ge $num_par_procs ]] ; do
    		sleep 1
    	done
    }
    
    # below is an example - make sure to change this to your needs
    for fname in `ls -1`; do
    	echo "`jobs | wc -l` jobs in spool"
    	echo -e "processing $fname\n"
    	# below should be the command you like to execute. in this example it's a php script that would do something with a file.
    	# important is the & at the end of the command which sends the command to the background
    	php ~/a_script_that_does_something_with_the_file --file=$fname &
    	# after this call the forky function with the amount of parallel processes as parameter
    	forky 5
    done
    
    wait

     
  • The SourceBench 11:52 pm on January 19, 2010 Permalink | Reply
    Tags: dns, , nameserver, ns, whois   Posted in Bash/shell

    Test if a nameserver change is done right 

    # imagine you just updated your whois data so it will point your domain to an other nameserver and you want to know if that worked out
    # this little command lets you know on *nix based machines
    for i in a b c d e f g h i j k l; do dig +short ns domainname.com @$i.GTLD-SERVERS.NET; done

     
  • The SourceBench 2:47 pm on December 21, 2009 Permalink | Reply
    Tags: context menu, disable, , right click   Posted in JavaScript

    Disable right click context menus with jQuery 

    $(document).ready(function(){
      $(document).bind("contextmenu",function(e){
        return false;
      });
    });

     
  • The SourceBench 9:15 am on December 9, 2009 Permalink | Reply
    Tags: , time machine   Posted in Bash/shell

    command line real-time view of what files Time Machine is copying 

    sudo fs_usage -w -f filesys backupd | grep open

     
  • The SourceBench 11:49 pm on December 6, 2009 Permalink | Reply
    Tags: connections, listening, netstat, ports   Posted in Bash/shell

    Print the number of established and listening connections to your server on port 80 per each IP address 

    netstat -plan|grep :80|grep -E "(EST|LIST)"|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1

     
  • The SourceBench 9:49 am on December 6, 2009 Permalink | Reply
    Tags: action, description, filter, meta, meta description, wp_head   Posted in WordPress

    Automatically insert meta description tag into posts/pages. 

    <?php
    /**
     * Automatically insert meta description tag into posts/pages.
     * - can be configured to use either first X chars/words of the post content or post excerpt if available
     * - can use category description for category archive pages if available
     * - can use tag description for tag archive pages if available
     * - can use blog description for everything else
     * - can use a default description if no suitable value is found
     * - can use the value of a custom field as description
     *
     * @usage
     * // add a custom configuration via filter
     * function set_meta_desc_settings( $settings ) {
     * 		return array( 'length' => 10, 'length_unit' => 'char|word', 'use_excerpt' => true, 'add_category_desc' => true, 'add_tag_desc' => true, 'add_other_desc' => true, 'default_description' => '', 'custom_field_key' => '' );
     * }
     * add_filter( 'meta_desc_settings', 'set_meta_desc_settings' );
     * add_action( 'wp_head', 'meta_desc' );
     *
     * @author Thorsten Ott
     */
    function meta_desc() {
    	$default_settings = array(
    								'length' => 25, // amount of length units to use for the meta description
    								'length_unit' => 'word', // the length unit can be either "word" or "char"
    								'use_excerpt' => true, // if the post/page has an excerpt it will overwrite the generated description if this is set to true
    								'add_category_desc' => true, // add the category description to category views if this value is true
    								'add_tag_desc' => true, // add the category description to category views if this value is true
    								'add_other_desc' => true, // add the blog description/tagline to all other pages if this value is true
    								'default_description' => '', // in case no description is defined use this as a default description
    								'custom_field_key' => '', // if a custom field key is set we try to use the value of this field as description
    								);
    
    	$settings = apply_filters( 'meta_desc_settings', $default_settings );
    
    	extract( shortcode_atts( $default_settings, $settings ) );
    
    	global $wp_query;
    
    	if( is_single() || is_page() ) {
    		$post = $wp_query->post;
    
    		// check for a custom field holding a description
    		if ( !empty( $custom_field_key ) ) {
    			$post_custom = get_post_custom_values( $custom_field_key, $post->ID );
    			if ( !empty( $post_custom ) )
    				$text = $post_custom[0];
    		}
    		// check for an excerpt we can use
    		else if( $use_excerpt && !empty( $post->post_excerpt ) ) {
    			$text = $post->post_excerpt;
    		}
    		// otherwise use the content
    		else {
    			$text = $post->post_content;
    		}
    
    		$text = str_replace( array( "\r\n", "\r", "\n", "  " ), " ", $text ); // get rid of all line breaks
    		$text = strip_shortcodes( $text ); // make sure to get rid of shortcodes
    		$text = apply_filters( 'the_content', $text ); // make sure it's save
    		$text = trim( strip_tags( $text ) ); // get rid of tags and html fragments
    		if ( empty( $text ) && !empty( $default_description ) )
    			$text = $default_description;	
    
    	} else if( is_category() && true == $add_category_desc ) {
    		$category = $wp_query->get_queried_object();
    		$text = trim( strip_tags( $category->category_description ) );
    		if ( empty( $text ) && !empty( $default_description ) )
    			$text = $default_description;
    
    	} else if( is_tag() && true == $add_tag_desc ) {
    		$tag = $wp_query->get_queried_object();
    		$text = trim( strip_tags( $tag->description ) );
    		if ( empty( $text ) && !empty( $default_description ) )
    			$text = $default_description;
    
    	} else if ( true == $add_other_desc ) {
    		$text = trim( strip_tags( get_bloginfo('description') ) );
    		if ( empty( $text ) && !empty( $default_description ) )
    			$text = $default_description;
    	}
    
    	if ( empty( $text ) )
    		return;
    
    	if ( 'word' == $length_unit ) {
    		$words = explode(' ', $text, $length + 1);
    		if ( count( $words ) > $length ) {
    			array_pop( $words );
    			array_push( $words, '...' );
    			$text = implode( ' ', $words );
    		}
    	} else {
    		if ( strlen( $text ) > $length ) {
    			$text = mb_strimwidth( $text, 0, $length, '...' );
    		}
    	}
    
    	if ( !empty( $text ) ) {
    		echo "\n<meta name=\"description\" content=\"$text\" />\n";
    	}
    }
    ?>

     
  • The SourceBench 11:08 pm on December 4, 2009 Permalink | Reply
    Tags: extract, preg_match_all, regex, regular expression,   Posted in PHP

    parse a urls from text file and split their components with named labels 

    preg_match_all('/(?P<protocol>(?:(?:f|ht)tp|https):\/\/)?(?P<domain>(?:(?!-)(?P<sld>[a-zA-Z\d\-]+)(?<!-)[\.]){1,2}(?P<tld>(?:[a-zA-Z]{2,}\.?){1,}){1,}|(?P<ip>(?:(?(?<!\/)\.)(?:25[0-5]|2[0-4]\d|[01]?\d?\d)){4}))(?::(?P<port>\d{2,5}))?(?:\/(?P<script>[~a-zA-Z\/.0-9-_]*)?(?:\?(?P<parameters>[=a-zA-Z+%&\&amp;\'\(\)0-9,.\/_ -]*))?)?(?:\#(?P<anchor>[=a-zA-Z+%&0-9._]*))?/x',$text,$data)

     
  • The SourceBench 11:05 pm on December 4, 2009 Permalink | Reply
    Tags: , , list, sed,   Posted in Bash/shell

    Get a list of urls/domains from a text file 

    sed 's/http/\^http/g' FILENAME | tr -s "^" "\n" | grep http| sed 's/[\ |\\\|\"].*//g' | sed "s/['].*//g" | sort | uniq

     
  • The SourceBench 10:03 pm on December 3, 2009 Permalink | Reply
    Tags: launch application, open,   Posted in Bash/shell

    Start an application from the command line in OSX 

    # use open -a followed by the program name and optionally a file name for an output log file
    open -a /Applications/TextEdit.app logfile.txt

     
  • The SourceBench 9:49 pm on December 3, 2009 Permalink | Reply
    Tags: proxy, socks5, ssh   Posted in Bash/shell

    Simple Socks5 Proxy connection with SSH 

    # Sometimes you want to get a simple proxy to fake your IP or encrypt your connection
    # in case you are surfing in an insecure space. All you need is a server which you can access via ssh.
    # then use the following command to open a ssh proxy connection with this command
    ssh -ND 9999 your_user@your_ssh_remote_host
    # after this you will have SOCKS5 server on localhost port 9999 which you can enter as your browser proxy

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel