Category: Scripting

  • Comparing Lists in Python

    If you have two lists that have the same content in a different order (1, 2, 3, vs 3, 2, 1) are they they the same or different? Python considers them to be different.

    In some cases order may not matter to you, and you just want to know if the same items exist in both lists. When this happens there are a couple ways you compare them.

    1. You can use the built-in sorted() function. This lets our “if” statement compare an alphanumeric sorted version of the list without modifying the content of the variable. If you print the variable after using sorted() the items are still in the original order.
    2. Or you can use the .sort() method. This also puts them list items in alphanumeric order, but it additionally stores these changes in the original variable. If you print the variable out after using .sort() they will be shown in the new order.

    Example code that illustrates both options:

    #!/usr/bin/env python3
    
    list1 = [ 3 , 4 , 5 , 1 , 2 ]
    list2 = [ 5 , 4 , 3 , 2 , 1 ]
    
    print("Comparing original lists")
    if list1 == list2: 
        print("Same")
    else:
        print("Different")
    print("list1:" , list1)
    print("list2:" , list2)
    
    print("Sort and compare using sorted()")
    if sorted(list1) == sorted(list2):
        print("Same")
    else:
        print("Different")
    print("list1:" , list1)
    print("list2:" , list2)
    
    print("Sort and modify using .sort()")
    if list1.sort() == list2.sort(): 
        print("Same")
    else:
        print("Different")
    print("list1:" , list1)
    print("list2:" , list2)

    The output:

    Comparing original lists
    Different
    list1: [3, 4, 5, 1, 2]
    list2: [5, 4, 3, 2, 1]
    Sort and compare using sorted()
    Same
    list1: [3, 4, 5, 1, 2]
    list2: [5, 4, 3, 2, 1]
    Sort and modify using .sort()
    Same
    list1: [1, 2, 3, 4, 5]
    list2: [1, 2, 3, 4, 5]
  • Shell scripting: Iterate over a list line-by-line instead of word-by-word

    Let’s say a directory contains these files:

    My First File.txt
    My Second File.txt

    If you wanted to take an action on each distinct file, you might try something like this:

    for file in `ls ~`; do echo $file; done

    The output will look like below, because the spaces and line breaks are both treated delineators of a new item in the list (ie. a new assignment to the “file” variable.)

    My 
    First
    File.txt
    My
    Second
    File.txt

    If your intent was to act on the complete filename (using only line breaks as indicators of a new item) then you can instead pipe your output through a “while” loop like this:

    ls ~ | while read item; do echo $item; done

    Which will yield the desired output:

    My First File.txt
    My Second File.txt
  • Munki nopkg to turn Bluetooth on and off

    I’ve posted a Munki nopkg installer to control Bluetooth. Assigning it for install causes Bluetooth to be (re)enabled whenever Munki runs. Uninstallation turns Bluetooth off.

    https://github.com/davidmnelson/munki-nopkg/blob/master/BluetoothEnabled.plist

  • Automatically Update WordPress, Themes, and Plugins using WP-CLI

    WordPress is a hugely popular blog/CMS platform, but with widespread adoption comes risk: It is a common target for hackers, exploits, etc. Accordingly, you should make sure it gets regular updates.

    WordPress has a built-in update mechanism but this also requires that its PHP files be writable by the web server, introducing a new set of security risks.

    Luckily there is another option. Instead you can use a command-line tool called WP-CLI, which enables us to script WordPress updates.

    These instructions will outline the steps necessary to install WP-CLI, create a script to update multiple sites at once, and install that script as a cron job to ensure updates happen on a regular schedule.


    Before You Begin

    As with any WordPress maintenance tasks, I recommend making regular backups of your database and files.

    For this process to succeed, you’ll need to run your script as a user who has permission to modify the WordPress files. This could be your regular user account, but you might also want to create a dedicated user such with a name like ‘scripts’, and give it write permissions to your WordPress files. It is not recommended to run this as root.


    1. Install WP-CLI
      Install WP-CLI (adapted from http://wp-cli.org/#installing)

      curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
      chmod +x wp-cli.phar
      sudo mv wp-cli.phar /usr/local/bin/wp
    2. Test WP-CLI
      Run as a user who has write privileges to your WordPress site. If everything works you should get a series of “Success” messages, and/or a list of updated items.

      cd /var/www/html   # replace with path to your site 
      /usr/local/bin/wp core update
      /usr/local/bin/wp core update-db
      /usr/local/bin/wp theme update --all
      /usr/local/bin/wp plugin update --all
    3. Create an Update Script
      Use your favorite text editor to create a new shell script. In that script, put the following code:

      #!/bin/bash
      
      # Absolute paths of WordPress sites. Space-separated.
      sites="/var/www/html/site1 /var/www/html/site2 /var/www/html/site3"
      
      for site in $sites; do
      
      echo $site
      
      /usr/local/bin/wp core update --path=$site --quiet
      /usr/local/bin/wp core update-db --path=$site --quiet
      /usr/local/bin/wp theme update --all --path=$site --quiet
      /usr/local/bin/wp plugin update --all --path=$site --quiet
      
      done
    4. Make The Script Executable
      chmod 700 wp-update
    5. Test The Update Script
      ./wp-update

      If everything works you’ll see a series of “Success” messages, and/or a list of updated items. If you see errors, double-check that your current user has permission to write to the WordPress site directories.

    6. Install Cron Job
      Make sure you’re still logged in as a user who has write permissions for the WordPress site directories.

      crontab -e

      Now create a new cron entry like this one, including the correct path to your update script. In this example it will run every day at 2:30am.

      30 2 * * * /home/scripts/bin/wp-update

      Close and save your crontab file.

    7. If everything worked correctly, your WordPress sites will now auto-update every night.