steps to download and run programs in Linux = Using Package Manager:

Once linux is installed and running, you will want to download and install new software on your system. One way to install any new software on your computer is to download source code from internet, then compile it on local machine using Makefile provided, then install it in appropriate dir. This process is very cumbersome, and is suitable for advanced users only. However for layman users, this process can all be automated by using package managers that come preinstalled on linux OS. However for these pacakage managers to be able to download, compile and install the software, the software needs to be in a package in a special format. The creator of the software should provide their software in specific format for that package manager, or else the package manager can't be used. Fortunately, most of the software that you will ever encounter is available in multiple package formats, so they can be used with almost all package managers.

Package Manager:

A package manager or package management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for an OS on a system. A package manager deals with packages which are distributions of software and data in archive files. Packages contain metadata, such as the software's name, description of its purpose, version number, vendor, checksum, and a list of computer_programming dependencies necessary for the software to run properly. Package managers are designed to eliminate the need for manual installs and updates. Running any package manager requires admin rights, so you have to precede cmds with "sudo" (see in "linux cmds" section for more details on sudo).

Following are the package managers and package formats supported by various linux distro. As you can see there are only 2 formats that will suffice all linux users - .deb and .rpm formats.

1. Deb (Debian) format: All Debian and its derivatives such as Ubuntu, Linux Mint, support .deb package format. The package manager is apt (advanced package tool). APT resolves dependency problems and retrieves requested packages from designated package repositories. APT delegates the actual installation and removal of packages to dpkg. So, instead of directly typing dpkg cmds, APT handles it for us. We type apt commands on command line to interact with APT. NOTE: APT is different than apt cmd line tools. There are various apt cmd line tools to interact with APT pkg mgr. Some examples of cmd line tools are apt-get, apt-cache, aptitude, apt, etc. Until 2016, we had apt-get and apt-cache as 2 most popular cmd line tools for APT. However, cmd line tool "apt" was introduced in Ubuntu 16.04, to combine cmds from both of these into one, and make cmds simpler. apt is now recommended cmd to use with APT. You will still see lots of documentation for apt-get and apt-cache online, and you can continue to use them (as they will still work), but apt is the path forward. So, I'll just show "apt" cmds from now on.

Few of the important apt cmds here: https://itsfoss.com/apt-command-guide/

  • sudo apt update => updates the pkg database repository, so that APT knows if there are any newer packages available, for software already installed on your system. You will see pkg info being retrieved from various servers. You need to run this cmd, before installing any new pkg, so that you get the latest in the repo.
  • sudo apt upgrade => once the pkg database is updated using above cmd, we can upgrade all installed pkg with this cmd. If update cmd not run before upgrade, then APT repository may not that there are latest updates available, and will say, it's up to date.
  • apt search php => this is neat way to find out or search for any packages which contains the specified term, here it searches for any pkg containing term "php" in it. It will generally show hundreds of pkg with that name, so it's usually hard to determine which pkg to choose. i.e if we see 100's of pkg with name "php", we don't know which one to download. Some of these are basic pkg, while some are additional modules for that pkg, etc. Usually search on internet will show what all to download. NOTE: no sudo reqd, as it doesn't make any changes to your system.
  • apt show php => will show additional info about pkg "php". Useful to know, before you install that pkg.
  • sudo apt install <pkg_name> => most used cmd. installs required pkg, like apache2, tcl, etc
  • sudo apt remove <pkg_name> =>removes the pkg.

These are some important directories associated with APT:

/etc/apt has the apt configuration folders and files. All files/folders in /etc/apt are:

  • sources.list: Locations to fetch packages from. This has path to locate the desired packages, which might be available on the network or a removable storage medium, for example, and retrieve them, and also obtain information about available (but not installed) packages.  On my system, this file has 1 line as follows:
    • #deb cdrom:[Linux Mint 19 _Tara_ - Release amd64 20180717]/ bionic contrib main non-free => This line is commented out. So, apt automatically searches other files in sources.list.d for location of packages:
  • sources.list.d/official-package-repositories.list => This has the location of where to fetch packages from:
    • deb http://packages.linuxmint.com tara main upstream import backport => We see many more links from ubuntu, canonical, etc.
  • To use official debian repository which contains about 50K packages, we could insert a line like this in any of above file, and then run "apt update"
    • deb http://ftp.debian.org/debian stable main contrib non-free => Now, APT will use this link to download stable pkg
  •  apt.conf.d => APT configuration file fragments.
  • preferences: version preferences file. This is where you would specify a preference to get certain packages from a separate source or from a different version of a distribution

/var/cache/apt/archives/: storage area for retrieved package files. It has all .deb files that are downloaded. deb files are readable text files. This is a good place to see what was downloaded for let's say "sql" package.

/var/lib/apt/lists/: storage area for state information for each package resource specified in sources.list

Ex: to install a new scripting language called tcl (which is not installed by default on Linux Mint), we run these cmds from any dir in a terminal:

sudo apt install tcl => sudo needed to run as super user since root permissions are needed to run apt. It asks for user's password, and then shows what's going to get installed, and asks if you want to continue. On pressing Y, it downloads pkg. shows lines like this ...

Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 libtcl8.6 amd64 8.6.8+dfsg-3 [881 kB] => download link for tcl pkg

Preparing to unpack .../libtcl8.6_8.6.8+dfsg-3_amd64.deb ... => .deb file for tcl downloaded from link above,
Unpacking libtcl8.6:amd64 (8.6.8+dfsg-3) ... =>

Setting up tcl8.6 (8.6.8+dfsg-3) ... => This finally installs tcl on your m/c.

NOTE: Instead of using command line above, Linux Mint provides graphical software called "synaptics package manager" and "software manager". We can use this too to install new software just by searching for that and clicking "install". However if any of these is open, and you try to run apt cmd on cmd line in terminal, you may get a lock error. So, close these before using cmd line i/f on terminal.

 

