Showing posts with label commands. Show all posts
Showing posts with label commands. 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 >.
  

Sunday, June 17, 2012

Living in the Command Line

Some people, usually extreme power users, server owners, and hobbyists choose to use the command line as their primary means of communicating with their computer. Richard Stallman has said that he finds that graphics "distract" him. The command line's learning curve is high since you must know about the command you are issuing, and because a lot of applications are now being developed with their front AND backends using GTK or QT. This article will teach you how to block everything else out, and use the command line for daily life.

Tty's
Tty's are separate logins. You may not know it, but you have 7 open right now if you are using a Linux box. To enter a tty, do this: alt+ctrl+a number 1-6. When you do alt+ctrl+7 from any tty, you will re-enter graphics. Some prefer to use a tty instead of a terminal. Each has a login prompt, where you enter your user name. Then, your password. Don't be surprised if your num lock key is turned off or if you do not see the characters of a pssword being typed. It does this for your safety.

Now that you are in a tty, there are a few things to learn about everyday usage.

Editing Text
To edit text from the command line, you can use GNU Emacs or VIM if either are installed. Otherwise, you will have to use vi, which is usually installed by default on any distribution. Each of the programs have different commands that they use. This can also be used to edit music, movies, and pictures.

Browsing the internet
yes, you can browse the internet from the command line! Download nad install something called links2, which is a textual web browser. From the command line, use the commnad links www.website.com. In links2, you can hit "g" on a non-input area to go to a different website. Sites like Google will work correctly. However, some that use Flash and some that use Javascript will not. Of course, no pictures will be available in this way.

Watching Movies
Wait, you can watch movies from the command line? Yes! You can watch movies in the command line. Download and install mplayer2 (sudo apt-get install mplayer2), then use (mplayer /home/user/movie). This will use the ncurses library to make colored letters to let you watch your video. On X, you can use this and it will create a window to show it in.

Send Email
http://www.simplehelp.net/2008/12/01/how-to-send-email-from-the-linux-command-line/
This works for some people, but my ISP doesn't allow this to happen. Instead, I have to use smtp, which is a bit harder.
http://tombuntu.com/index.php/2008/10/21/sending-email-from-your-system-with-ssmtp/
That explains this in-depth.

Move files
cd changes your directory. ls lists directories. mv moves a file. You can also rename files with mv. To make files, use mkdir. To remove them, use rm.

Kill Processes
Sometimes a process will run long in a tty. When this happens, log into another tty and issue ps -aux
This will list processes and ID's. Then, you can use (kill (ID NUMBER)) (Example: kill 23)

KILL, KILL, KILL
This is a very popular way to remove everything on your computer. Do NOT fall for this anywhere where it may tell you.
sudo rm -rf /
Do not use this command unless you absolutely want to


Tips
man describes a program.
The tab key will autocomplete a statement. I cannot put enough emphasize on how useful this is.

http://www.ubuntugeek.com/useful-and-fun-things-to-do-with-the-ubuntu-terminal.html
This website houses a bunch of cool information.

Sunday, April 29, 2012

How to package a .deb

          On Debian-based systems, the package manager uses .deb containers to handle the installation of packages. Many people make a connection between .deb and .exe since both are easy formats that usually lead to the installation of a program, but this is not the case. Deb files are more like an advanced .tar, and they don't even need to be used for installing programs. In this tutorial I will show you how to make and build a debian package. Make a new directory, let's say for instance, mydeb. Then, in the mydeb directory make a folder named DEBIAN. Now we can begin.

There are a few essential things to put into DEBIAN. You need to have a control file called control, a pre-installation script called preinst, and a pre-removal file called prerm. The control file uses a special syntax to define dependencies, make a description, and note the authors. Here is a sample control file:

Package: mydeb
Version: 1.0.0-0ubuntu1
Architecture: amd64
Maintainer: You <You@email>
Installed-Size: 154
Depends:
Section: accessories
Priority: optional
Description: A sample debian package.

Package is the name of the package, in this case it's mydeb. The version is the version of the file with any special revision names included. Architecture is what type of CPU it can run on. Depends is the most important, which lists dependencies. The operations > < and >= <= can be used to specify higher or lower version numbers for dependencies. The section is what type of application it is. Priority is whether or not the package is a necessity. In this case it's not. Description is a long description of what the package can do. This can be multiple lines.

Now that the control file is out of the way, make two scripts called preinst and postinst. These are bash scripts (text files with #!/bin/bash at the top) that list the commands to install and remove the parts of the program that were included on install. ldconfig should be called in either file.

The fun part
The fun and arguably the most important step is specifying the directories where files will be installed. Leave the DEBIAN folder and make one called home. Then, make one that is exactly equal to your home folder. Make a sample text file here. When the .deb is installed that text file will be placed in your home folder. This is used to spread resources across the files system, for instance the icon goes in /usr/share/pixmaps. You actually have to create those directories inside of the parent directory.

Now you should have two directories in the parent directory, DEBIAN and home. Now we can package these with the dpkg command. Enter the terminal and use the command
dpkg -b 'PATH_TO_PARENT_FOLDER' myDeb.deb


This should create a file in your home folder called myDeb.deb. Now you can install the package with gdebi, the software center, or dpkg.

A note for application developers: ldd lists the dependencies of an executable file for the Depends: part of a command file.

External: http://www.linuxfordevices.com/c/a/Linux-For-Devices-Articles/How-to-make-deb-packages/

Saturday, December 24, 2011

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.

Sunday, March 13, 2011

Windows cmd lesson 1 - The basics

Cmd, or command prompt, is the shell of the Windows Operating system. It is here where you can tell your computer to execute commands in the simplest way possible; text commands. There's no fancy user interface to tell it what to do. Just a black screen with white text.

 To run cmd on Windows 7 type "cmd" into the search box and hit enter. It will run automatically. On earlier versions of windows you will have to use Run and then type cmd+enter. You will be greeted with C:/Users/user: | This is where you can tell cmd what actions you want done. If you are not happy with the color setup you can type COLOR (Numbers 1-100 or letters) to change the color. Color 3 and Color a are both greens. Color A than color 3.
Once you have your color scheme arranged it is time to tell it to start doing commands. You could start off simple by finding out your IP address with Ipconfig. Another useful command is Tracert followed by an IP address or a URL. This will show how many hops (steps) it takes to reach a destination on the web from your current location. This will count for a maximum of 30 hops unless you type -h (number) after the tracert commands. This will tell cmd exactly how many hops you want to set as a maximum. -w  (number) will tell it how long to wait before it considers the connection "time out" or lost. If you have a slow connection speed this will be your best bet.

The next and most widely used command on cmd is
Ping. By default Ping sends a request for 32 bytes of data to a server and waits for it to send the 32 bytes back. It will do this a default of 4 times before telling you your highest, average, and lowest connection speed in milliseconds. This is a good tool for testing internet connection speed against a server or to see if an internet connection problem originated from your connection or your website. Another (illegal) use for this is attacking a website with massive ping requests in an attempt to slow it down. This can be done using the commands -t (makes the ping process go until it is interrupted) and -l (number of bytes after -l). The higher the number of bytes the higher amount of work the server has to do. There is a cap of 64,000 bytes, but most computers can't handle that with the default timeout time. Once again, typing -w (time) will allow you to reset that amount.
The last commands are Cls and Title. CLS just clears the cmd screen from all previous text. Title (followed by text) is what the name of the cmd window will be. So for instance if you wrote Title The legionnaire's den. the title of the cmd window would change from cmd.exe to "The Legionnaire's Den.".