Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Wednesday, July 25, 2012

Piping input and Redirection - Linux

A program interacts with two essential principles: input and output. The Linux command line allows us to skew these principles in something called IO redirection and piping, which is a very helpful thing to be able to do. If you already know C++ then your knowledge of the << and >> operators will come in handy. 

About
 Redirection
  Redirection allows you to move output somewhere else. For instance, dmesg > file.txt would move the 
  would move the output of "dmesg" into file.txt instead of to standard output. Input redirection is like output
  redirection, but it gets its input from somewhere else.
  
 Piping
  Piping is using one program's output as another's input.

How to use IO redirection and piping
 We will be using dmesg and espeak for our example. If you cannot get a hold of espeak, it is a program that outputs text given to it as an argument as audio.

Output Redirection
The greater than symbol, ">", is used to shoot output into somewhere else, usually a file. An example would be ls > file.txt which puts the output of "ls" into file.txt.

Input Redirection
Much like >, < is used to redirect input. Input Redirection can be confusing. It uses a file or some other source to get the input for a program. An example would be espeak < file.txt. This will use file.txt as input for espeak.

Combining IO redirection
The following example is going to gather input from a file, get the output, and put it in another file.
 wc < my_files > wc_output. This calls wc on my_files and puts it to wc_output. It would look like this with parenthesis: (wc < my_file)>wc_output. To use this as input, you would need to do program < wc < my_files > wc_output. That is overly complicated, so we will use piping.

Piping
Piping is a very important concept. It uses the pipe bar ( | ) to use the output of one program as input for another. So far we have used ls to put its output in a file and then used espeak to read that file. Now we are going to combine the steps. ls | espeak. What that line means is that the output of ls is going to be piped to espeak as input. Of course, you can still use IO redirection to get espeak's output into some file by using >.
  

Saturday, December 24, 2011

How to install Minecraft on Fedora 19 and under

Minecraft is the only game which I still play on my computer. Minecraft is unique since it is made in a platform-independent programming language called Java. To install it on Fedora 15, you need to get a bit more involved than normal. Reading my previous post about BASH shell scripts may help you out a bit.

First, install sun's java runtime environment here (http://www.java.com/en/download/manual.jsp) Go to the Linux section and download the RPM. When it is finished it can be installed with the software install tool or rpm. That's have the challenge down. Now go to www.minecraft.net and download minecraft.jar. You will notice that when you try to run this .jar file it opens with the archive manager instead. Here is when the slightly harder part comes in.

Go to the directory you have minecraft.jar saved in and make a new document called "Minecraft.sh". In the next few commands replace [path] with the path to Minecraft.jar. Open Minecraft.sh and enter the following:
#! /bin/bash
java -jar [path]
save and close the file. Now make it executable by right clicking it and going to permissions, or using chmod +x from the command line. Double click the file and click "open in terminal". If the terminal opens and closes with quick succession, minecraft threw an exception. To find out what went wrong drag the Minecraft.sh file into a terminal that is already open and run it from there. Google the resulting text.

If everything did go according to plan, minecraft should start normally. If you enjoy mods, then you need to go back to step one and install jre 7 instead of jre 6. The reason I provided the jre 6 link is that it's considered the best option since most java developers are still using it to write their programs / mods.

Alternate Route
There is an alternate, slightly more advanced route to launching minecraft. Go to your desktop and make the text document "minecraft" or whatever you want to name it. Now, copy this information but, once again, change [path] to where your minecraft is located. USER_NAME should be replaced with your username. That entire string should be replaced with a path to the picture.


[Desktop Entry]
Version=1.0
Type=Application
Name=Minecraft
Comment=A game about moving blocks and killing monsters.
Exec=java -jar [path]
Icon=/home/USER_NAME/Pictures/Icons/Applications/Minecraft Icon.png
Path=
Terminal=false

