Posts Tagged ‘shell’

If you have a filename or list of filenames, you may want to strip the extension. There are a few ways of “detecting” which part of the filename is the extension, but may not work if your file has multiple extensions (e.g. .tar.gz), contains spaces or periods, or meets other weird criteria. If you know […]

Imagine you have two text files, one with a list of names and another with a list of birth dates, which each name corresponding to the date of birth on the same line number in the other file, like so: names.txt Anthony Kiedis Flea Chad Smith John Frusciante dob.txt 1-Nov-1962 16-Oct-1962 25-Oct-1961 5-Mar-1970 You would […]

Pause in Bash

Posted: 27th June 2015 by Tim in Bash, Linux
Tags: , , , , , , ,

Sometimes it is useful to pause a script until the user is ready to proceed. In Windows Batch scripting, this can be done with the PAUSE command. In Bash, the same thing can be done using read with the right parameters: read -rn1 -p “Press any key to continue” This command is doing the following: […]

Awk is a useful language for many command line tasks. Often awk is used on the command line on its own or with strings piped to it, but it is possible to turn that awk code into an executable script. Consider the following script. This file contains awk code with a shebang of awk -f. […]

Linux Terminal Clock

Posted: 9th March 2015 by Tim in Bash, Linux
Tags: , , , , , , , , ,

Ever wanted to have a real-time clock on your linux terminal? We can create one with a single line of bash, like so: while echo -en “$(date)\r”; do sleep 1; done Let’s look at how this works: date is a common unix tool used to print the current date and time. The $(…) means that […]

Reading environment variables in python

Posted: 13th November 2012 by Tim in Python
Tags: , , , , , ,

Shell envronment variables contain many useful paths and settings. At times you may want to use these variables from within a python program. This can be done via the os.path.expandvars(<var>) call, defined in os.path. For example, the following program: import os.path print os.path.expandvars(‘$SHELL’); Will print (on my machine): /bin/bash

Get current runlevel in Linux

Posted: 27th October 2012 by Tim in Linux
Tags: , , , , , , ,

Runlevel on linux systems refers to the level of operation of the operating system. For example, runlevel 0 is shutdown, 5 is (generally) multi-user with everything enabled, etc. It’s probably best to refer to Wikipedia if you need a more detailed explaination. To query the current run level, you can run the following command: who […]

Each user on a unix system is assigned a user id, or uid. You can see your uid by running ‘id -u’. root will have a uid of zero. So to check if the user running a script is root, you can simply check that id -u is equal to zero, like so: if [ […]

Variable default values in Bash

Posted: 2nd October 2011 by Tim in Bash
Tags: , , , , , , ,

Sometimes you will be writing a script which, for example, can have some configuration changed from a command line argument. In traditional programming languages you would declare your variables with default values and then overwrite those values with the arguments which are passed in. In bash, however, you can do this all in one statement. […]

Sometimes in a terminal you want to strip out the first line of output from a command. For example, you may want to generate a list of users which have tasks running using the ps command. This command puts a header at the top of the output. You can remove this header by piping the […]

Bash Wildcards

Posted: 8th November 2010 by Tim in Bash
Tags: , , , , , ,

There are lots crazy things you can do with bash. Some of the more useful of these are the bash wildcards. This post will explore the *, ?, {…}, […] and [!…] wildcards. For the examples below, we will demonstrate wildcard usage with the ls command, and assume that the current directory has the following […]

Substrings in Bash

Posted: 17th October 2010 by Tim in Bash
Tags: , , , , , , , ,

There are a number of ways to extract parts of a string in bash. If you know the position of the substring you’re looking for, then you can use the ${string:offset[:length]} syntax. This works by providing a string, an offset (or starting position – remember that the first letter is in position 0) and, optionally, […]

Ever jumped onto an Ubuntu server somewhere without knowing which operating system version it’s running? You can find this out with one simple command: lsb_release -a This will provide output like: Distributor ID: Ubuntu Description: Ubuntu 9.10 Release: 9.10 Codename: karmic

In Bash you quite often need to check to see if a variable has been set or has a value other than an empty string. This can be done using the -n or -z string comparison operators. The -n operator checks whether the string is not null. Effectively, this will return true for every case […]

CVS is annoying in that if you want to find out which files have been modified or need updating, you can’t simply use the cvs status command as there’s too much information displayed. In order to make it useful, you really need to filter the output. Note: the following tutorial only works for linux computers […]