2. RPM (Red Hat package manager) format: All Fedora and it's derivatives as CentOS, RHEL, and SUSE, OpenSUSE support .rpm package format. RPM is delivered as a single file either as .src.rpm (for source pkg) or as .<arch>.rpm(for binary pkg). Package manager is yum.

Yum (yellowdog Updater Modified). Few of the imp cmds:

  • sudo yum update => updates the pkg database repository, so that yum knows if there are any newer packages available, for software already installed on your system. -y option updates w/o asking for your confirmation. Similar to apt, you need to run this cmd, before installing any new pkg, so that you get the latest in the repo.
  • sudo yum upgrade => once the pkg database is updated, we can upgrade all installed pkg with this cmd. 
  • sudo yum upgrade httpd -y =>this upgrades specific pkg named "httpd". -y upgrades w/o asking for confirmation
  • yum search httpd => searches for any pkg containing term "httpd" in it
  • yum info httpd => shows additional info about pkg "httpd".
  • sudo yum install <pkg_name> => most used cmd. installs required pkg, like apache2, tcl, etc
  • sudo yum remove <pkg_name> =>removes the pkg. This will figure out all files associated with that pkg, and cleanly remove them. Linux "rm" cmd should NOT be used to remove packages.

 ex: upgrading firefox:

  1. sudo yum update => updates local repo.
  2. sudo yum info firefox => This gives info on installed firefox pkg, as well as the latest one available. For me, it was showing intalled firefox version as 80.xx, while available was showing as 93.0.0. That meant my pkg was out of date. However, for latest avilable pkg to show, we should have run above "update" cmd, else yum will show latest based on when the repo was last updated.
  3. sudo yum upgrade firefox => This finally upgrades firefox to latest 93.0.0

On top of individual pkg, there are also groups, of which there are 2 kinds:

1. environment groups: consists of a group of packages define how the server needs to be built, it can be Web Server, Minimal installation or server with the graphical interface.

2. groups: consists of all other groups of software like “Development Tools” or “System Administration Tools”. We can apply yum to these groups too

  • yum groups => prints summary of groups. On my centOS laptop, it shows 12 Environment groups and 25 groups.
  • yum grouplist => prints all avilable groups with group names. Env groups are Cinnamon Desktop, Mate Desktop, Basic Web Server, etc. Groups are Cinnamon, Mate, Sound and Video, etc.
  • yum groupinfo "Cinnamon Desktop" => provides more info about this DE (Desktop Env) pkg. 
  • sudo yum groupinstall "GNOME desktop" => installs GNOME3 DE
  • sudo yum groupremove "Security Tools" => removes this group named "security tools"

FIXME: yum details FIXME

Raspberry Pi:

Very useful tool to learning microcontrollers and building projects. This is preferred over all other microcontrollers, as it can support Linux OS, so you can write programs in any programming language, and be able to control pins. It works just like working on a desktop or laptop with Linux installed. The advantage that Raspberry Board provides is that it behaves as a microcontroller, so it has pins available that you can use to drive any output or to receive any input which allows you to connect various devices to it's pins and make cool projects. A regular microprocessor doesn't provide you these general purpose pins (known as GPIO), so we can't use processors from Intel/AMD to make these projects.

official website: https://www.raspberrypi.org

Generations of Raspberry pi:

A summary of Raspberry pi journey is on wiki here: https://en.wikipedia.org/wiki/Raspberry_Pi

More and more powerful raspberry pi are coming on market. What started as 1st generation Raspberry pi 1 in 2012, soon expanded to Raspberry Pi 2, Pi Zero, Pi 3 and now Pi 4 as of 2020. All Raspberry Pi have Broadcom SOC which have ARM microprocessor. Here are the details:

Pi 1:  It came out with Model B in 2012, then a cheaper Model A. Then improved pi Models B+ and A+ were released. Broadcom BCM2835 SOC was used in these 1st gen Pi, which had 700MHz ARM 11 Processor. Models A/B had 256MB memory, while A+/B+ had 512MB memory. The ARM11 is the same CPU used in the original iPhone although at a higher clock rate and mated with a much faster GPU. So, you can imagine the power of Pi 1, and this is the lowest performing 1st gen Pi !!

Pi 2: These were released in 2015. They initially had BCM2836 SOC which featured a 900 MHz 32-bit quad-core ARM Cortex-A7 processor with 1 GB RAM. Later versions had BCM2837 SOC which featured a 1.2 GHz 64 bit quad-core ARM Cortex-A53 processor.

Pi Zero and Pi Zero W: These were cheapest pi and smaller in size. Pi Zero was released in Nov, 2015 for $5. In 2017, Pi Zero W was released, which was similar as Pi Zero but with wifi and bluetooth capabilities (the term W implied Wireless). This sold for $10. Both used the same Broadcom BCM2835 SoC as the first generation Raspberry Pi, although now running at 1 GHz CPU clock speed. They both had 512MB memory. With wireless capabilities added, Pi Zero W was the first real all purpose microcontroller. With a rock bottom price of $10, and working as a desktop, there was nothing on market that could compete with it. I myself bought couple of Pi Zero W for $3.14. They work extremely well, although slow for any browser viewing.

Pi 3: There were 3 versions: B, A+ and B+. All used BCM2837 SOC. Model B was released in 2016 with BCM2837 SOC which featured a 1.2 GHz 64-bit quad-core ARM Cortex-A53 processor (same as Pi 2). B/B+ had 1GB memory, while A+ had 512MB memory. Model B+ and A+ had higher CPU speed of 1.4GHz. However, now all Pi had wireless capabilities, That was the only meaningful addition over Pi 2.

Pi 4: The only model released here as of 2020 is Model B which was released in 2019. It used BCM2711 with a 1.5 GHz 64-bit quad-core  ARM Cortex-A72 processor. More details in next section.

