Linux Blog

Bin and Cue Support for Linux

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 2:35 pm on Tuesday, March 18, 2008

If you have a bin/cue image that you need to use under Linux you may be in for a surprise. I recently downloaded a copy of a game that I owned but was too scratched to use. The problem was the game came in bin / cue format. To fix this I just used a nifty little program called bchunk and converted it to ISO. It can be found here: http://he.fi/bchunk/

I would really like to find a way to mount bin and cue images under Linux but so far I’m not having any luck. I’m wondering if with some modification the bchunk program could be used with fuse.

Links -g Graphical links

Filed under: General Linux,Linux Software — Kaleb at 12:01 am on Tuesday, February 5, 2008

Hello I am Kaleb Porter from http://kps-blog.blogspot.com writing a post here about links -g, the graphical text web browser. I am sure your asking your self by now, “graphical text web browser? that makes no sense.” well your pretty much right.

What links -g is, is the text based web browser “links” in it’s own X window. This gives it the ability to display images which is very neat actually.

links -g google
Neat right?
Links -g still uses the same old links keys that we are used to from our cli versions that we love so dear. And if you don’t use gpm, you can now use your mouse along with your browser. However if your experienced with gpm this feature may be old news to you.So your asking yourself, “Why the hell do I care about this?” well links -g is an amazingly fast web browser. So if your like me and completely upset at the horrid speed of today’s full featured web browsers… Opera, Firefox, or if your in MacOS Safari, and IE for Windows, then you will love the super fast speed of links -g. Also if your the type of person I am who just flat out likes the simple stuff, or the power user using a nice tiling window manager like dwm or something and you want to be able to display images in your web browser, then you will love links -g.

Sounds great eh? Well it truly is there are drawbacks however most of who will want to use links -g don’t mind these so called drawbacks.
1. Flash.
OK OK so it doesn’t support flash playback…big deal, hey it’s a TEXT based web browser that just happens to be running in X so to support images. You have to give it credit for doing that. And doing that very well.
2. No built in file browser.
OK for this you might be wondering, “What file browser in my web browser?” Well there is a file browser in Web browsers such as Firefox and Opera. These file browsers allow you do do things like pick a file you want to upload to say Photobucket or something. It can still be done, you just need to know where on your system, the file you want to upload is.
3. Other animation software (Java… etc.)
Well you can’t just expect this thing to have support for super cool animation effects from Java because it just doesn’t have a Java plug-in. Note that this is NOT Javascript. Javascript and Java are two different technologies. Javascript is fully supported under links -g.

To install:

In Gentoo:
Make sure you have the proper use flags set up….(png, jpeg, svga, tiff, javascript, X, and ssl if you want it.

emerge -av links

In Arch Linux:
Everything should be set up for you on Arch so just make sure you have libsvga installed (it may be installed when you install links as a dependency).

pacman -Sy links

To run links in graphical mode:

links -g

or

links2 -g

Have fun!

Decision making in Bash If / Then / Else statements and Cases

Filed under: Shell Script Sundays — TheLinuxBlog.com at 3:44 pm on Sunday, August 5, 2007

There comes a time in every shell script where a decision has to be made.

To make a decision in bash the following if then else syntax can be used:

if [ condition ]
then

statements

[ elif [ condition ]

then

statements ]

[ else

statements ]

fi

Anything in non bold [ brackets ] is optional, and they should not be typed but the bold in them is required. Anything in bold must be typed. Statements are generally other commands or flow control statements.

To give an example of how to use bash if then else statements in the real world take the following scenario:

A system administrator has a strict habit of firing people that have too many .png files. He checks the systems regularly and makes sure that nobody has too many. The following script will display a message depending on the number of .png’s that are in the directory.

#!/bin/bash
gif_count=$(ls | grep .png | wc -l)
echo “Number of PNG’s: $gif_count”
if [ $gif_count -lt 10 ]
then
echo “He will be happy, you have less than 10 files”
elif [ $gif_count -gt 10 ] && [ $gif_count -lt 20 ]
then
echo “Consider deleting some files”
else
echo “you have too many files, DELETE SOME BEFORE HE FINDS OUT!”;
fi

Using Cases.

Cases are similar to if statements in that they match a expression and perform a task if the pattern is matched.

bash case syntax:

case expression in

pattern1 ) statements ;;

