Linux to look into

  1. Login vs Non login shell sessions :
    http://linuxcommand.org/lc3_wss0020.php
    https://mafayyaz.wordpress.com/2012/02/25/linux-bash-login-vs-non-login-shell/
Advertisements

Getting started with Bash Scripting

I am working on Ubuntu 16.04

Step 1: Editing the environment PATH variable to include the directory of our scripts.

Editing the PATH to include the directory of my scripts.
echo $PATH
Edit the .profile file (.bash_profile) PATH, to include the directory of your scripts. (file is in $HOME)
For temporary usage you can set the PATH with : export PATH=$PATH:directory
For user scripts there is bin directory in $HOME. If not create a bin directory in $HOME for your scripts.

.profile is loaded when we login, so we need to logout and login again for changes in .profile to take effect.

 

Step 2: Variables

 

 

 

 

 

Linux Basics

Shell : Wht is it?

Shell is a program that takes commands from the terminal and gives them to the operating system kernel to perform.

A common shell program is bash, ksh, tcsh etc.

 

Terminal: Wht is it?

Terminal also called terminal emulator, is a program that opens a window that lets us interact with the shell.

Most common terminal emulators are gnome-terminal, konsole, xterm.

 

Shell Prompt Analysis

zack@hercules:~$
zack is the user logged into the shell. The machine name is hercules.
If last character of shell prompt is # is instead of $, it means you are logged in as superuser.

 

File Naming facts in linux

  1. File names in linux are case sensitive just like unix.
  2. Files do not need to have an extension in linux.
  3. Files that begin with a period (.) are hidden files

 

Aliases and Shell Functions

They are another way to add new commands. (the words may not be technically correct.)

Alias
Syntax alias name=value
eg. alias l=’ls -la’

Shell Functions: Can be used to add more complex commands to shell

today() {
echo -n “Today’s date is: ”
date +”%A, %B %-d, %Y”
}
src of above command

To add alias and shell functions edit the .bashrc file in home directory and start a new terminal.

 

Redirection of Standard Input/ Output

There are 3 main redirection symbols >, >>, <

: If file exists it will be overwritten or else new file will be created. eg. ls > abc.txt
>>: If file exists it will be appended with the new results or else new will be created.
eg. ls >> abc.txt
<: To redirect input to a command through a file not through keyboard input.
eg. cat < abc.txt

eg. sort < names > sorted_names.txt
eg. tr [a-z] [A-Z] < names > capital_names.txt

 

Processes:
An instance of running command is called process and the number printed by shell is called process-id (PID).

The ampersand (&) at the end of command tells shells to start the process in background.
eg. $ ls / -R | wc -l &

src of the below table

For this purpose Use this Command Examples*
To see currently running process ps $ ps
To stop any process by PID i.e. to kill process kill    {PID} $ kill  1012
To stop processes by name i.e. to kill process killall   {Process-name} $ killall httpd
To get information about all running process ps -ag $ ps -ag
To stop all process except your shell kill 0 $ kill 0
For background processing (With &, use to put particular command and program in background) linux-command  & $ ls / -R | wc -l &
To display the owner of the processes along with the processes   ps aux $ ps aux
To see if a particular process is running or not. For this purpose you have to use ps command in combination with the grep command ps ax | grep  process-U-want-to see

 

For e.g. you want to see whether Apache web server process is running or not then give command$ ps ax | grep httpd
To see currently running processes and other information like memory and CPU usage with real time updates. top
.

$ top
Notethat to exit from top command press q.
To display a tree of processes pstree $ pstree

 

Exit Status of a Linux Command

If return value/ exit status of a command is 0, then command was successful.

If return value/ exit status non zero then command was not successful or some error occured

To check the exit status of a program echo $? .
eg. echo “hello”
echo $?

 

 

Linux File System

Directory Description
/
The root directory where the file system begins. In most cases the root directory only contains subdirectories.
/boot This is where the Linux kernel and boot loader files are kept. The kernel is a file called vmlinuz.
/etc The /etc directory contains the configuration files for the system. All of the files in /etc should be text files. Points of interest:

/etc/passwd
The passwd file contains the essential information for each user. It is here that users are defined.
/etc/fstab
The fstab file contains a table of devices that get mounted when your system boots. This file defines your disk drives.
/etc/hosts
This file lists the network host names and IP addresses that are intrinsically known to the system.
/etc/init.d
This directory contains the scripts that start various system services typically at boot time.
/bin, /usr/bin These two directories contain most of the programs for the system. The /bin directory has the essential programs that the system requires to operate, while /usr/bin contains applications for the system’s users.
/sbin, /usr/sbin The sbin directories contain programs for system administration, mostly for use by the superuser.
/usr The /usr directory contains a variety of things that support user applications. Some highlights:

