Linux Blog

Making ISO’s with dd

Filed under: General Linux — TheLinuxBlog.com at 4:59 pm on Monday, July 26, 2010

Creating an ISO Image under Linux from the command line is a really easy process. Fire up your favorite terminal and type the following:

dd if=/dev/cdrom of=<iso image name>

The if is for input file and the of for output file. There are lots of options for dd so check out the man page.

To mount the newly created image (as root) you can mount it as a normal device with the -o loop option:

mount -o loop <iso image name> /mnt/<dest dir>

Backup your DVDs on Linux

Filed under: General Linux — TheLinuxBlog.com at 12:01 am on Wednesday, November 4, 2009

At an average of around $20 per DVD* most can’t afford to have any of their DVDs lost, stolen or broken. Backing them up is a touchy subject depending on who you talk to, but here is how to do it anyway.

Backing up a DVD on Linux used to be much harder than it is with K9Copy. No more flaky DVD Shrink crashes under wine. K9Copy takes the hassles out of creating backups of your DVDs under Linux. As the name implies it is KDE software but works well under gnome provided you have the needed libraries installed.

It has many options to backup and really is pretty comparable in functionality to the infamous DVD Shrink for Windows. Take a look at the screenshots and give it a try yourself:

* 2007 US Entertainment Industry Report (www.mpaa.org/USEntertainmentIndustryMarketStats.pdf)

A Manpage Template for your Scripts

Filed under: Shell Script Sundays — TheLinuxBlog.com at 11:44 am on Sunday, June 21, 2009

So, you just finished that killer script and the first thing you want to do is write that man page for it right? Not exactly? What do you mean?
Writing a man page isn’t that hard and will give your users an idea of how to use a script.

Here is a basic template for creating your own man pages:

.\” This is a comment
.\” Contact Owen@thelinuxblog.com
.TH man 1 “21 June 2009″ “.1″ “Man Page Example”
.SH NAME
Man \- Example Man Page
.SH SYNOPSIS
Example Man Page
.SH DESCRIPTION
This is an example of a man page.
.SH OPTIONS
No options for this man page
.SH SEE ALSO
man(1), groff(1), man.config(5)
.SH BUGS
No known bugs for this template, except you might want to replace the quotes if you copy from my blog.
.SH AUTHOR
Owen (owen@thelinuxblog.com)

When you’re done with the gruelling task of writing documentation (which your users won’t read or thank you for) just plop it the correct man section in /usr/share/man
That’s all there is to it!

rsync to smb share

Filed under: General Linux — TheLinuxBlog.com at 4:30 pm on Thursday, September 11, 2008

If you happen to have a SMB share with a lot of disk space laying around, then you may have considered backing up to it. There is more than one methods that you could back up to a SMB share but this article will show how to rsync to a smb share. This blog post assumes that you have successfully set up your SMB share and have installed RSync.
(Read on …)

Creating Script Parameters With getopts

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:06 am on Sunday, October 14, 2007

Many programs for Linux have parameters that can be given at run time. These are also known as switches, arguments or options. These parameters make it easy to tell a program or script what to do and what options to use. In this Shell Script Sundays Blog post I will show you how to implement these in a script by using getopts.

For this example to work the following must be placed in a script:

while getopts “:e:” ARG;
do case “${ARG}” in
e) echo “${OPTARG}”;;
esac;
done

This code basically gets loops around the arguments. All this script does is take the value after the -e and echo’s it out. In the example below I named the script getopts.

owen@the-linux-blog:$ ./getopts -e “Hi There Linux Blog Viewers”
Hi There Linux Blog Viewers

For each extra parameter that is needed a new constant to the getopts and do loop need to be added. For example if the option -e and -q need to be in the script then the following getopts loop would be created:

while getopts “:e:q” ARG;
do case “${ARG}” in
e) echo “${OPTARG}”;;
q) echo “${OPTARG}”;;
esac;
done

Of course the above script for -q also only echo’s out the value for -q as seen below:

owen@the-linux-blog:$ ./getopts -e “Hi There Linux Blog Viewers” -q “Another Option”
Hi There Linux Blog Viewers
Another Option

This is all very well, but documentation is always nice, even if you think you know how to use your script you may forget in the future. Its also nice if you have a script that other people can easily use. That being said its good to have a way to show users how to run the script.

usage () {
echo -e “Usage: $0 -e \”What To Echo\” [ -q \"Output\" ]”
echo -e “\t-e: specifies what to echo (required)”
echo -e “\t-q: Where to write output to. If not specified the output is written to the console”
}

while getopts “:e:q:” ARG;
do case “${ARG}” in
e) ECHO=”${OPTARG}”;;
q) OUTPUT=”${OPTARG}”;;
esac;
done

[ -z "${ECHO}" ] && { usage && exit 1; }

[ "${OUTPUT}" ] && { echo $ECHO > $OUTPUT; } || { echo $ECHO; }

The code above takes the options and assigns a variable to each of the options $ECHO is what to echo and $OUTPUT is where to write the output to. The script calls the usage() function and exits whenever the required option ($ECHO) is not set. If $ECHO is set it checks to see if $OUTPUT is set, if so it echo’s the contents of $ECHO to the $OUTPUT variable (file or device). If $OUTPUT is not set then it just echo’s the $ECHO variable normally. This is the script running with its various different actions:

owen@the-linux-blog:$ ./getopts
Usage: ./getopts -e “What To Echo” [ -q "Output" ]
-e: specifies what to echo (required)
-q: Where to write output to. If not specified the output is written to the console

owen@the-linux-blog:$ ./getopts -e “The Linux Blog getopts Example”
The Linux Blog getopts Example

owen@the-linux-blog:$ ./getopts -e “The Linux Blog getopts Example. Output To Null” -q /dev/null

owen@the-linux-blog:$ ./getopts -e “The Linux Blog getopts Example” -q Write_To_This_File
owen@the-linux-blog:$ ls
Write_To_This_File getopts
owen@the-linux-blog:$ cat Write_To_This_File
The Linux Blog getopts Example

As there are many different variations and each implementation would be different I can not cover each individual getopts scenario but by assigning variables your option arguments you should be able to get switches working in your own shell scripts.

Using cut – Shellscript string manipulation

Filed under: Shell Script Sundays — TheLinuxBlog.com at 1:21 am on Sunday, August 26, 2007

This post is designed to be a refresher, reference or quick intro into how to manipulate strings with the cut command in bash.

Some times its useful to take the output of a command and reformat it. I sometimes do this for aesthetic purposes or tor format for use as input into another command.
Cut has options to cut by bytes (-b), characters (-c) or fields (-f). I normally cut by character or field but byte can come in handy some times.
The options to cut by are below.

N          N’th byte, character or field, counted from 1
N-         from N’th byte, character or field, to end of line
N-M      from N’th to M’th (included) byte, character or field
-M         from first to M’th (included) byte, character or field

The options pretty much explain themselves but I have included some simple examples below:
Cutting by characters (command on top, output below)

echo "123456789" | cut -c -5
12345

echo "123456789" | cut -c 5-
56789

echo "123456789" | cut -c 3-7
34567

echo "123456789" | cut -c 5
5

Sometimes output from a command is delimited so a cut by characters will not work. Take the example below:

echo -e "1\t2\t3\t4\t5" |cut -c 5-7
3       4

To echo a tab you have to use the -e switch to enable echo to process back slashed characters. If the desired output is 3\t4 then this would work great if the strings were always 1 character but if anywhere before field 3 a character was added the output would be completely changed as followed:

echo -e "1a\t2b\t3c\t4d\t5e" | cut -c 5-7
b       3

This is resolved by cutting by fields.
Cutting by fields

The syntax to cut by fields is the same as characters or bytes. The two examples below display different output but are both displaying the same fields (Fields 3 Through to the end of line.)

echo -e "1\t2\t3\t4\t5" | cut -f 3-
3    4    5

echo -e "1a\t2a\t3a\t4a\t5a" | cut -f 3-
3a      4a      5a

The default delimiter is a tab, if the output is delimited another way a custom delimiter can be specified with the -d option. It can be just about any printable character, just make sure that the character is escaped (back slashed) if needed. In the example below I cut the string up using the pipe as the delimiter.

echo "1|2|3|4|5" | cut -f 3- -d \|
3|4|5

One great feature of cut is that the delimiter that was used for input can be changed by the output of cut. In the example below I change the format of the string from a dash delimited output and change it to a comma.

echo -e "1a-2a-3a-4a-5a" | cut -f 3- -d – --output-delimiter=,
3a,4a,5a

Formatting with Cut Example

Sometimes certain Linux applications such as uptime do not have options to format the output. Cut can be used to pull out the information that is desired.
Normal up-time Command:

owen@the-linux-blog:~$ uptime
19:18:40 up 1 day, 22:15,  4 users,  load average: 0.45, 0.10, 0.03

Time with up-time displayed:

owen@the-linux-blog:~$ uptime |cut -d , -f 1,2 | cut -c 2-
19:19:36 up 1 day, 22:22

For the above example I pipe the output of uptime to cut and tell it I want to split it with a comma , delimiter. I then choose fields 1 and 2. The output from that cut is piped into another cut that removes the spaces in front of the output.
Load averages extracted from uptime:

owen@the-linux-blog:~$ uptime |cut -d , -f 4- | cut -c 3-
load average: 0.42, 0.10, 0.03

This is about the same as the previous example except the fields changed. Instead of fields 1 and 2 I told it to display fields 4 through the end. The output from that is piped to another cut which removes the three spaces that were after the comma in "4 users,  " by starting at the 3rd character.
The great thing about cutting by fields is that no matter if the field length changes the data stays the same. Take the example below. I now have 17 users logged in which would have broke the output if I had used -c (since there is an extra character due to a double digit number of users being logged in.)

owen@the-linux-blog:~$ uptime
19:25:11 up 1 day, 22:28, 17 users,  load average: 0.00, 0.06, 0.04

owen@the-linux-blog:~$ uptime |cut -d , -f 4- | cut -c 3-
load average: 0.00, 0.06, 0.04

That just about covers everything for the cut command. Now you know about it you can use cut to chop up all types of strings. It is one of the many great tools available for string manipulation in bash. If you can remember what cut does it will make your shell scripting easier, you don’t need to memorize the syntax because all of the information on how to use cut is available here, in the man pages and all over the web.