If you’ve got a string of items in bash which are delimited by a common character (comma, space, tab, etc) you can split that into an array quite easily. Simply (re)define the IFS variable to the delimiter character and assign the values to a new variable using the array=($<string_var>) syntax. The new variable will now [...]
Archive for the ‘Bash’ Category
Convert a delimited string into an array in Bash
Posted: 9th March 2012 by Tim in BashTags: array, Bash, delimiter, IFS, linux, OIFS, script, scripting, string, terminal, unix
Variable default values in Bash
Posted: 2nd October 2011 by Tim in BashTags: Bash, default, preset, sh, shell, value, var, variable
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. [...]
Piping stderr in unix
Posted: 26th May 2011 by Tim in Bash, UbuntuTags: cerr, cout, fd, file descriptor, linux, pipe, redirect, stderr, stdout, unix
In unix, you can pass output from one program to another using the pipe symbol (|). Unfortunately, it only pipes the output from stdout (cout). You can pass the output from both stdout and stderr (cerr) by adding 2>&1 to the end of the command before the pipe, where 1 is the file descriptor for [...]
Ignoring the first line of output in a unix terminal
Posted: 31st January 2011 by Tim in Bash, Software Development, UbuntuTags: first line, ignore, linux, sed, sh, shell, terminal, Ubuntu, unix
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 BashTags: Bash, filename, files, placeholder, shell, wild card, wildcard
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 BashTags: Bash, manipulation, programming, script, shell, string, substr, substring, variable
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, [...]
Checking for empty string in Bash
Posted: 19th May 2010 by Tim in BashTags: Bash, coding, dash, empty, language, linux, null, programming, script, scripting, SET, shell, string, terminal
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 [...]
Getting avr32-linux-gcc compiler on linux
Posted: 14th December 2009 by Tim in Bash, C, UbuntuTags: atmel, avr32, avr32-linux-gcc, buildroot, compile, gcc, platform
If you’re trying to compile C programs for Linux on an AVR32 architecture, you’re going to have to get the avr32-linux-gcc cross compiler. Note that you can’t use the avr32-gcc compiler, as this compiler makes programs which do not run on an operating system (ie: they talk to the system directly), which will not run [...]
SSH Login without a password
Posted: 17th November 2009 by Tim in Bash, UbuntuTags: key, keygen, passphrase, password, public, remote, rsa, secure, ssh
If you access the same computer through SSH on a regular basis, or want to access a machine through SSH in a script, then you don’t want to have to worry about passwords. Luckily, there is a way to grant SSH access without a password while remaining secure. For a quick and easy fix, download [...]
List files which are not up to date in CVS
Posted: 5th October 2009 by Tim in Bash, CVS, UbuntuTags: Bash, cvs, grep, pipe, repository, script, shell, terminal
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 [...]
Removing directories in CVS
Posted: 21st September 2009 by Tim in Bash, CVSTags: Bash, cvs, delete, directories, files, folders, linux, remove, repository, script, unix, version control, versioning
There is no way to delete a folder in CVS like you can with files. The directories must be kept so that the versioning information relating to the files which used to be in the repository can still be used (ie: you can revert back to a revision when the files still existed). The only [...]