Categories=Games
StartupNotify=false

  When you are done, give the file a ".desktop" extension. This will directly launch the program with a double-click rather than asking you to run it. If you have permission to become root, you can copy this to /usr/share/applications or ~.local/share/applications. Now you can access it from programs like the gnome menu or any type of launcher for even quicker access.
The content in this post is public property
Happy mining

Bash Lesson one - Learn the Linux command line

Bourne Again SHell, or Bash, is the shell most commonly used on Linux systems. Bash can be installed on Windows, BSD, and other variants of Unix. Most of the functionality that Bash provides really isn't bash at all, but external programs. Some examples are sudo, vi, emacs, and espeak, just to name a few. To complete this lesson you will need either vi or emacs and sudo. Vi is run from the command line like emacs, but emacs also has a graphical mode that will boot when you launch it from a terminal. Make sure that you are in the sudoers file.

Let's get started. Open up a new terminal (xterm, terminator, yakakua, etc) and type "cd /" and press enter. Cd changes directory, so you just moved from home (or whatever location you booted the terminal from) to the root directory. Now, without the next command cd isn't much good since you would be flying blind. Type "ls". This will show you everything in your current folder in a color coded fashion.

Now, we are going to try something a bit more difficult. While in this directory type "cd bin" and press enter. You can type ls in here if you want, but be prepared because this is a huge folder. While in this folder type "sudo vi "myScript.sh"" Be sure to include the quotes even though they are not needed.

This will open Vim, VI improved.Hit i on the keyboard to enter the text insertion mode. Now type the following in the file just like I have it structured.
#! /bin/bash
cd /
mkdir success
I will explain mkdir a bit later. To save the file from vi hit the escape button once followed by a capital 'Z' twice. (ex: [esc] ZZ.)This will close vi and save the file. If you were choosing to use emacs you can go to file>save or use the command C-x C-s. Now you should be back at the command prompt. This is probably the step that gets messed up the most. Now type "chmod +x myScript.sh" This just makes the shell script (which is what you made) executable.

We are now ready to run the script, but what do you think it will do? The two commands in it were cd / which you already know and mkdir success. What this does is changes directory to root and makes a directory called success.

To test this out, type "sudo myScript.sh". Running it with administrative privileges is a must because of where it makes the folder. Now, type "cd /" again and "ls". If you have done everything correctly you should see blue letters that say "success" on them.

BASH gets much more confusing than this, but for now it's a good start.




The same exercise as a non-root user
This is the same exercise from above but I will use your home directory instead. If you have already read parts of it, you can skip over right to the commands.


Let's get started. Open up a new terminal (xterm, terminator, yakakua, etc) and type "cd" and press enter. Cd changes directory, so you just moved from home (or whatever location you booted the terminal from) to the home directory. That is useful if you get lost and need to get back to a checkpoint. Now, without the next command cd isn't much good since you would be flying blind. Type "ls". This will show you everything in your current folder in a color coded fashion.

Now, we are going to try something a bit more difficult. While in this folder type "sudo vi "myScript.sh"" Be sure to include the quotes even though they are not needed.


This will open Vim, VI improved.Hit i on the keyboard to enter the text insertion mode. Now type the following in the file just like I have it structured.
#! /bin/bash
cd
mkdir success
I will explain mkdir a bit later. To save the file from vi hit the escape button once followed by a capital 'Z' twice. (ex: [esc] ZZ.)This will close vi and save the file. If you were choosing to use emacs you can go to file>save or use the command C-x C-s. Now you should be back at the command prompt. This is probably the step that gets messed up the most. Now type "chmod +x myScript.sh" This just makes the shell script (which is what you made) executable.


We are now ready to run the script, but what do you think it will do? The two commands in it were cd which you already know and mkdir success. What this does is changes directory to home and makes a directory called success.


To test this out, type "myScript.sh". Now, type "cd" again and "ls". If you have done everything correctly you should see blue letters that say "success" on them.