Monthly Archives: June 2017

How to use Xcode for C and C++ development

Not only is Xcode an excellent IDE for iOS and macOS apps in both Swift and Objective-C; it does just as fine a job for regular C and C++ code. This includes all the features we know and love, such as code completion, version control, and all the rest of it.

Let’s see how to use Xcode 8.3 for C and C++ development.

Continue reading How to use Xcode for C and C++ development

How to fix “can’t log into YouTube from Safari” on macOS

Something rather strange happened to me today: Safari 10 on macOS Sierra refused to let me login to YouTube. All it did was constantly refresh the page in an endless loop, or just display the front page of YouTube. I cleared the caches, reset the history, but no trick seemed to solve the problem.

When I dug deeper into the Preferences, I found something under Privacy that finally fixed it. Let me share with you what worked on my system.

  • head over to Safari – Preferences
  • select the Privacy tab
  • you’ll see a window like this one:

  • select Manage Website Data
  • after a few moments you’ll see a LONG list of websites that have saved cookies on your machine over time
  • in the top right corner, search for YouTube
  • you’ll see something like this:

  • select the YouTube.com entry and hit Remove, followed by Done
  • now surf back to YouTube and login – this time it’ll work

What we’ve just removed were not just cookies, but also HTML local storage data, as well as cache data specific to the YouTube website. I guess cached stuff can get outdated, or not properly deleted when we close our browser in a hurry.

The principle should work for other websites too, should they give you trouble. If you’re sick and tired of any website saving data to your system, consider switching to the “Always Block” option seen in the first screen shot.

 

How to rename a batch of files in Linux

Bulk renaming files can be done with the rename command. It shares many similarities with cp and mv, but its simplicity can be so staggering that its difficult to figure out how to use it.

If we just type “rename” at the command prompt, all we get is the message

While technically correct, what on earth does it mean? How do we use rename?

Let’s do a little exercise. Imagine we had a batch of files, perhaps something like “Title 101.mp4” to “Title 110.mp4”. Let’s create some empty files with those names in a test directory:

So far so good. Now we’d like to rename those files so they read “New Title 101.mp4” to “New Title 110.mp4”. Here’s how it works:

Technically, this follows what the command showed us earlier: “rename from to files…”. Still I feel a little explanation is in order.

For the rename command to work, we don’t need to specify the full file name, nor that we want to rename a batch of files. The command will rename anything that it encounters. All it needs to know is which string to replace with which other string. Those are the first two parameters we give it, in our case wrapped in single quotes because we have a space character in our titles.

The third parameter tells rename where the files live that we want to rename. In our example it was here in the current directory, but it could be anywhere on the system. By specifying *.mp4, only files with that ending will be renamed, all other files will be left in peace.

I hope this helps to understand rename a little better.

How to exit VI with or without saving

Although many alternatives exist, I like using vi for all my command line editing needs. To save changes, I usually use SHIFT + Z + Z, exiting vi under most circumstances.

But sometimes, this trick doesn’t work because of write permission problems. In such cases, vi doesn’t close with the above command. Instead, we must either stash our changes in another file, or quit the session without saving. Here’s how to do that.

Quit vi without saving:
:q!

Save current file under a different name:
:w newfile

  • https://www.cs.colostate.edu/helpdocs/vi.html

How to read command line parameters in BASH Scripts

Shell Scripts (BASH Scripts) can access command line parameters using the the variables $1, $2, $2 and so forth, up to $9. In fact, more parameters can be accessed by using curly brackets, like ${10}, ${187} and so forth.

Here’s an example:

If we run the script with like this

it will tell us the statement is true. Otherwise, it’ll tell us the opposite.

Note the whitespace around the evaluation: [[ ]] is actually a command (much like the == operator) and therefore needs to be surrounded with whitespace.

How to read command line parameters in PHP Shell Scripts

We can access parameters passed via the command line in our PHP shell scripts. Those are stored as an array in the variable $argv. Consider this:

The first part of the script prints out all parameters that have been given, while the second part checks if the parameter was “x” or not. Note that the first item in the array ($argv[0]) will be the the first item on the command line, i.e. the file name and path to this very script. $argv[1] is the first parameter, $argv[2] the second, and so forth.

We can call the script with

to give it one parameter, or with

to give it three parameters.

  • http://php.net/manual/en/reserved.variables.argv.php

How to extract files from a bz2 archive in Linux

If you’ve ever tried to decompress a file that ends in tar.bz2 using the tar command with the standard -x option, you’ll have noticed that it doesn’t work. That’s because some versions of tar don’t understand the bzip2 codec used in these archives.

However, you can tell tar to use this option by specifying the -j parameter, like so:

If this still doesn’t work, we can use the dedicated bzip2 command like so:

The -d switch stands for “decompress”. Notice that this will extract all files and delete the original .bz2 file by default. Very convenient indeed! If you’d like to keep it, just pass the -k switch (for “keep”), like this:

Checkout man bzip2 for more details, or pass the –help for as quick overview.