For the 1st time, Pi supported ARM virtualisation capabilities. It came in 4 flavors, with RAM of 1GB, 2GB, 4GB and 8GB.  It added USB type C port for powering the board. It had 2 micro HDMI ports for connecting 2 4K monitors. It also has 2 USB 2.0 ports and 2 USB 3.0 ports.  There was significant upgrade in RAM, comparable to low end PC, for the first time. Pi 4 with 8GB RAM became a full desktop capable processor, which can perform on same level as Intel or AMD machines.

Where to Buy:

Raspberry Pi is sold at bunch of places (the website lists such places), but the prices vary a lot. You can usually buy raspberry pi at discount during sale. In USA, Microcenter is the best place to buy, as they frequently have lowest performing Raspberry Pi Zero W for $3.14 (i.e value of pi). BestBuy also has sales some of the times. Best website to look for such deals is slickdeals.net, and search for raspberry.

Which Raspberry Pi to Buy:

I would suggest sticking to Raspberry Pi Zero W, or Raspberry Pi 4. All other Raspberry Pi are basically just variations of these 2. My next sections will only deal with these 2 flavors of Pi. Pi Zero W is the cheapest Raspberry Pi, and could be had for $3. Raspberry Pi 4 with 4GB memory could be had for $40. It is so powerful that it can serve as a full desktop replacement. I've a 2GB version, and though it works well, it lags in browsing. 8GB is where all programs can fit in memory well, even with multiple tabs open on browser. So, 8GB would be ideal, though 4GB will serve well for 99% of tasks.

You don't need to buy any accessories bundled with Pi, since they are a lot cheaper to buy separately. You will need to buy below accesories, before you can use Pi. However, I don't suggest buying Keyboard/Mouse and Monitor, as we'll use vnc to connect to raspberry pi. That way we can use our laptop's keyboard and monitor to work on raspberry pi.

- memory SD card => Get a SD card of 64GB or 128GB as these are very cheap and go for $10 or less. Large size is good, incase you need to install a lot of stuff on raspberry pi later.

- USB cable => You may order some cheap USB cables from ebay

- Keyboard/Mouse => You don't need to buy this.

- Monitor => You don't need to buy this.

Raspebrry Pi: Getting started

