Posts Tagged ‘sh’

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: […]

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 […]

There are many ways in the linux terminal to print the nth word of a given file or output. One way to do this without worrying about tabs, extra spaces or word length is to use awk. With awk, this can be done on one line by using the {print $<n>} syntax. For example, the […]

There are a few ways to find out if a string contains a substring using bash. Below are a couple of ways this can be done without invoking any other processes. Star Wildcard One very simple method is to match strings using the * character to denote any number of other characters. For example: if […]

Sed is a useful tool for editing strings on the command line. Changing characters to uppercase or lowercase can be done easily with this tool by simply adding one or more of the following to your substitution string: * \L – convert all proceeding characters to lowercase * \U – convert all proceeding characters to […]

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 […]

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 […]