Null/Shell

From XPUB & Lens-Based wiki
Revision as of 17:02, 18 November 2015 by Castrobot (talk | contribs)

Living in a Shell

Unix like OS are multi-user system, allowing a number of users to be logged at the same time and perform different tasks. Each user get's his/ser own home in ~/

shell / terminal

Shell is the program that interprets the commands written by the users (or other programs) and returns their output. There various shells: sh, bash, zsh. You can find out what shell you are running by prompting $SHELL

The Terminal is a wrapper program which runs a shell. It was once a physical device - a dumb terminal - consisting of monitor and keyboard, which would constitute a interface to the computer. Typical a computer would have many dumb connected to it.

ssh: remote access

If you have an account in a remote machine, you can login onto it from your machine by running the ssh - secure shell program.

ssh username@hostname

once inside the machine

You can

  • move around with the cd (change directory)
  • ask where are you pwd (print work directory)
  • list the files in your current directory ls
  • ask who are you whoami
  • ask what other users are logged in users

communicating with other users

wall - let's you send messages to all the logged users

write username - writes private message to user

ASCII Art text

with: figlet "asciiart text"

Challenge: Use figlet fonts. Figlet fonts are stored in

ASCII Art images

with: jp2 img.jpg

Challenge: download an jpg image to the Pi and convert it into ASCII art

pipes, writes, appends

A pipes (" | ") sends the output of one program to the input of another program.

$ echo "my sentence"| wc the echoed sentence "my sentence" is pipped into the program wc which counts the number of lines, words, and characters


> Writes the output of a command to a file, rather than to print on terminal.

man df > df_output.txt redirect the content of man dfM to a file called df_output.txt

If the said file doesn't exit it will create it, if it already exists it will overwrite its contents/


>> appends the output of a command to a file, without overwriting the original file. echo 'also add this' >> df_output.txt will add 'also add this' to the contents of df_output.txt


Raw printing

In raw printing the printer does not interpret a description of the forms to draw. In raw printing plain-text characters are send to the printer and outputted by the printer in the sequence they are received, one line at a time. echo "Hello Printer" > /dev/usb/lp0 will result in "Hello Printer" being printed.

Common Raw Printing Languages

The simplicity of raw printing has its shortcomings. You can easily print ASCII characters, but what if you want print large titles, or words in bold, or other character sets than ASCII, or even images?

One popular raw printing languages is ESC/POS, which stands for Epson Standard Code.

ESC/POS

Hexadecimal

get manual


rawprinting.py

In the Pi you have a program rawprinting.py located in /usr/local/bin that sends text to the printer and changes its modes.

To ask for help about this script run: rawprinting.py -h

Challenge: Print stuff and change the mode of the printer

Raw printing - script

Challenge: Can you get around to change the rawprinting.py script, adding more printing modes?

Printing codes


ensure

Users can write to /dev/usb/lp0

Give users sudo

software

  • figlet
  • jp2a
  • wget
  • curl

subjects

  • shell / terminal
  • multi-user session
  • access another machine: ssh
  • communication among users
  • ASCII art
    • figlet
    • jp2a
  • (text to speech)
  • raw printing




Shell Cheat Sheet

$ cd - change directory

$ pwd - print working directory

$ ls - list directory

$ rm - remove file or directory

$ cp - copy file or directory

$ mv - move or change name of file or directory

$ cat - concatenate print they content

$ echo - repeats what is written after

meta characters

Meta Characters are characters that have special meaning within the terminal

  • ~ the tilde stands for the user's home. cd ~/ change directory to home
  • . dot stands for this directory. ls . list this directory
  • .. dot dot stands for the parent directory to this directory. cp myfile.jpg .. copy myfile.jpg to the parent directory
  • * asterisk is a wildcards which represents zero or more characters ls P*.jpg will list all the files, in the current directory, that begin with P and end with .jpg
  • \ backslash it is a literal character. It escape the meta value of the meta-characters and display them only as literal characters. echo Foo \* will output Foo * If \ wasn't there it would output all the files in that directory.

man pages

man pages are manuals of program. They tells you what the program is, what it can do and how.

$ man df show the manual for the program df that is used to display the free disk space

Can you find out how to display the output from df in a human readable format?


pipe

A pipes (" | ") sends the output of one program to the input of another program.

$ echo "my sentence"| wc the echoed sentence "my sentence" is pipped into the program wc which counts the number of lines, words, and characters

write

> Writes the output of a command to a file, rather than to print on terminal.

man df > df_output.txt redirect the content of man dfM to a file called df_output.txt

If the said file doesn't exit it will create it, if it already exists it will overwrite its contents/

append

>> appends the output of a command to a file, without overwriting the original file. echo 'also add this' >> df_output.txt will add 'also add this' to the contents of df_output.txt

==package managers==
Package managers like apt and aptitude (on Debian/Ubuntu Linux distributions) and Homebrew and MacPorts on Mac, allow more (command-line, but not only) programs, than the ones that come with the operating, to be installed on our system.

Will need to install 2:
* figlet - A program to make large titles our of ASCII characters
* imagemagick - The swiss army knife for image manipulation on the command line


== Challenges ==
* You need to use '''figlet''' to print words and sentence on the terminal as large titles

* Using Wget and imagemagick you need to 
** Download an image from the web without using the browser to download the image, but the '''wget''' program
using imagemagick's '''display''' program display the image
** using imagemagick's '''display''' program display the image, but flipped and monochrome
** using imagemagick's '''convert''' program the image to monochrome and to a different file extension.

references