pattern2 ) statements ;;

esac

This is fairly simple and some people find this easier than doing if statements for simple logic. Take the following real world example:

The system administrator has recently gone on a bigger power trip than before. Since people got wise about using png’s and started saving images in other file formats he is now monitoring png’s gif’s and jpg’s. To combat the problem, you can use a case to count how many files you have of each type. (This is intended as an example, there are many ways to accomplish this task, this is just to demonstrate how cases work)

#!/bin/bash
#set all variables to 0
png=0
gif=0
jpg=0

# start loop
for wc in $(ls); do

case “$wc” in
*png ) let png=$png+1 ;;
*gif ) let gif=$gif+1 ;;
*jpg ) let jpg=$jpg+1 ;;
esac

# end loop
done

echo “Png’s $png”;
echo “gif’s $gif”;
echo “jpg’s $jpg”;

There you have it, two ways to make basic decisions in bash. Just figure out what you want to do then use an if then else, or a case statement to do the logic. I myself prefer if statements over cases as they make more sense to me and I find it easier to perform logic within ifs.

Programs used in this post
ls, echo, grep, wc

Shell Scripting 101

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:27 am on Sunday, August 5, 2007

In my last shell scripting article I made a script to manipulate images with imagemagick. The script was only five lines long and has saved me endless hours of manual work. The script can be adapted to do anything repetitive on any types of files.

Since this is my first official shell scripting post I am going to introduce some common basic shell scripting. This is intended for people that want to learn the basics of shell scripting, so those that have experience already may want to skip this post.

Basic applications used in this tutorial
In order to teach a little about shell scripting, I need to use a couple of programs other than bash its self. The ones that I will use in this post are:
echo
This program displays a line of text
cat
Cat likes to read files and print what it finds out.
grep
Search through files with an expression to find a line that matches the pattern. Sounds complicated but really is not.
less
A paging utility is used to display data. Its kind of like opening a file in notepad Windows except with most utilities more can be done.
less is probably my favorite pager. There are many of these including tail and more. Less is more is the tag line of this pager because it does more than less but is the same thing.

Basic standard output redirecting
Often abbreviated as stdout or stderr for standard out and standard error.
This is normally used to take the output of one application and direct it somewhere. You can overwrite or append to the stdout. The symbol to do this is > to overwrite and >> to append.
stdin example:

echo “This is the new text” > File_To_Be_Written
echo “This is appended” >> File_To_Be_Written

“This is the new text” (without quotes) is redirected to File_To_Be_Written and overwrites the file.

“This is appended” is appended to the end of the file that was just created. If the file did not exist before hand, the file would have been created.

Redirecting Errors
Redirecting errors is achieved the same way as redirecting stdout except it has a 2 in front of the greater than symbol.
stderror example:

echo “This is the new text” 2> File_To_Be_Written
echo “This is appended” 2>> File_To_Be_Written

To append to stderror just think “To redirect to stderror is to redirect with two” Meaning, to redirect stderror is just to redirect with two in front. Cheesy, but its easy to remember.

Pipes
Piping is a common technique I normally use in all of my scripts. The way this works is to send the stdout of one program to the stdin of another.
Example:

cat Big_File | grep search | less

This basically reads the Big_File and then grep’s the file for lines that have search in them. It then pipes that output into the paging utility less.

Lists
A list is just a sequence of pipes that are separated by control operators. Basically
what that means is you have operators that do specific things depending on return codes

&
This operator puts the command into the background, gives a return status of 1 and moves on to the next task.
Example:

owen@amd:~$ echo “tails” > heads
owen@amd:~$ cat heads & echo “done”
tails
[1] 15385
done
[1] Done cat heads

;
The ; operator waits until the process is finished before moving onto the next task.
Example:

