• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

function

Bash input executes functions

December 1, 2021

How to execute functions in Bash based on argument.

There are 2 functions: build and terminate.

#!/bin/bash
build () {
 echo 'Building services'
}
terminate () {
 echo 'Terminating services'
}
if [ $1 == 'build' ]; then build
elif [ $1 == 'terminate' ]; then terminate
else echo 'Valid options are "build" or "terminate"'
fi

#!/bin/bash build () { echo 'Building services' } terminate () { echo 'Terminating services' } if [ $1 == 'build' ]; then build elif [ $1 == 'terminate' ]; then terminate else echo 'Valid options are "build" or "terminate"' fi

Execute a function based on argument.

$ bash test.sh build
Building services
 
$ bash test.sh terminate
Terminating services
 
$ bash test.sh anyother
Valid options are "build" or "terminate"

$ bash test.sh build Building services $ bash test.sh terminate Terminating services $ bash test.sh anyother Valid options are "build" or "terminate"

Filed Under: Linux Tagged With: argument, bash, execute, function

Logitech K811 Page Up and Down

September 23, 2020

I’ve been using this keyboard for a few years, but never knew how to use page and page down. It’s …

fn + up arrow = page up
fn + down arrow = page down

fn + up arrow = page up fn + down arrow = page down

Filed Under: Misc Tagged With: down, fn, function, k811, logitech, page, up

Bash Convert Bytes

January 26, 2020

Here’s a function to convert bytes into kilo, mega, giga, tera up to zeta bytes.

function convert_bytes {
    bytes=$credits
    b=${bytes:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}B)
    while ((b > 1024)); do
        d="$(printf ".%02d" $((b % 1000 * 100 / 1000)))"
        b=$((b / 1000))
        let s++
    done
    echo "$b$d ${S[$s]}"
}
credits=2308974418330
echo $credits 
convert_bytes $credits

function convert_bytes { bytes=$credits b=${bytes:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}B) while ((b > 1024)); do d="$(printf ".%02d" $((b % 1000 * 100 / 1000)))" b=$((b / 1000)) let s++ done echo "$b$d ${S[$s]}" } credits=2308974418330 echo $credits convert_bytes $credits

Result is:

2308974418330
2.30 TB

2308974418330 2.30 TB

Filed Under: Linux Tagged With: bash, bytes, conversion, convert, function

WordPress Numeric Pagination

September 8, 2019

Here’s how to add numeric pagination on your WordPress pages.

Add this code to functions.php.

function pagination($pages = '', $range = 4) {
  $showitems = ($range * 2)+1;
  global $paged;
  if(empty($paged)) $paged = 1;
  if($pages == '') {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages) {
      $pages = 1;
    }
  }
  if(1 != $pages) {
    echo '<p><div class="pagination"><span>Page' . $paged . ' of  ' . $pages . '</span>   ';
    if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo '<a href="' . get_pagenum_link(1) . '"><< First</a>';
    if($paged > 1 && $showitems < $pages) echo '<a href="'.get_pagenum_link($paged - 1).'">< Previous</a>';
    for ($i=1; $i <= $pages; $i++) {
      if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
        echo ($paged == $i)? '<span class="current">' . $i . '</span>' : '<a href="' . get_pagenum_link($i) . '"class="inactive">' . $i . '</a>';
      }
    }
    if ($paged < $pages && $showitems < $pages) echo '<a href="' . get_pagenum_link($paged + 1) . '">Next ></a>';
    if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo '<a href="' . get_pagenum_link($pages) . '">Last >></a>';
    echo '</div></p>';
  }
}

function pagination($pages = '', $range = 4) { $showitems = ($range * 2)+1; global $paged; if(empty($paged)) $paged = 1; if($pages == '') { global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages) { $pages = 1; } } if(1 != $pages) { echo '<p><div class="pagination"><span>Page' . $paged . ' of ' . $pages . '</span> '; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo '<a href="' . get_pagenum_link(1) . '"><< First</a>'; if($paged > 1 && $showitems < $pages) echo '<a href="'.get_pagenum_link($paged - 1).'">< Previous</a>'; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? '<span class="current">' . $i . '</span>' : '<a href="' . get_pagenum_link($i) . '"class="inactive">' . $i . '</a>'; } } if ($paged < $pages && $showitems < $pages) echo '<a href="' . get_pagenum_link($paged + 1) . '">Next ></a>'; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo '<a href="' . get_pagenum_link($pages) . '">Last >></a>'; echo '</div></p>'; } }

To display, comment out ‘the_post_navigation’ function and add the ‘pagination’ function instead in your template files.

//the_posts_navigation();
pagination();

//the_posts_navigation(); pagination();

Code courtesy of webdesignsun.

Filed Under: WP Tagged With: function, numeric, pagination, php, template, wordpress

Custom Content in WordPress

January 24, 2014

There are some instances where you might need to alter or customize the content of a WordPress post. You could be working on a custom post type with a few extra meta box fields, and you simply want to display them on a template. So, instead of editing the WordPress theme files, you can simply customize the WordPress content if it’s a certain post type. Otherwise, the content remains the same if it’s a regular post or page.

Show me! In this example, we will filter the WordPress content called the_content by passing a custom function to it. The custom function will pass the content and checks to see if the post type is equal to ‘books.’ If the post type is equal to ‘books’, it will prepend a paragraph saying, ‘Hello Bookworms!!!’ before the content. If the post type is not equal to ‘books’, it will simply return the original content.

Here’s the Code

function urr_filter_content($content) {
  $format = get_post_type( get_the_ID() );
  if ( $format == 'books' ) : // your custom post-type
    $new_content  = '<p>Hello Bookworms!!!</p>';
    // add your custom post type meta 
    $new_content  .= '<p>Writer: ' . get_post_meta( get_the_ID(), 'books_writer', true) . '</p>';
  endif;
  $content = $new_content . $content;
  return $content;
}
add_filter('the_content', 'urr_filter_content');

function urr_filter_content($content) { $format = get_post_type( get_the_ID() ); if ( $format == 'books' ) : // your custom post-type $new_content = '<p>Hello Bookworms!!!</p>'; // add your custom post type meta $new_content .= '<p>Writer: ' . get_post_meta( get_the_ID(), 'books_writer', true) . '</p>'; endif; $content = $new_content . $content; return $content; } add_filter('the_content', 'urr_filter_content');

There could be numerous reasons why you want to filter the content. Here’s just a few.

  • Custom format based on ‘post type’ or ‘post format’
  • Adding image, audio and video embeds to your content
  • Adding special divs to your content
  • Drop Caps
  • An author byline
  • Adding a widget area

In most cases, content remains undisturbed. But, there might be some crazy instance where you may have to alter it. The function above shows you how you can filter and alter your WordPress content. If you have questions, just leave me a comment!

Filed Under: PHP, WP Tagged With: content, function, post type

  • Home
  • About
  • Archives

Copyright © 2023