Archive for the ‘Unix’ Category

Articles

Create/Mount Hard Drive Image

In Unix on 07/02/2011 by pier0w

How to create and mount a hard drive image.

Advertisements

Articles

Finding the directory path of a bash script

In Unix on 05/03/2010 by pier0w Tagged: , ,

The full path of any bash script is stored in the argument variable $0 which also includes the name of the script it’s self. So the following script (find-path.sh):

#!/bin/bash
echo $0

Will produce the following output:

# /some/random/path/find-path.sh
/some/random/path/find-path.sh

It should be noted though that the $0 argument doesn’t actually record the scripts absolute path. It holds the path that that was used to reach the script. For example if the same script is run from a relative path the output will be different.

# cd /some/random
# path/find-path.sh
path/find-path.sh


# cd /some/directory
# ../random/path/find-path.sh
../random/path/find-path.sh

So to get the directory path of bash script you could write something to remove the last member of the string using the ‘/’ as a delimiter or you could just use the dirname program that reside on pretty much all Unix environments:

#!/bin/bash
echo `dirname $0`


# /some/random/path/find-path.sh
/some/random/path

Comments Off on Finding the directory path of a bash script

Articles

How to merge the contents of one file with the contents of another file

In Unix on 04/03/2010 by pier0w Tagged: ,

This is how you use AWK to take the contents of one file and then insert that content into a specific place within another file.

Say we have file1 and file2 that have the following contents:

file1:
one, NUM, three
four, NUM, six
seven, NUM, nine
ten, NUM, twelve

file2:
two
five
eight
eleven

First we need to get AWK to pars both files, this can be done as follows:

~#: awk '{print $0}' file2 file1
two
five
eight
eleven
one, NUM, three
four, NUM, six
seven, NUM, nine
ten, NUM, twelve

So you can see it is very easy to get AWK to pars multiple files, all you have to do is add as many files as you like as the final arguments.
Next we need to get AWK to run one set of code over the first file then another set of code over the next file. This can be done with the use of the NR and FNR global variables. NR is a number that increments for every line that is parsed by AWK over all of the files.

~#: awk '{print NR "\t" $0}' file2 file1
1 two
2 five
3 eight
4 eleven
5 one, NUM, three
6 four, NUM, six
7 seven, NUM, nine
8 ten, NUM, twelve

Where FNR is incremented for every line that is parsed by AWK within each file.

~#: awk '{print FNR "\t" $0}' file2 file1
1 two
2 five
3 eight
4 eleven
1 one, NUM, three
2 four, NUM, six
3 seven, NUM, nine
4 ten, NUM, twelve

So to get AWK to run some code over only the first file we can use a conditional to check weather the global counts equals the file count and then only run the code when this is true.

~#: awk 'NR==FNR{print "file2\t" $0; next}{print "file1\t" $0}' file2 file1
file2 two
file2 five
file2 eight
file2 eleven
file1 one, NUM, three
file1 four, NUM, six
file1 seven, NUM, nine
file1 ten, NUM, twelve

In the code above we check to see if we are processing the first file with the “NR==FNR” conditional. If this is true we will run the code in the curly braces directly to the right of the conditional. Now the conditional only applies to this code not the second code block, this means that the second code block will be run every time. So to stop this we call the next function within the first code block, this is similar to “break;” and tells AWK to stop processing this line and move to the next.

Now that we have separate code blocks running over each file we need to insert the text from the first file into the second. To do this we will need to record the contents of the first file into an array to be used when parsing the second file.

~#: awk 'NR==FNR{array\[FNR\]=$1; next}{print "file1\t" $0 "\tfile2\t" array\[FNR\]}' file2 file1
file1 one, NUM, three file2 two
file1 four, NUM, six file2 five
file1 seven, NUM, nine file2 eight
file1 ten, NUM, twelve file2 eleven

In the first code block we have built the array with the first element within file2 and indexed the array with file2’s line numbers array[FNR]=$1. Then we have appended the contents of the array onto the end of each line of the second file using the second files line numbers print "file1\t" $0 "file2\t" array[FNR].
Now that we are able to use the data of file2 with the data of file1 we can do a search and replace. This can be done with AWK’s gsub function, this function is used by supplying a regular expression for the first argument that will be used for the search and then some text as the second argument to be used as the replace. So gsub(/one/,"two") would be the same as :s/one/two/g in vim.

~#: awk 'NR==FNR{array\[FNR\]=$0; next}{gsub(/NUM/,array\[FNR\]);print}' file2 file1
one, two, three
four, five, six
seven, eight, nine
ten, eleven, twelve

There is also another way to carry out this replace, that would be to replace the field instead of the string.

~#: awk 'NR==FNR{array\[FNR\]=$0; next}{$2=array\[FNR\]",";print}' file2 file1
one, two, three
four, five, six
seven, eight, nine
ten, eleven, twelve

Comments Off on How to merge the contents of one file with the contents of another file

Articles

Grid view in GNU Screen

In Unix on 04/03/2010 by pier0w Tagged: , ,

Screen is a brilliant program that I personally think should be merged into all terminal emulators and ssh. One of it’s great features is it’s ability to split the screen up into a grid of buffers that can contain views to different terminals or application output.

To do this first start up a screen say with the name “Split”:
# screen -S Split

Now that you have a screen session running type “hello” into the prompt just so you know this is your first window then create a new window by pressing CTRL-a then c. You will know that you are now in a new window because the hello text will no longer be visable.

    CTRL-a, c – Create a new screen window.

You will now have two windows open and can switch between them by pressing CTRL-a then n or SPACE to move forward through them or CTRL-a then p or BACKSPACE to move backwards through them.

    CTRL-a, n – Move to the next screen window.
    CTRL-a, SPACE – Move to the next screen window.
    CTRL-a, p – Move to the previous screen window.
    CTRL-a, BACKSPACE – Move to the previous screen window.

Now create two more windows by pressing CTRL-a then c two more times.

Now that we have four windows open it is time to split up the screen. To split the screen horizontally press CTRL-a then SHIFT-s. You will now see a white line across the screen with your current window on the top and nothing on the bottom. On the bottom is an empty buffer, screen does not assume that you will want a new terminal opened in the new buffer so leaves it completely empty, you will have to tell screen what to put in it. To do this move into the new buffer with CTRL-a then TAB. Now select the second window with CTRL-a then 1. Screen numbers the windows from 0 upwards.

    CTRL-a, SHIFT-s – Split the screen horizontally
    CTRL-a, TAB – Move to the next buffer.
    CTRL-a, – Show the selected window.

You should now be able to see two terminals running one above the other. Move back to the first buffer with CTRL-a then TAB, make sure it is showing the first window with CTRL-a then 0, and split it vertically with CTRL-a then | (Pipe). Then move into the new buffer with display the second window with CTRL-a then 1. The second and third buffers will now be displaying the same window, you can prove this by typing some text, you will see it displayed in both buffers.

    CTRL-a, | – Split the screen vertically

Now to finish off move to buffer three with CTRL-a then TAB, display the third window with CTRL-a then 2, split the buffer vertically with CTRL-a then |, move into buffer 4 with CTRL-a then TAB, and lastly display the fourth window with CTRL-a then 3. Now you have a grid of four terminals all displaying in the same terminal.

On a side note you can change the name of a window with CTRL-a then SHIFT-a. Using this you could name the windows One, Two, Three, Four.

    CTRL-a, SHIFT-a – Rename the screen window.

Comments Off on Grid view in GNU Screen