owen@amd:~$ cat heads; echo “done”
tails
done

&&
command1 && command2
command2 will only be executed if command1 returns an exit status of zero
Example:

owen@amd:~$ cat heads && echo “done”
tails
done

Note this works because cat has returned the correct exit status

owen@amd:~$ cat eads && echo “done”
cat: eads: No such file or directory

Notice that this does not work because cat did not return the correct exit status.

||
command1 || command2
command2 will be executed if command1 returns a non zero return code.
Example:

owen@amd:~$ cat heads || echo “Did not work”
tails
Did not work is not printed because cat exited properly
owen@amd:~$ cat eads || echo “Did not work”
cat: eads: No such file or directory
Did not work

Did not work was printed because the eads file does not exist.

Throwing it all together:

echo “This is whats in the output file” > output;
echo “Now that has done, lets add something to the end” >> output;
cat output > normal_output && echo “This is not the error log. Press Q to exit this pager and view the error log” >> normal_output;
cat put >> normal_output 2> error_output;
cat normal_output | less ; cat error_output | less

This short example basically echos some text to the output file. It then adds some more to the end of the output file. Next it reads from the output file into the normal_output file. If that executes successfully it goes on to echo some instructions to the normal_output file. The script moves on to read the put file (which does not exist) and directs the error of that command to a error_output file. After that, it reads the normal output and sends it to the less pager. The first prompt has instructions for ending the pager, and once the pager has been stopped it reads the error log.

Variables
Variables are used to temporarily store data in memory. To read a variable you can just just the echo command. An example of setting and reading a bash variable is below:

owen@amd:~$ variable=’This is my variable’
owen@amd:~$ echo $variable
This is my variable

Flow Control
There are ways of controlling how a script is going to work. This is the logic part of the script, the intelligence if you will. There are many different ways to control flow with bash. The following topics should get you started:
Bash If / Then / Else statements & Cases
Bash Loops For, While and Until
Creating menus in Bash with select

Where to from here?
If you followed this example and understand everything so far then you have got your feet wet in shell scripting. You should now know a little on how to read/write files, redirect output and use conditional operators.
If you would like more help there are plenty of resources on-line and probably on the computer you are using to try the examples. Check the man pages for more help on the utility and “man bash” for bash help. If your looking for more tutorials go to Google and type “bash shell scripting tutorial”.
If you have a specific question that needs answering leave a comment or contact me.

When Photoshop Fails

Filed under: General Linux,Shell Script Sundays — TheLinuxBlog.com at 9:50 pm on Monday, January 15, 2007

I was recently assigned the task of reducing the quality of over 1000 images. My first instinct was to use photoshop’s batch functions to complete the task. After playing around with it for a little while it became apparent that you cannot save files for web with spaces in. Photoshop had failed me. My next idea was to use a bash script to loop over every file and process the image. This was sure to work. Since I have prior experience with image processing I decided to use ImageMagick to complete the task. The command to adjust the quality is:

convert (FILENAME) -quality 50 (FILENAME)

This command is useless on its own so using a for loop I came up with:

#!/bin/bash
for i in $(ls); do
convert $i -quality 50 $i
done

This script would have worked for me if there were no spaces in the filenames. Because there were it took each filename that had spaces and ran the command on each part of the file name. Unfortunately this was not going to work.

After googling for a while it became apparent that I was not the only person to have the problem of spaces in filenames with for loops. The solution I found was to use the find command and a while loop. Below is working script that successfully completed the task in no time:

#!/bin/bash
find * -iname “*” | \
while read I; do
convert “$I” -quality 50 “$I”
done

This script could be easily modified to take advantage of ImageMagick’s many other functions. For example it could be used to batch resize a folder of images to make thumbnails by changing the command to

convert “$I” -resize 200 “$I”

Or it could be used to overlay text onto an image with the following command:

convert “$I” -gravity southeast -annotate +5+10 “thelinuxblog.com” -fill black “$I”

As shown above ImageMagick is really powerful image editing software which can easily be used with the bash shell to process thousands of images with no trouble.