/usr/share/X11
Support files for the X Window system
/usr/share/dict
Dictionaries for the spelling checker. Bet you didn’t know that Linux had a spelling checker. See look and aspell.
/usr/share/doc
Various documentation files in a variety of formats.
/usr/share/man
The man pages are kept here.
/usr/src
Source code files. If you installed the kernel source code package, you will find the entire Linux kernel source code here.
/usr/local /usr/local and its subdirectories are used for the installation of software and other files for use on the local machine. What this really means is that software that is not part of the official distribution (which usually goes in /usr/bin) goes here.

When you find interesting programs to install on your system, they should be installed in one of the /usr/local directories. Most often, the directory of choice is /usr/local/bin.

/var The /var directory contains files that change as the system is running. This includes:

/var/log
Directory that contains log files. These are updated as the system runs. You should view the files in this directory from time to time, to monitor the health of your system.
/var/spool
This directory is used to hold files that are queued for some process, such as mail messages and print jobs. When a user’s mail first arrives on the local system (assuming you have local mail), the messages are first stored in /var/spool/mail
/lib The shared libraries (similar to DLLs in that other operating system) are kept here.
/home /home is where users keep their personal work. In general, this is the only place users are allowed to write files. This keeps things nice and clean 🙂
/root This is the superuser’s home directory.
/tmp /tmp is a directory in which programs can write their temporary files.
/dev The /dev directory is a special directory, since it does not really contain files in the usual sense. Rather, it contains devices that are available to the system. In Linux (like Unix), devices are treated like files. You can read and write devices as though they were files. For example /dev/fd0 is the first floppy disk drive, /dev/sda (/dev/hda on older systems) is the first hard drive. All the devices that the kernel understands are represented here.
/proc The /proc directory is also special. This directory does not contain files. In fact, this directory does not really exist at all. It is entirely virtual. The /proc directory contains little peep holes into the kernel itself. There are a group of numbered entries in this directory that correspond to all the processes running on the system. In addition, there are a number of named entries that permit access to the current configuration of the system. Many of these entries can be viewed. Try viewing /proc/cpuinfo. This entry will tell you what the kernel thinks of your CPU.
/media,/mnt Finally, we come to /media, a normal directory which is used in a special way. The /media directory is used for mount points. As we learned in the second lesson, the different physical storage devices (like hard disk drives) are attached to the file system tree in various places. This process of attaching a device to the tree is called mounting. For a device to be available, it must first be mounted.

When your system boots, it reads a list of mounting instructions in the file /etc/fstab, which describes which device is mounted at which mount point in the directory tree. This takes care of the hard drives, but you may also have devices that are considered temporary, such as CD-ROMs, thumb drives, and floppy disks. Since these are removable, they do not stay mounted all the time. The /media directory is used by the automatic device mounting mechanisms found in modern desktop oriented Linux distributions. On systems that require manual mounting of removable devices, the /mnt directory provides a convenient place for mounting these temporary devices. You will often see the directories /mnt/floppy and /mnt/cdrom. To see what devices and mount points are used, type mount.

Refer to this for more explanation

Linux Useful Commads

View the list of hidden files (files that begin with a period character .)
use  -a with ls.
eg. ls -la

cd command tricks
cd followed by nothing will change the directory to your home directory.
cd – : changes working directory to previous one.
cd ~zack: changes working directory to my home directory.

 

Useful programs
less
: can be used to view text files   eg. less file-name
file : can be used to get the type of data that a file contains. eg. file file-name

 

tr : translate range of characters into other range of characters.
eg. Translate characters in lowercase to uppercase. tr [a-z] [A-Z]
eg. Translate character a to b and 2 to 3. tr “a2” “b3” < names.txt

 

whoami: current user logged in
who: current user logged in

head :
head abc.txt     # display the first 10 lines
head -n4 abc.txt     # display the first 4 lines of file
head -n-6 abc.txt    # display all the lines except the last 6 lines of file

tail:
tail abc.txt    # display the last 10 lines of the file.
tail -n5 abc.txt    # display the last 5 lines of the file.
tail -n+8 abc.txt     # display from the 8th line till the last.

 

top: To see currently running processes and other information like memory and CPU usage with real time updates.

 

sort: sort the characters
uniq:
removes duplicates by comparing the adjacent values
eg. $ sort < sname | uniq > u_sname

 

bc: it is the calculator program in linux

Value Shown in bc as Shown in Linux Shell as
True/Yes 1 0
False/No 0 Non – zero value

cal : it’s the calendar program

Builtin shell variables:
$# : Gives no. of arguments to command.
$0 : Shell script name
$1, $2 : Argument 1 and 2 to script.
$*, $@ : Gives all the arguments to the script.

eg. Script name is abc
abc hello world
echo “No. of args $#”
echo “First arg $1”
echo “Second arg $2”
echo “Script name $0”