Tag Archives: Bash

Making disk imaging a little easier

Greetings everyone. I recently created a Linux bash script that will add a text based user interface to one of the oldest disk imaging tools out there. The idea behind this was I tend to fat finger a bit so instead of typing out all of the command I would rather have a script handle the command syntax for me and all I would have to do is enter a few bits of data. After that the script would handle the rest.

This script uses the dd command for imaging. First let’s start off with the normal way dd is used.

checking for mounted devices
Before I use the dd command I check on which location in the file system the drive I want to image is located.
image being taken
This is the way the dd command is used on the command line.

Here’s the breakdown of the command:

sudo = this command provides the user with temporary root privileges

dd= the invocation of the dd command

if= This is the location of the disk that is to be imaged. In this case it’s /dev/sdb

of= the name of the output file. In this cases it’s image.dd

bs= this is the block size. DD takes data in chunks called blocks. The smaller the block the less errors you may have during imaging but it will take longer. The block size for this image is 2048K

Here’s the script in action.

This is the invocation of the dd script
This is the invocation of the dd script
Entering the location
Next the script will display a list of what devices are mounted to the OS The blkls command is used in the script to show this data. From this data a location of a mounted drive can be entered in.
entering output file name
The script is then going to ask for a file name for the image
Selecting the block size
After the file name is entered the script is going to ask for a block size. All the user has to do is enter a number 1-4 and a corresponding block size is selected
imaging started
Next the script will show what options the user selected and the imaging will begin. The script is still a work in progress. This is evident with the blank output in the current location field. The image is always located in the current working directory.

I plan on making tweaks and changes to this script. Once everything is done I’ll put the completed script in another post. Thanks for reading!


Bash IF statements

In computer programs sometimes you need something to happen if a condition is fulfilled. Other times if the condition is not fulfilled the program has to do something. In programming IF statements is one of the ways that is used to have a computer do multiple tasks based on a variable outcome. Here’s an example using a small bash script I wrote.

This script shows the user three options and prompts the user for a response. Either 1, 2, or 3.

If 1 is chosen then the computer will display the word “Yes”

If 2 is chosen the the word “No” will display.

If 3 is chosen then the program will quit.

Here's the first part of the script.
Here’s the first part of the script.
Here's the second part of the script.
Here’s the second part of the script.

Here’s the breakdown of the script:

  • shebang (#!/bin/bash)
    • This is telling the OS which programming language I’m using. In this case it’s the built in bash scripting language.
  • The text in blue is just comments for the programmers reference
  • I have the program echo or display the options to choose from to the user
  • The read command take user’s input and puts it into a variable. In this case the variable name is option1
  • Next are my IF statements. These statements use the option1 variable as a condition to check and see what code to execute. For example if the user selects 1 then the first IF statement will execute because option1 (which is the user’s choice) equals 1.
    • The IF statements require several things in order to work right:
      • The condition
      • Brackets to encase the condition [[ ]]
      • Spaces between the brackets and the conditional
      • A semicolon at the end of the brackets
      • A then statement
      • Code to execute if the condition is met
      • A fi statement to end the IF statement

You will notice that all of the IF statements in this script have all of these.

Here’s the script in action using all of the options available to choose from.

option 3 part 1

option 3 part 2

option1 part 1

option1 part 2

option2 part 1

option2 part 2

Depending on what option I give the program it will either display Yes, No, or quit the program with a termination message. This is the power of IF statements. A programmer can have the computer test a large amount of possible options all within one program. Great stuff!

Another option is to use ELIFs and the ELSE statement with the IF statement. With ELIF (Else If) I can combine multiple conditionals into one statement rather then using multiple IF statements. The ELSE  statement will execute if the conditional for the IF statement is not met. To show this I’m going to modify the top script with ELIFs and an ELSE statement.

mod script part 1
Not only did I modify the script but I’m also using a different text editor. The editor is called vim which stands for vi improved. Vi is included in every distro of Linux.
mod script part 2
Instead of having multiple separate IF statements I now only have one huge statement that houses all of the conditions I want the program to evaluate. If any text other the the numbers 1, 2, and 3 are chosen the program will quit with a termination message. I also modified the user prompt using the -p (prompt) option with the read command. 

invaild input part 1

invalid input part 2

For more information on bash IF statements see: http://codewiki.wikidot.com/shell-script:if-else

Thanks for reading!

What is Linux scripting?

Linux scripting or bash (bourne again shell) scripting is the technique of taking one or more commands in the Linux command line and placing them into a single text file to be executed. This technique can be used to automate command line functions and can also be used to run multiple commands using just a single typed command on the Linux command line. Linux scripts are just a series of Linux commands.

Here’s an example: say I want to display a multiple line welcome message while I’m in the command line. There are two ways (that I know of) to do this:

1- use the echo command to display the each line one at a time

You could use two lines to display a welcome message
You could use two lines to display a welcome message

2 – write a script that will display the entire message all at once

I used the nano text editor to write the script
I used the nano text editor to write the script .The “#!/bin/bash is called a shebang. It is used to tell Linux what “interpreter” to use when going through the lines written in the script. In this case I’m using the built in bash scripting language so I have to tell Linux via the shebang that I’m using the bash scripting language otherwise the OS will not understand what I’m writing.

After I finished writing the script in order to use it I have to make it an executable program. I do this using the chmod command.

making the script executable
First is the command invocation”chmod” then “u” which stands for user the “+x” means that I’m giving executable permissions to the user. Then the last part is the file I’m changing permissions on which is “welcome”

After I set the permissions I run the script.

running the welcome script
Instead of having to type two lines to display a welcome message I just had to type a single word and the script did everything else for me.

This is just a small taste of what scripting in Linux can do. Scripting can do mundane things like display text and also advanced OS commands like process management. Thanks for reading!