Xterm

(move some content to Terminal)
(The basics: move more content to Terminal)
Line 2: Line 2:
===The basics===
===The basics===
-
''user@computer:~$'' '''echo "hello world"'''
 
-
 
-
hello world
 
-
 
-
:says/outputs/prints text to sdtout
 
-
 
-
 
-
''user@computer:~$'' '''echo $USER'''
 
-
 
-
user
 
-
 
-
:says/outputs/prints the currently logged in user's name
 
-
 
-
''user@computer:~$'' '''uname -r'''
 
-
 
-
2.6.21-omap1
 
-
 
-
:List the kernel version. the -a parameter outputs all known information
 
-
 
-
 
-
''user@computer:~$'' '''cat /etc/resolv.conf'''
 
-
 
-
'''#'''nameserver 127.0.0.1
 
-
 
-
nameserver 192.168.1.1
 
-
 
-
'''#'''nameserver 4.2.2.2
 
-
 
-
:Cat spits out an entire text file to terminal. resolv.conf (remember *NIX is case-sensitive) is where DNS servers are stored. The # pound character comments/"comments out" aka disables a line of text in a BASH script/file
 
-
 
-
 
-
''user@computer:~$'' '''ps | awk '{ print $5 }''''
 
-
 
-
...
 
-
 
-
/usr/bin/osso-xterm
 
-
 
-
/usr/libexec/gnome-vfs-daemon
 
-
 
-
bash
 
-
 
-
ps
 
-
 
-
awk
 
-
 
-
:ps lists running processes, you might also be interested in the interactive top command. piped to awk which in this case says to only show the 5th column (separated by tab characters)
 
-
 
-
 
-
''user@computer:~$'' '''foobar='example'; echo $foobar'''
 
-
 
-
example
 
-
 
-
:Basic example of a variable
 
-
 
-
 
-
''user@computer:~$'' '''uname -s > /dev/shm/os; cat /dev/shm/os'''
 
-
 
-
:Example of directing output to a file. Files stored in /dev/shm are stored directly in RAM and thus are deleted on shutdown
 
-
 
=== Writing a shell function ===
=== Writing a shell function ===

Revision as of 09:31, 28 June 2010

Contents

This page requires major clean-up!

The basics

Writing a shell function

user@computer:~$ minute() { foo=`date +%S`; echo $foo; if [ "$foo" -lt "30" ]; then echo "under half"; else echo "over half"; fi; }

Simple shell function that uses a variable and if/then/else logic

user@computer:~$ minute

29

under half

Using the shell function we just wrote, outputs the current time in seconds (only the seconds part) and whether it is closer to the last minute or next minute. Shell functions are only available for the terminal session. Save in .bashrc or .profile to be persistent/saved

user@computer:~$ minute

31

over half

Intermediate

user@computer:~$ /usr/bin/camera &

Everything is run from a terminal, quite literally in *NIX. A GUI program is a running instance of a terminal process. Therefore the camera program can be launched. The & amperstand tells it to disassociate from the current terminal and create it's own process


user@computer:~$ ln -s /sbin/ifconfig /usr/bin

The user PATH does not include /sbin for some reason by default on NITs, which is annoying as typing in /sbin/ifconfig - which should be in /usr/bin/ anyway. Thus we are going to symlink it.


user@computer:~$ ifconfig | grep "inet addr"

Example use of grep. grep outputs the entire line matching the pattern, in this case "inet addr" which is piped into it from the output of ifconfig using the | pipe character


user@computer:~$ ifconfig | grep "inet addr" | awk '{ print $2 }' | cut -d : -f2

Advanced example pipes from ifconfig to grep to awk and finally to cut to output the clean IP address which can then be fed into something else if desired


Logged in as root from here on out. Not necessary for every command but for some of them. If you have sudoer installed you can simply prefix each command with sudo. If not you are gonna use an equivalent of sudo su - or simply login a root using another method. For easyroot users, simply type $ root and enter.

This next section is Debian-based specific

root@computer:~# apt-cache search nano

nano-tiny - free Pico clone

traceroute-nanog - Determine route of packets

nano - nano editor

Searches repos for packages by keyword


root@computer:~# apt-get install nano

install package nano. It's an easy to use CLI text editor.


root@computer:~# wget http://nitstuff.appspot.com/dists/chinook/user/binary-armel/nano_2.0.6_armel.deb

Instead of using the repos using wget to download .deb package from website.


root@computer:~# dpkg -i nano_2.0.6_armel.deb

Installing locally saved debian package.


The following is networking specific

root@computer:~# ssh name@192.168.1.100

...

Are you sure you want to continue connecting (yes/no)?yes

name@desktop:~$ exit

SSH example. Specify the user _at_ the IP address of the computer you want to access. Keep in mind that this works only over LAN by default (you need to forward port 22 on the router and give outside IP address or use some type of fake VPN). If you get a RSA error message, simply delete ~/.ssh/known_hosts using $ rm ~/.ssh/known_hosts Type exit to leave remote session


root@computer:~# scp name@192.168.1.100:/home/name/randomfile /home/user/

user@computer:~$ ls /home/user/randomfile

File is copied from remote computer to local computer to user's home directory. Root's home directory is /root


root@computer:~# ssh name@192.168.1.100

name@desktop:~$ screen

name@desktop:~$ hello

Leaves a shared session open

friend@laptop:~$ ssh name@192.168.1.100

name@desktop:~$ hello world

Allows you to work with a friend or simply resume a session later. Good for long tasks that need to disconnect from a machine and reconnect later.


root@computer:~# ssh name@192.168.1.100

name@desktop:~$ screen -ln nano /etc/resolv.conf

[Nano instance]

[CTRL]+[A]+[D]

Detaches from a screen leaving a process running on a remote machine

name@desktop:~$ exit

root@computer:~# ssh name@192.168.1.100

name@desktop:~$ screen -r

[Nano instance]

[CTRL]+[X]

name@desktop:~$ exit

[screen is terminating]

name@desktop:~$ exit

root@computer:~#

Detach a process. If you are running a process via ssh on a remote machine, the process is killed when the connection is interrupted, unless it was detached with screen.