Lecture 1 Flashcards
How many namespaces do operating systems like Windows and Unix have?
Windows can have many. i.e. C:\, D:\, etc.
Unix has 1. /
What is a path name?
A description of where a file can be found.
In Unix example: dir1/dir2/dir3/file.c
What are some special directories in the Unix root?
/bin
/sbin
/lib
/usr
/etc
/home
/boot
/dev
/opt
/var
/tmp
/proc
/bin directory.
Where most executables or binaries are found. Things like gcc or vim. Essential for operation of the system itself.
Home directory shorthand.
~/
Know this well.
/sbin directory.
System binaries that should only be executed by the root (admin) user.
/lib directory.
Libraries.
/usr directory.
User binaries, libraries, etc. Meant for the end-user and not required for the system itself.
/etc directory.
Editable Text Configurations. Where configuration files are found.
/home directory.
User home (or personal) directories found here.
/boot directory.
Files needed to boot the system like the operating system kernel.
/dev directory.
Device files. Allows interface with I/O devices.
/opt directory.
Optional software located here. Rarely used.
/var directory.
Variable files that change as the system operates. Like log files.
/tmp directory.
Temporary files not persistent between system reboots.
/proc directory.
An imaginary directory created by the kernel to keep track of a currently running process.
Change directory command.
cd
How to go to home directory?
~
What does BASH stand for?
Bourne Again Shell.
General format of commands.
command [arg1] [arg2] … [argn] RETURN
Two ways to get help on a command through BASH.
command –help
or man command
What command is EOF?
ctrl+d
Output redirection.
> overwrites
> appends
command [arguments] > filename
Input redirection
<
command [arguments] < filename
Pipelines (review).
One or more commands separated by |
Connects standard output of the command preceding the pipe symbol to standard input of the following.
command_a [arguments] | command_b [arguments]
echo command
Takes input and puts it in output.
tr command
tr string1 string2
It then looks for characters that match string1 and finds the corresponding in string2.
Example:
tr abc xyz
a
x
abab
xyxy
printf command
Different than C function.
Can be used in pipelines to pass input.
What is BASH scripting?
One or more shell commands in a text file.
Running a BASH script makes it run the commands one by one.
Everything a BASH script can do can be done in the command line, and vice versa.
This includes variables and control structures.
First line of a BASH script.
!/bin/bash
How to write comments in BASH script?
comment goes here
Variables in BASH.
They are strings.
name=value
or
let name=value
Leave no spaces around ‘=’
Variable reassignment in BASH.
Use let.
Example:
let x=x+1
Accessing command line arguments.
$1 gives first positional argument (‘arg1’).
Using if-else in BASH.
if test-command
then
commands
fi
or
if test-command
then
commands
else
commands
fi
test-command in BASH
Example:
[ $variable conditional-operator value ]
Such as [ $1 == “apple”]
Checks if arg1 passed to the command is “apple”.
Conditional operators in BASH
== equality
!= not equality
< if string1 sorts before string2 lexicographically
> opposite of <
Also,
arg1 OP arg2
If OP is one of -eq, -ne, -lt, -le, -gt, -ge, they act as equal to, not equal to, less than, and so on
How to chain if-else statements in BASH?
if test-command
then
commands
elif test command
then
commands
…
else
commands
fi
! pneumonic to remember BASH scripts.
!, shabang
#!/bin/bash
While loops in BASH.
while test-command
do
commands
done
How to make a bash scipt?
vim myscript.sh
(.sh or .bash by convention)
convert to executable by using
chmod +x myscript.sh
run using
./myscript.sh