Steps to get a raspberry project going. I'm listing general steps irrespective of which raspberry Pi you use. My preferred raspberry Pi are Pi Zero W (since it's the cheapest one), and Pi 4 (since it's the most powerful one).

A. Burn Raspberry Pi OS on SD card: I'm doing this on a Laptop which has Linux Mint installed on it. The below steps are all done on the laptop, and NOT on raspberry board.

1. download he raspberry pi OS:

https://www.raspberrypi.org/downloads/raspbian/

Download the "Raspbian Buster with desktop and recommended software". Download the zip file, and extract it. It gets extracted as "2020-02-13-raspbian-buster-full.img".

2. get a SD memory card with atleast 8GB space as raspbian OS after extraction needs atleast 8GB. On a terminal, type the lsblk cmd to check what what devices it shows connected (After inserting the SD card)

$ lsblk -p
NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
/dev/sda           8:0    0 931.5G  0 disk
├─/dev/sda1        8:1    0   260M  0 part /boot/efi
├─/dev/sda2        8:2    0    16M  0 part
├─/dev/sda3        8:3    0 246.7G  0 part
├─/dev/sda4        8:4    0   980M  0 part
├─/dev/sda5        8:5    0   7.5G  0 part [SWAP]
└─/dev/sda6        8:6    0 676.1G  0 part /
/dev/sr0          11:0    1  1024M  0 rom  
/dev/mmcblk0     179:0    0  29.7G  0 disk
└─/dev/mmcblk0p1 179:1    0  29.7G  0 part /media/ash/6636-6330

Above o/p of lsblk cmd shows our SD memory card as mmcblk0. It has 1 partition mmcblk0p1 which is mounted on /media/ash/6636-6330. We need to unmount the device by typing below cmd.

$ umount /dev/mmcblk0p1 => NOTE: we unmount the partition in SD card, and not the SD card device itself (i.e unmount mmcblk0p1 and NOT mmcblk0)

3. Now copy the raspbian image on to SD memory card using Linux dd cmd below:

$ sudo dd bs=4M if=2020-02-13-raspbian-buster-full.img of=/dev/mmcblk0 status=progress conv=fsync => block size is set to 4MB to speed up copy time, while status is set so that copy progress can be seen
[sudo] password for anvay:
7319060480 bytes (7.3 GB, 6.8 GiB) copied, 222 s, 33.0 MB/s
1746+0 records in
1746+0 records out
7323254784 bytes (7.3 GB, 6.8 GiB) copied, 249.04 s, 29.4 MB/s

4. Now once the image is copied on SD card, eject the SD card, and it's time to insert that card in Raspberry pi motherboard.

B. Power up Raspberry Pi: Now, get your Raspeberry Pi Board out, and perform all below steps on it:

1. Insert The memory card into the card slot on Raspberry pi board.

2. Get the accessroies needed for raspberry Pi. At the least you need a usb Type A or Type C cable for powering the raspberry pi. For initial setup, you will also need a monitor and a keyboard and mouse.

3. There is no turn on/off button on any raspberry pi. They start powering up, as soon as the usb cable is connected to a power supply. You will see a raspberry logo, and then the screen should appear. You should connect it to your wireless network, by clicking on wireless icon on top right. That wireless network name and password will be stored, so you don't need to enter it ever again.

At this stage you are done with basic setup, and can start using your Pi to browse, write scripts, play games or do nothing.

Other Utilities:

There are few other things that we can install on raspberry pi, that will be useful.

C. ssh: To connect to raspberry pi terminal, you could use ssh. Look into "openssh" section of "software tools". First we need to install ssh on raspberry pi. NOTE: client ssh is always installed by default. It's the sshd daemon or server ssh that is not installed (or not activated) by default, as it's not needed that often.  ssh is already installed on raspberry pi, it just needs to be enabled. This link shows how: https://www.raspberrypi.org/documentation/remote-access/ssh/

Our laptop from which we are trying to connect to raspberry pi only needs the client ssh, and NOT the server ssh. Since client ssh is slways installed by default, we don't need to install server ssh (i.e sshd), but it's always good to install it on the laptop too, as that way we can connect to our laptop from raspberry pi too (the use case is rare though).

Once all that is done, we need to find the ip address of raspberry pi. You can also use the name of "raspberry pi". By default, name of your raspberry pi is "raspberrypi" and it's private ip address is of form 192.168.1.xx. You could run cmd ifconfig and get the ip address of your ethernet i/f or wireless i/f. But in our case, it's not needed. We know the name of our device, and that will suffice. Next, we also need to know he username on raspberry pi. By default, username is "pi". So, to connect to raspberry pi, we need to go to our laptop from where we want to connect to raspberry pi. Assuming it's a Linux laptop, open a terminal and type ssh cmd as below:

ssh pi@raspberrypi => This will ask for your password on raspberry pi for user "pi". On successful connection, it will show raspberry pi terminal. The terminal prompt will change to that of raspberry pi prompt. By default, the new prompt will show as "pi@raspberrypi".

Now you can access any file, open apps , etc. NOTE: not all application will open from this terminal due to Xdisplay not setup. Look in Xwindows section on how to do it.

D. scp: If you want to copu any file from raspberry pi to your laptop or from your laptop to raspberry pi, you use this cmd. Usage is provided on "openssh" section. From your local computer, type this cmd to transfer files back and forth. NOTE: in both examples below, we ssh into raspberry pi, that's why we need server ssh (sshd) running on raspberry pi.

ex: scp test1.py pi@raspberrypi:/home/ramesh/. => copies test1.py from local computer to raspberry pi

ex: scp pi@raspberrypi:test3.txt project/tmp/tmp.txt => copies test3.txt from raspberry pi to our local computer in current dir subdir as tmp.txt

 E. Setup VNC:   Now we'll connect VNC. This is the last thing that will allow us to use our raspberry pi as a local desktop without requiring monitors, mouse, etc. VNC steps are detailed in vnc section. Once you have vnc installed on raspberry pi, connect to it using any laptop running windows or Linux. There in nothing special or different for raspberry pi. You need to have vnc server installed on raspberry pi, and vnc client installed on your laptop from which you are going to connect to raspberry pi.

 

 

 

 

Linux Mint:

Best one is Linux Mint with least issues during installation. I would recommend this over any other OS (except CentOS which is my favorite), as everything works flawlessly. For beginners who are used to Windows, Linux Mint might be more comfortable to use than CentOS, as the user interface is very similar to Windows.

Installating OS:

Download, install using these links:

https://itsfoss.com/guide-install-linux-mint-16-dual-boot-windows/

The only deviation from steps in link above, is that you will be asked if you want internet connection enabled to allow 3rd party software to be installed. You can choose "no" and not install any 3rd party software, as you can always install it later when needed. I have chosen "yes" too, and that has worked fine. Also, choose just 2 partitions, a "/" partition, and a small "swap" partition (no need for a separate "home" partition). Once you click on "Install now", you will see 2 partitions (ext4 and swap). Click "OK", and it will start installing Linux. It will take a while (shown by progress bar, usually 30 minutes). Click "restart now" at end, and it will now start showing GRUB menu. It shows "Linux mint", "windows" as well as few other options. You can choose "Linux Mint" or "windows" depending on which OS you want to start.

steps:

1. Download LinuxMint ISO:

 I downloaded "Linux Mint 19, Cinnamon 64-bit" edition. Follow link below to get Linux mint.

 https://linuxmint.com/download.php

As of Sept 2021, Latest one is Linux Mint 20. It has 3 variations - Cinnamon, Mate and Xfce. The only difference is in the GUI, otherwise they are all similar. If you don't know which one to install, choose "Cinnamon" as it's most popular.

2. Copy ISO files to USB drive:

You need linux already installed in order to copy the iso to usb from within linux. We don't use UUI to burn the iso as explained previously. Use cp cmd as explained under installation.

1A. Use cp cmd to copy to usb drive

sudo cp /home/aarav/Downloads/LinuxMint-20.2-cinnamon-64bit.iso /dev/sdb => Do not use /dev/sdb1. See in "Linux Installation section for details"

3. Reboot Windows:

Once copied, plug it in windows laptop and reboot it. Do not use UUI, as it gives error. You will be greeted with "Install LinuxMint" screen. Follow prompts as explained in link above from itsfoss

 

Useful Customization:

Adding application pgm icons to panel:

To add to panel (bottom most bar on your laptop is called panel), goto Menu-> serach for that application, and then right click when it shows up. Now you will options for "add to panel" and "add to desktop". click on "add to panel", and you will see the icon added to bottom panel.

 

Installing other software:

Here are links for few more pgms to install. Firefox and Skype are the important ones. Luckily they install flawlessly.

Install Firefox browser:

Most websites, with exception of very few, open fine in firefox in Linux Mint. Video/audio on internet sites work as well. Firefox is installed by default. If not install, type these cmds on Terminal.

firefox -version => shows version of firefox installed. If it's not installed or not latest one, run below cmds

sudo yum install firefox => installs firefox

sudo yum update firefox => This updates firefox to latest version

firefox -version => shows version of firefox installed

Install Skype:

Installing skype is very easy on LinuxMint. Skype binaries for linux are now available from skype website. Follow these steps to install skype:

  1. sudo apt update
  2. Download skype binary. 2 ways:
    1. download skype binary (.deb pkg) directly from skype site: https://www.skype.com/en/get-skype/. Choose "Get Skype for Linux deb"
    2. wget cmd: install wget (if not installed and then use it to download skype)
      1. sudo apt install -y wget
      2. type this cmd on linux terminal to download it: wget https://repo.skype.com/latest/skypeforlinux-64.deb
  3. sudo apt install ./skypeforlinux-64.deb => NOTE: ./ is needed else you get an error that deb file not found. provide appr path for downloaded .deb pkg, if you are not running this cmd in the dir where you downloaded this deb pkg. Linux will ask for root passwd, and then install skype.

 

CSS - cascading style sheet

CSS  are very important to know. They provide a central location to control the physical appearance of your website. You create a single css file and put definitions to define how elements are to be displayed on all of the pages of your website. Other big advantage is that you can control the layout of your whole website through this css file. Before CSS, the only way I knew to control the layout of a page, was by coding the whole page as a big table, with several nested tables inside it, to make complicated layouts. However, CSS greatly eases this by having a div command, that does this work for you. Below, I'll start with various CSS commands.

comments indicated as /* comments */

CSS syntax:

CSS is made up of rules. A CSS rule is made up of a selector and a semicolon-separated list of declarations inside brackets. Each declaration has a property and a value separated by a colon. If an element in an associated HTML document matches a selector in the style sheet, then the declarations will be applied to that element.

selector {property1: value1; property2:value2; } /* This whole thing is called a CSS rule.  Everything inside { ... } is called a declaration, which can have multiple property:value pairs
selector is html element/tag or something else defined.
property is the attribute
value is the value assigned to the attribute.

ex: center aligned paragraph with black text color and arial font. Now, any p element in html doc, will use these values to display the text. Everything can be on a single line, since CSS, like HTML ignores spaces.
p{
text-align: center;
color:black;
font-family: arial;
}

ex: assign green color to h1, h2 and h3 tags. Now, any ph1,h2,h3 element in html doc, will display the text in green color.
h1,h2,h3 {color: green}

Inheritance:  One of the key properties of CSS is that styles are inherited down the document tree, i.e if a property is specified for body, all elements in document tree of body (i.e p tags, etc) will inherit this property.

CSS implementation:

CSS can be implemented in 4 ways:
1. inside html elements: you can apply it directly to individual elements with the style attribute. This is called inline style. This is doCSS implementation:ne inside html document, and is not really in the CSS format that we talked above. This is because style is an html attribute.

style attribute: specify the styling of an element, like color, font, size etc. Can be used with almost any tag. Format is style="property:value;"  where property/value are CSS property/value. However, since using style attribute in html document has a very localized effect, it's least efficient way of applying CSS, and used rarely, only to fix bugs or cross-browser issues.

<p style="color:red">This is a red paragraph.</p> => specifies text color as red
<body style="background-color:powderblue;"> ..... </body> => everthing within body tag has background clor as blue
<h1 style="border:2px solid Tomato;">Hello World</h1> => This puts a border of 2pixels around the text and assigns red color to it
<h1 style="font-size:60px;">Heading 1</h1> => specifies font-size as 60 pixels. font-size can also be in %, i.e 150%
<h1 style="font-family:verdana;">This is a heading</h1> => specifies font as verdana
<p style="text-align:center;">Centered paragraph.</p> => defines horizontal text alignment in that para


2. inside html page: In this implementation, style element is used. <style> is a tag (or element) just like other elements in html. This is also called internal style sheet. Rules in a <style> element apply to everything in that page, so it is more efficient. <style> elemnt is usally put in the head section of html, but browsers will obey the style even if put in the body section.

ex: <style> p { color: red; } </style>

3. in external css file: This should be the preferred way, especially if the style is applied to many pages. Above style element applies rules only to the page they are in, but this applies to all the pages, where this file in referenced.The <link> element references an external style sheet. It's put in the head section.
<link href="/styles.css" rel="style sheet">

The file styles.css would then contain the CSS code only. The same CSS file can be used for multiple pages, so browser download styles.css file only once saving on bandwidth and page rendering speed.
p { color: red; }

4. link from within existing file: The final way to include CSS is to link from within existing CSS . This requires that some CSS has already been included, via a link or a <style> element:
<style> @import url('styles.css'); </style> /* here style sheet is imported from the head section of the document.

Other CSS selectors:

So far, we saw type selectors, where the selector consists of the element name and selects a type of element for the property to be applied to. But there are many other CSS selectors as well, which are more helpful. We'll see 3 css selectors => Element name selector(we saw this above), Id selector (chooses element based on id attribute) and class selector (chooses element based on class attribute)

id selector: An ID selector chooses an element based on the id attribute. An ID selector consists of a hash character (#) followed by the id. Property is applied to element with matching id. Only one element can have a given id. So, ID selector by itself is not very helpful, as it requires adding an unique id attribute to every element that we want the property to be applied to. In case we want to use the same property to be applied to more than one element, we use class selector explained later.

ex: here we define myelement id, and apply it to p element in html file.

#myelement { => this CSS section may be in head of html file or separate css file.
color: white;
background-color: black;
}

<p id="myelement">A paragraph</p> => This is in body of html file. Here p element has an id=myelement. No other element can have this id. Above selector applies color and background-color to this p element as it has matching id. All ohter p elements are left intact, if they don't have this id.

ex:  #green {color: green} => matches any element that has an id attr with a value of green. It sets attribute "color" to green for all those matching elements.

ex:  p#para1{text-align:center} => matches any "p" element that has id with value of para1. Thus we are more specific here that the attribute can only be applied to "p" element and not to any other element.

class selector: The class selector chooses elements based on the class attribute. Unlike the id attribute, values in the class attribute don’t have to be unique throughout the document, so rules based on class selectors usually apply to a selection of elements in a document. A class selector consists of a period ( . ) followed by the class name. class selectors can be applied to multiple elements of same or different types.

ex: here we define myclass class, and apply it to multiple elements as p and b.

.myclass {
color: white;
background-color: black;
}

<p class="myclass">A paragraph</p>
<b class="myclass">Another text</b>

ex: any p tag with "right" and "center" class gets its text aligned to right and center respectively. This style is same as hwat we used in id selector above.
p.right {text-align: right}
p.center {text-align: center}

ex:  .center {text-align: center} => any tag with center class gets its text aligned to center.

Grouping CSS selectors:

Just as we saw in the example for element selector where we applied the same attribute to multiple selectors, we can do the same for id and class selectors. The main motivation behind grouping multiple selectors is to save on size of CSS file, so that it's faster to load. These are 4 ways of grouping selectors:

1. grouping with comma as a separator: Here comma is used to separate the selectors. The attribute in parenthesis is applied to all the selectors. Selectors can be mixed, i.e id and class can be mixed.

ex: In this we group multiple selectors in same, i.e attribute color is applied to all class="red"(only for p elements since it's p.red), id="sub" and element="h1"

p.red, #sub, h1 { color: #f00; }

2. grouping using space as a separator (no comma): Here we don't use comma as CSS separators. This changes the meaning to imply a parent child relationship. The first selector is considerd as parent of subsequent selector (in nested form) and attribute is applied is applied to only those elements which is a descendant of all those selectors.

ex: .red #sub {color: #f00; } => Here the attribute color is applied to elements with id="sub" whose parent class="red" (i.e "sub" is a descendant of "red"). All the selectors here have to be single word selectors, as the parser will have no way of distinguishing selector with multiple words (they will be interpreted as different selectors by the parser)

<h1 class="red"> .... <h3 id="sub"> <p> my name </p> </h3> </h1> => Here color attribute will be applied to "p" element "my name" since the elemenet's parent has both class and id matching.

3. grouping by mixing separators with and without comma: This is mixing separators with and without comma (which is a combination of two styles #1 and #2 above and perfectly valid). 

ex: p.red, #sub, div a:link {color:red;} => Here p.red, #sub and "div a:link" are 3 diff selectors to which attribute "color" is applied. Note there is no comma b/w 3rd and 4th selector. The last selector is a compound selector and the rule is applied to The link pseudo class of the anchor elements that are descendants of a division (div tag).

4. grouping by having no space or comma separator: One other variation is to have no space between multiple class or id. In that case, it means apply the css property to element which matches all the given class and id. All examples above had attributes applied to any matching selector, but here, attribute is applied only if all the selectors match.

ex: #header.callout { } => here it says select all elements which have id=header and class=callout. NOTE: there's no space

ex: #header .callout { } => here there is  a space b/w #header and .callout. This becomes same as #2 above. So, this becomes css selector grouping with parent child relationship, i.e it selects all elements whose class="callout" and which are descendants of elements with id=header.

ex: .snip#header.code.red { } => We can mix as many id/header selectors that we like. Here there are 4 selectors, and we select all elements whose class=snip and id=header and class=code and class=red. This certainly is complex way of writing, but is valid anyway. We use these complex selectors to override individual selectors as .snip, .code, etc. We might want to apply certain property on class=snip or some other proerty on class=code, but we may want to apply some other property for elements which have both classes = snip and code. In such cases, we can use this style.

 

Properties and values:

As can be imagined, there are tons of properties and values, and it's not possible to remember or even know all of them. You should look online if you want to learn more about valid properties for various elements. This is a good ref for CSS properties:

https://www.w3schools.com/cssref/

We will just look at few common ones:

1. Colors and lengths:  The most common values in CSS are colors and lengths.

ex: color: #0ff; => This specs text color of an element to blue. We can specify value of color in 4 ways: name (eg red, blue), #rrggbb, #rgb and rgb(r,g,b), where r=0, g=0, b=0 gives coclor black, while r=255, g=255, b=255 gives color white.

ex: border-width: 5px => This specs width to be 5 pixels.  Length can be specified in many diff units as px (pixels), pt (pints where 1 point=1/72 of inch), cm (centimeter), in (inches), % (as percentage of size of element's parent)

2. display: The display property specifies the display behavior (the type of rendering box) of an element. By default, display mode is "block" which causes line breaks before and after each element. If we set display mode to "inline", that causes elements to be displayed side by side w/o line break. There are many more display modes.

https://www.w3schools.com/CSSref/pr_class_display.asp

ex: display: none; //this display mode doesn't display the element. This is used for drop down menu that you see in websites, where it shows other elements only on hovering mouse over it.

3. float and clear:

A floated element is one that’s outside of the normal flow of text, like a cutout. The text flows around these floated elements as long as there’s room. Floats could be used to lay out entire pages. Floats rely on two CSS properties: float and clear . The float property determines which side the element floats to, whereas the clear property determines how the element behaves with respect to other floated elements. Values for float are left , right , and none ; values for clear are left , right , both , and none .

ex: float: left;

Borders and backgrounds: Two of the most common things to style are borders and backgrounds.

1. Borders: A border has a width, a type, and a color. All three can be set separately using the properties border-width (a length, a width of 0px implies no visible border), border-style (a special property for borders), and border-color (a color):

ex:

.one { => this is the long notation
border-width: 5px;
border-style: solid;
border-color: #07f;
}

.one { => We can also use the short hand notation for the above border, with the triplet: width, style and color specified on a single line with spaces in between. We can also specify these triplet separately for each side of border, i.e border-top, border-bottom, etc.

border: 5px solid #07f;

}

<div class="one"></div>

There are many other properties of border that you can specify to custmize borders further. Look in ref link above.

2. Background: This allows images as background, which can be positioned, repeated, etc. Background is used very widely in joomla and other cms.

ex: this is the long notation where we specify all background properties with values. Here background property is applied on element "one"

one {
background-color: red;

background-image: url(background.png);

background-repeat: repeat;

background-position: top;

background-attachment: scroll;
}

ex: We can also set all background properties in one declaration. More details here: https://www.w3schools.com/cssref/css3_pr_background.asp

body {background: url(background.png) 50% 50% no-repeat fixed #f00; // this is the short notation, instead of using the long one above.

<body> <h1> Hello </h1> </body> //the above declared background property is applied to <body>  element

Page layout: CSS allows us to specify where to place elements on a given page, and how to display them. We'll learn about various CSS concepts below.

CSS Box model:

The CSS box model defines the dimensions of elements as they’re laid out on the page. Each html element can be considered a box. It consists of actual content of element, then a padding, border and margin around it. The size of each element is determined by it's width and height, then a padding around it, then a border and then a margin (margin is the space b/w this element and the next one).  The padding clears the area around the element before the border is drawn. padding is transparent. Then a border is drawn. Margin clears an area around the border. The element’s width and height is either defined explicitly or determined automatically by the browser based on the content and display mode. The total size of the element is the size after adding all of padding, border and margin to the element's size, as any other element has to be outside this box. Each of padding, border and margin have a width specified for top/bottom, left/right. Either all 4 values for the 4 sides can be given or just 1, 2 or 3 values can be given, and the browser will decode it based on some rules.

This is how it looks  with margin on the outermost and element in the innermost  => [ [ [ [  margin  [ [ [  border  [ [  padding  [   element   ]  padding   ] ]    border   ] ] ]   margin   ] ] ] ]

ex:  specifying all 3 for any content inside <p> element (in short notation). Since border=0px, border is not visible at all

.p {

border: 0px solid green; //border-top etc can also be specified in this format for each side
padding: 5px;
margin: 10px;

}

ex: border-width: 5px 10px 20px 30px; => This specs top=5px, right=10px, bot=20px and left=30px. Or if all 4 sides has same size, we could write "border-width: 5px;" We could also write this as 4 separate lines as below. Similarly for margin and padding, which we can specify as margin-top, padding-bottom, etc.
border-top-width: 5px;
border-right-width: 10px;
border-bottom-width: 20px;
border-left-width: 30px;

div tag:

The <div> tag defines a division or a section in an HTML document. The div element is a generic container with no particular semantic meaning. It's commonly used in web authoring for styling purposes, in conjunction with the style and class attributes. div is the one of the most commonly used elements you will find on website's CSS file. It's used in dividing a webpage in sections as header, main content, navigation, footer, etc, each with it's own distinct style. In HTML5 there is a <header> element, as well as a <nav>, <main>, <article>, <footer> and a couple other new elements that replace these div tags.

We can apply above properties to div element as:

div {
width: 2.5em;
height: 2.5em;
margin: 0.5em;
padding: 0.5em;
border: 5px dashed black;

display: block; //However, width and height properties don’t apply to inline elements, default width and height are chosen for that element
}

Now in html body, we can have something like:

<div> text 123 </div> <div> text 756 </div>

Now, we can make a whole page layout with header, article, navigation, footer, etc. Here's a link on creating layout using 5 different methods. Look at "CSS floats" for an example:

https://www.w3schools.com/html/html_layout.asp

 

-------

 

to define different styles for same type of html element.

 

this ..

this ..

styles can also be applied to html elements with particular attributes.
ex:
input[type=text] {background-color: blue}

 

Shell: shell are cmd language interpreter for GNU OS (or Linux OS). Shell is simply a program that executes cmds. It is used both as cmd interpreter and a pgm language (by allowing multiple cmds to be combined and having them execute based on conditions). Shells can be used interactively (by accepting i/p typed from keyboard), or non-interactively (via script)

2 most common shells are csh and bash. Bash is the default shell in most Linux distros, and is the preferred one. Most of the scripts written for GNU are written in bash, and not csh.

shell basics:

If you power on a computer with no monitor, keyboard and mouse attached, the processor will run the pgm stored in memory, and keep on doing whatever the pgm is asking it to do. This pgm stored in memroy is called the kernel, and is loaded in memeory from bootup time to shut down. If we connect a monitor, the computer will display what the pgm is sending to monitor interface. If we want to make computer useful, we need a hook, so that we can use it to run our pgm. That is possible if we provide a user interface as "mouse/keyboard", but there has to be a program on computer that takes these inputs, and pass it on to some other program that can decide what to do with these user inputs. Shell is one such program, that takes user inputs which is usually a cmd followed by args. Shell passes these details to the kernel, which runs the program, and then passes the o/p to the shell, which then displays the o/p to the user. NOTE: the shell remains in memory, just like kernel which remains in memory all the time. Any cmd (i.e program) that we call from inside shell usually resides on disk, so a copy of that program is loaded in memory and run.

Any shell (bash, csh, etc) is basically a pgm to run other programs. It reads a line of input from you, and then interprets it as one or more commands (i.e programs) to run. Shell pgm is no different than any other pgm.

Shell basic operation on being provded a cmd is to:

  • Read i/p from terminal, or file.
  • Then break this into words and operators. These are called tokens. These tokens are separated by metacharacters (characters with special meaning, as whitespace, newline, $, ;, etc).
  • Then parse the tokens into cmds, and perform various shell expansion i.e parameter expansion, wildcard expansion, etc (redirection is performed if necessary). pattern matching for filenames is done after various shell expansion, and then quote removal (for unnecessary quotes) and finally redirection done just before executing the cmd
  • the cmd never sees any special char in it's args, they are gobbled up the shell. It only sees the cmd and full expanded args, w/o any metacharacters. For pattern matching, glob, etc, see section on
    • ex: ls -l *.c. => shell scans this line, identifies 1st token as pgm "ls", 2nd and 3rd token as 2 args. However, 3rd arg has *, which is expanded by the shell (for ex as tmp1.c and tmp2.c). Now the 3 args: -l, tmp1.c and tmp2.c are passed to the "ls" pgm, which never sees any of these meta char.
    • In case shell can't find anything matching after glob expansion, it passes the whole thing unexpanded to the pgm. So, if *.c doesn't match any file. the whole cmd "ls -l *.c" is passed, so ls pgm sees 2 args: -l and *.c. Now it's upon ls to expand *.c, and if it sees nothing matching, it returns "ls: no match".
    • So, the same utility may behave differently in different dir depending on whether it's capable of handling glob wildcards or not.
    • IMP: To prevent this confusing behaviour, we sometimes prefer to enclose these glob special characters within single or double quotes to prevent the shell from seeing these special characters, and instead pass it as literals to the linux pgm. i.e ls -l "*.c" will prevent shell from expanding *.c as it's surrounded within double quotes, which prevents bash shell from expanding it. For more details, see "bash" or "csh" section on how to escape metacharacters. These quoting mechanism are for shell's use and NOT for linux pgm's use => the pgm sees cmd with these quotes stripped out.
    • Since this all may seem confusing, the best way to know all these nuances is to try linux cmds on shell with/without quotes etc, and chck for yourself how they behave. They may behave differently on different machines depending on what incarnation of linux cmds you have. That's why it's best to stick to "GNU" linux cmds.
  • Now the shell asks the kernel to initiate pgm's execution. In this case, ls pgm execution starts with the 3 args provided. Then shell goes to sleep until the pgm has finished.
  • The kernel copies the specified program into memory and begins its execution. This copied program is called a process; in this way, the distinction is made between a program that is kept in a file on the disk and a process that is in memory doing things. If the program writes output to standard output, it will appear at your terminal unless redirected or piped into another command. Similarly, if the program reads input from standard input, it will wait for you to type in input unless redirected from a file or piped from another command. When the command finishes execution, control once again returns to the shell.

  • shell collects exit status of pgm (which is 1 byte, so exit status is from 0-255. exit status of 0 is true, while other exit status indicate error). This is unique since usually 1 is associated with success, and 0 with failure. But in shells, we keep 0 for success, so that we can assign all other values for failure. NOTE: even though we use 0 for true, when used in conditions, expr, etc, we treat 0 as true, and 1..255 as false. i.e (cmd1 & cmd2) will retrun true(0), when both cmd1 and cmd2 returns true(0). If cmd2 returns 1(false), then this expr will return false (as true & false gives false). NOTE: cmd is executed only after all the expansion has been done by the shell. cmd is always passed the final expanded form.
  • At this point, execution of that pgm is finished, and shell awaits for next cmd. On providing next cmd, cycle starts from top bullet above. This cycle keeps on repeating.

Terminal settings: Before we go into details of various shells, let's see how to change settings in Terminal, Konsole, xterm, etc from where we access the shell. In old days when there was no gui, the first prompt that came up was a shell, from where we typed any cmd.

#to change defaults in konsole gui
Goto settings->size->custom, change cols to 120, lines to 40, save it, then goto settings->save as default. That saves anything changed as default so that next time it starts with those. can also use settings->configure Konsole

#to change defaults in terminal gui

Goto Edit->Profiles->Edit(after choosing default profile) and change any setting. It takes effect immediately

Ex: To change scrolling limit to unlimited, goto "scrolling" tab, and then set Scrollback to unlimited. However, unlimited scollback uses a lot of memory, since it requires a lot of scrollback text to be stored in the buffer, so your computer may slow down a lot, or worse may just hang or crash. A decent compromise is to set scrollback to 500 lines or so.

displaying shells:

To get current shell name in an xterm or terminal: echo $SHELL => shows the current shell (bash or csh). Note that even though it may say csh, it's actually tcsh.

alternative way to print shell: ps -p $$ => ps prints snapshot of current processes (ps -ef shows every process on system, -e=all process, -f=long format, -F=full long format). ps -p prints only those process with that pid number. $ returns the PID of current process (which is the shell), so running ps -p on that number (echo $$ returns PID of current process) displays process status of your shell.

Usually "$" prompt is shown for bash, while "%" prompt is shown for csh.

changing shells:

To change startup shell:
/etc/shells has the list of valid shells. This list updates each time you install a new shell. We can also manually modify this file to add the name of a shell (with full path) if it doesn't show up here after installation.

cat /etc/shells

/bin/sh => This is a soft link to bash in /bin/bash. So, even when we use sh, we are actually using bash

/bin/bash

/usr/bin/sh => This is again a soft link to bash in /usr/bin/bash

/usr/bin/bash => There is a copy of bash in /usr/bin too which is exactly same size as /bin/bash

 Usually we'll see bash already installed. Some Linux distros have tcsh installed too. If above file doesn't show the name of shell that we want, we have to install it by typing below cmds.

sudo apt install tcsh => on debian based distro

sudo yum install tclsh => on fedora based distro


To change shell temporarily, just type the new shell name (i.e bash).
To change user login shell permanently, either edit /etc/passwd file (Administrator's right, this file shows all user's default shell type ), or use change shell cmd (chsh). finger on that user shows default login shell.
chsh -s {shell-name with full path} {user-name} , it will ask for user's password
ex: chsh -s /bin/bash johnsk => changes user johnsk shell from csh to bash

/etc/default/useradd file has settings to be applied anytime a new user is created. We can change SHELL= to whatever we want, and then any new user will get this shell type by default.

NOTE: usually on systems where you don't have admin rights, you can't do chsh. So, if you work at corporation, you can change login shell by going to an internal website, which allows you to choose shell. This is done for security purpose.
for bash, type: /bin/bash, for csh, type: /usr/bin/csh

Common shell:

Customizing prompt for diferent shells: Link: http://understudy.net/custom.html

We'll look at 2 most popular shells: bourne shell (bash), and C shell (csh). Most of the Unix cmds can be used in both shells. But we write larger scripting pgms in a file which is either in bash or csh files. They both support constructs like if-else, loops, etc that allow us to write complicated code. However, these scripting languages were written in early days of Unix. In today's age, modern scriting languages like perl, python, etc can do everything that these bash/csh scripts used to do. So, there is really no reason to learn these scripting languages as bash or csh. I'm providing sections on these 2 scripting languages since you may have to read code written in bash/csh, and possibly modify it, as most of the Linux community uses one or both of these.

For my purpose, learning Python will suffice for all your scripting needs, just bypass both bash and csh.