Shell - program that provides users means to interact with other programs.
There
are different shell programs, with their own languages (bash, dash, csh, zsh, ksh)
terminal - text input/output environment, if in DE they are emulated as software
within DE
tty - teletypewriter, one of first terminals, the shorthand stuck
console - physical terminal
highlighted text - is in buffer maintained by x, and it can be pasted by middle
click
Bash can run in two different modes:
Bash interactive mode - user inputs commands and executes them, after every command
needs to wait for bash to finish
scripts- pre-written commands, non-interactive mode
Most programs work by communicating to Kernel - kernel is "root" program that
communicates to hardware and allows other programs to run
Program is collection of instructions that run when executed
library - non volatile resources used by programs
Software - program + libraries + data
System call - When a process/program makes request of the kernel
File descriptor - hooks of a process to an outside world -> to devices, files or
other processes
Stream - data that flows through file descriptor connecting two
processes/device/file
First three file descriptors have standard names-standard input, standard output,
standard error
Prompt - one or more characters in command line interface indicating readiness of
command prompt
$ user
# root
> command is not finished
(blank) non interactive mode
bash scripts have hashbang first line like so #!/usr/bin/env bash
/usr/bin/env is a program finding program intended to seek program mentioned (eg
bash) on a computer
simple command - variables NAME options arguments redirection
variables->optioanl variables intended for process
name->name of the program to run, it searches in this order for
function - declared functions are stored in a list that bash first searches
when command is given
builtin - procedures built in bash itself
program - in configured path tries to find program to run
options->listed after "-" usually as single characters, sometimes can be listed in
long format "--help"
arguments->optional arguments for the program
redirection->intended to change the stream of the file descriptors
>donk.ls ->sends descriptor 1 to donk.ls
2>/dev/null ->sends descriptor 2 to "nothing"
2>&1 ->write fd2 where 1 is being written (very important to not send them
independently on same place because streams will mix together in nonsense)
[x]>file (write to file from file descriptor x(defaults to FD 1))
[x]<file (read from file with file descriptor x(defaults to FD 0))
[x]>&y make FD x write to y's stream
[x]<&y make FD x read from y's stream
[x]>>file instead of emptying file and then writing ([x]>file) this appends to
the end of file
<<[x]delimiter
wholetext } this reads from wholetext to x descriptor (0 default),
we set delimiter at the start which will end the text in between.
delimiter
<<<string make FD 0 read from the string
x>&- close FD x
x>&y- replace FD x by FD y, close FD x
&>file make both FD 1 and 2 write to file
x<>file open FD x for both reading and writing to file
redirections are read from left to right!
pipe - command | command2 ->sends d output of first as standard input of second
command
command |& command 2 ->sends standard output and standard error of first as
standard input of second command
compound commands - they are intended so multiple commands can be grouped within {}
to allow complex flow control of the bash
control operators -> set between commands to tell them how to be executed and in
what condiditons
command; command -> ; is same as enter
command || command2 -> || tells to run command 2 only if command 1 failed
coprocess-allows for a process to run in the background it allows piping its input
and output
coproc [name] command [redirection]
functions - allow grouping of commands under one name for faster use
name ()
{compund-command} [redirection]
PATH - it is environment variable, it conttains set of directories when searching
for programs, for unix it looks like this PATH=/bin:/sbin:/usr/bin:/usr/sbin, it
searches in that order and stops when program is found
type commmand can be used to locate programs, it searches PATH folders and
returns where it is located, before that it searches functions and builtins, type
has argument -a that allows to find all options (functions, builtins and programs)
otherwise it would just give one, first, answer
if a command has / in it it doesnt search PATH, the command is assumed to be
full path to a command
Arguments=words=tokens -> for bash it is single unit of information, words are
separated by space, when we need more words in a single argument, to make spaces
literal put words within quotes (''->absolutely literal,""->allows some expansions
such as variables) or use escape characters before space (/)
Must quote ->whenever we have whitespace or a symbol in argumnet
Don't quote ->inside [[tests and arounbd ${...+...} expansions
Bash paramaters - regions in memory where we can temporarily store some information
positional parameters
special parameters
shell variables
Shell variables - we assign variables to it's name with "="(!no spaces around!) and
we can call it later by it's name with $name (expansion), we can use ${name} to
tell where the begining and the end of the variable is or when we want to use
operators on variable data we use them within those brackets
OPERATORS:
name#pattern removes the shortest string that matches the pattern at the start
name##pattern removes the longest string that matches the pattern at the start
name%pattern removes the shortest string that matches the pattern at the end
name%%pattern removes the longest string that matches the pattern at the end
name/pattern/replacement removes the first string that matches pattern with the
replacement
name//pattern/replacement removes all strings that match the pattern with the
replacement
name/#pattern/replacement removes the string that matches the first pattern
with the replacement
name/%pattern/replacement removes the string that matches the last pattern with
the replacement
#name expands the length of the value in bytes
name:start(:length)->optional cuts the string, starts on start inclusive, stops
after length bytes
name(^|^^|,|,,)(pattern) uppercase first, upercaseall, lowercase first,
lowercase all or same but just for the patterns if specified
Environment variables->variables that belong to the program process, they can be
used by the process or shell itself, and whenever a new process is started
enviornment variables are COPIED to that new process(child), however if we change
those variables in new process they will be changed only there
Shell variables->variables belonging to bash and it's own processes
Constants->variables that are not meant to be changed, although there is sintax for
it, convection is to just use variables with a ALLCAPS name
$? ->parameter of a last exit status (0 for success, 1-255 for rest)
When we write scripts we should always includ3e exit command like so
exit 0 or exit 1 ->exit commands terminate immediately
Shell initialization->When interactive bash session is started, bash prepares
itself by reading files. .bash_profile is in home directory and it is intended so
we can export variables in the environment, if not found it will look for .profile
which is intended for all shell programs
Positional paramaters->something like variables, but instead of the name they have
a number. By default positional paramaters are command arguments and pos par 0 is
the name of the command itself. they can be changed with:
set -- firstarg secondarg "third arg"...
shift x -> this pushes x arguments out and moves later arguments to front
bash -c string-> -c tells bash to read commands from string, arguments after
the string are assigned to the positional parameters starting with $0 (good
practice is to start $0 as --)
Special parameters->special parameters are like variables but it's name is
represented by single symbol
$* expands a single string joining all positional parameters into one separated
by first char nin IFS(by default a space)
$@ expands positional parameters as a list of separate arguments
$# expands into a number indicating the amount of positional parameters that
are available
$? expands the exit code of the last command that finished (0 for success) -
>useful for checking if last command succeded
$- expands to the set of option flags that are active in the shell
$$ expands to the number that is the unique process identifier of the last
process that started in the background(asynhronously)
$_ expands to the last argument of the previous command
Internal shell variables-> handy variables intended for the user written in
ALLCAPS, good practice is to write shell variables in lower case and environment
variables in UPERCASE
Linux file organisation-it is always single tree, additional storage might come as
branches, never as another root
permissions-three different cases USER, GROUP and OTHERS, in ls -l 9 charachters
describe rwx for these 3
cd ->change directory
. ->current directory (./ttytw)
.. ->parrent directory
cd -> home directory
cd ~user -> home directory of user
pwd ->path of working directory
ls ->list files and directories,
argument -a to list hidden files (that start with .)
argument -l for long format
less ->view text files (commands b, space, G, 1G, /characters, n, q for
pageup,down,end,start,searchnext,next,quit)
file -> classify a file's contents
cp ->copy files/directories
cp file1 file2 ->copies file1 into file2, if file2 doesn't exist it creates it
cp file1 dir1 ->copies contents of file1 inside dir1
cp -R dir1 dir2 ->copies contents dir1 into dir2, if dir2 doesn't exist it is
created
mv ->remove rename files
mv file1 file2 ->rename file1 into file2 OR replace contents of file2 with
file1
mv file1 file2 file3 dir1 ->moves files to dir
mv dir1 dir2 ->if dir2 doesn't exist dir1 is renamed, if dir2 exists dir1 is
created within
rm ->remove files/directories
rm file1 file2 file3 ->delete files
rm -r dir1 dir2 ->delete folders
mkdir ->make directory
mkdir directory
lpr ->accepts standard input and sends it to the printer
cat ->concatenate files and send to standard output
tar ->for manipulating archives
tar -cf archive.tar foo bar ->create archive.tar from files foo and bar
tar -tvf archive.tar ->list all ifles in archive.tar verbosely
tar -xf archive.tar ->extract all files from archive.tar
Wildcards ->special characters used to specify filenamse
* any characters
? any character
[characters] matches character that is member of the set characters [:alnum:]
[:alpha:][:digit:][:upper:][:lower:]
[!characters] matches any character that is not a member of the set character
filters ->class of programs that take standard input and filter the data to
standard output
sort ->sorts data
uniq ->removes duplicate lines
grep ->outputs lines with specified pattern of characters
fmt ->reads text from standard input then outputs formatted text on output
pr ->takes text input and prepares it for printing(page breaks, headers,
footers)
head ->outputs first 10 lines of the input
tail ->outputs last 1' lines of the input
tr ->translates characters
sed ->stream editor(for more complex translations)
awk ->entire programming language designed for constructing filters
/boot ->files of boot loader and kernel(vmlinuz)
/etc ->directory for configuration files of the system, all should be text
/passwd ->information about users
/fstab ->table of devices that get mounted on boot, defines disk drives
/hosts ->host names and ip adresses
/init.d ->directory with scripts that start various system services, usually at
boot
/bin ->essential programs for system
/usr/bin ->applications for system users
/sbin ->programs for system administration(also /usr/sbin)
/usr ->directory for applications support
/share/x11 ->support files for the X Windows system (windowing system on bitmap
displays)
/share/dict ->dictionaries for spelling checker
/share/doc ->documentation files
/share/man ->man pages
/src ->source code files
/local ->directories used for installation of programs not included with
distro(usually /local/bin)
/var ->files that change as system is running
/log ->log files that update as system runs
/spool ->holds files that are queued for some process, such as mail messages
and print jobs
/lib ->shared libraries
/home ->where users keep their personal work, in general only place allowed for
users to write to
/root ->superusers home directory
/tmp ->directory for programs to write their temporary files
/dev ->special directory with available devices
/proc ->special directory, peepholes to the kernel:its processes and system configs
/media ->directory used for mount points
/mnt ->/media for removable devices such as floppys, cds
permissions:
with ls -l we can get permission status as frwxrwxrwx where f can be -(file) or
d(directory) and others represent if owner/group/others have right to read write or
execute the file
chmod xyz somefile -> xyz each represent one number between 0 and 7 that represent
rwx (7 for 111, 0 for 000)
su ->program to get superusers access, it requires su password, to exit su session
type exit
sudo ->alternative on some distros to su, it gives option to get su privileges just
on a desired command, users password is required
chown user file ->change ownership of file to user(requires su)
chgrp group file ->change group of file to group
job control:
process & ->run it in the background
ctrl+z ->put a running process in suspend
bg - put a pocess in the background
fg - put a process in the forground
ps -list processes running on the system(we get process ID)
jobs - alternative way to list(we get job number)
kill - send a signal to the process
kill processID or %jobnumber - sends termination signal
kill -ARGUMENT process ->where argument is either -number or -command
1 (SIGHUP) hang up signal
2 (SIGINT) interrupt signal ->ctrl+c in running terminal
15 (SIGTERM) termination signal
9 (SIGKILL) kill signal, no chance of program to listen
Environment:
set ->program that outputs ENVIRONMENT
login shell startup files:
/etc/profile ->global configuration script for all users
~/.bash_profile ->personal startup file
~/.bash_login ->read if no ~/.bash_profile is found
~/.profile ->read if no bash_profile and no bash_login is found(default on
debian)
non-login shell session: (NOTE:non login shell usually inherits Environment from
login shell as its child)
/etc/bash.bashrc ->global configuration script
~/.bashrc ->users personal startup file
export ->program for sending environment to child processes
export -p ->outputs current export
export [-fn] [name[=value] ...] ->marks each name for export, assign value if
supplied; n=remove export property from name; f=refer to shell functions
alias name=value ->creates a new command that acts as an abbreviation
uptime, df(driver function), du(usage of home directories), ->command for some
general info
test expression OR [expression] ->gives out 0 for true or 1 for false
-d file ->true if file is a directory
-e file ->true if file exists
-f file ->true if file is a regular file
-L file ->true if file is a symbolic link
-r/w/x file ->true if file is readable/writeable/executable by you
file1 -nt/-ot file2 ->true if file1 is newer/older than file2
-z/-n string ->true if string is empty/not empty
string1 =/!= string2 ->true if string1 equals/doesnt equal string2
id -u ->command that outputs the id of an user, superuser has 0 so we can use it
when we need su privileges
if statement; then
code
elif statement
code
else
code
fi
Since commands have exit code we can use it in if statement like so
if whole command; then
code
fi
case word in
patterns ) commands ;;
patterns ) commands ;; }->this checks word for specified patterns, if it
matches it runs commands
esac
while statement; do
commands
done
until statement; do
commands
done
for variable in iterable; do
commands }for loop, assigns all items in iterable to $variable
done
set -x ->creates tracing mode, bash will show everything it does as it executes it
set +x ->turns off tracing mode
read variable ->asks for user input and assigns it to variable, if nothing follows
after read it assings it to $REPLY
read -t x variable ->-t argument adds waiting for x seconds before continuing
read -s variable ->-s argument makes typed letters invisible
$((integer mathematics)) ->supports +-*/% and **
printf ->similar to echo, except it allows lot of functionality by involving format
string
find [-H] [-L] [-P] [path...] [expression]
uname -a ->for printing system information, -a or -all for everything
command1 && command2 ->command2 executes only if command1 returns exit status 0
command1 || command2 ->command2 executes only if command1 doesn't return exit
status 0
opposed to python where these are boole operators, here they are used as
control flow
trap arg signals ->arg are commands that we want to be executed when specified
signals occur(eg SIGHUP SIGINT SIGTERM)
trap -l ->to list all the signals
note:SIGKILL cannot be trapped!
virtual terminals ->sessions that run behind the graphical desktop, often
accessible by pressing Ctrl-
Alt-F1 - F6; to return to GUI ALT-F7
Linux has always the single root directory, additional storage devices are mounted
at various points in the tree at whims of sys admin
Linux doesn't have concept of file extensions, some applications within linux
however do
commands:
date ->displays date
cal ->displays callendar
df ->to display free space on disk drives
pwd ->print working directory
cd ->change directory (arg "-" ->previous directory)
ls ->list items (option -l for long format (access rights/# of hard
links/owner/group/size/date mod/name)
free ->to displaxy the amount of free memory
exit ->ends a terminal session
file ->displays information about file of the arguments
less ->displays content of arguments