Week 8 Flashcards
What three files are opened by default when a process starts
stdin, stdout, stderr
What is the purpose of a pipe
To put the output of one program as the input to another
ex ls | grep ‘foo’
What are the file descriptors of the three default files? and what describes them
stdin = 0, by default a read only file
stdout = 1, by default a write only file
stderr = 2, by default a write only file used for error and non-normal messages
No argument is by default 0 or 1
ex mycmd > out.txt 2>&1 places stdout to out.txt, and assigns stderr (2) to what stdout is pointing to (out.txt)
How do you connect inputs, and outputs
<, >. doing > places it in something < reads from
ex: mycmd <infile.txt
What are all the shell variables and what do they do
$0 - name of shell
$# - number of args
$1 - first arg
$* - all args seperated by spaces
$? - exit code of last cmd (0 = success)
Shell Script Definition?
a text file containing commands to be executed for a shell
How do we flip (flick) the execution bit (bean)
chmod +x file.extension
What are the different magic number lines
0x7F E L F - File contains linux binary code
M Z - File contains windows binary code
#! - path to the shell
Default: uses default shell
What does shift do?
Shifts the position arguments.
EX: example.sh 1 2 3
while(($#>0))
do
echo $1
shift
done
output is 1, 2, 3
What are the differences between (()) and [[]]
(()) - integer arithmetic ( you can assign variables within this ) $ not required
[[]] - true/false comparison between strings or files
what files are called when a new shell file starts
.bash_profile - read whenever a login shell starts
-bashrc - read when any bash shells are started
What are the different flags for find?
-type -size -time -name
-iname -atime +(time) -or
what do -f -d -l do?
-f = file
-d = directory
-l = link
Whats the difference between -name and -iname
-iname is case insensitive
What does find do ?
searches every file within a given list of directives
ex: find /home/student -name ‘*.txt’
What actions can you pass to find?
-print (default )
-delete
-exec
what does a2ps and lpr do?
lpr prints a file, a2ps converts the file to postscript and sends to stdout
What does grep do?
Searches line by line through fiels to find a regex (stdin by default)
ex: grep pattern file1 file2
What do these do in order? (GREP)
-i : -w : -v : -H : -h : -l : -q
case insensitive match, match must be the whole word,
lines that DONT match,
shows names of files,
do not show names of files ( default ),
only shows file names (not lines)
print nothing
What are the exit statuses of grep?
0 - atleast 1 match, 1- no match, 2 - error
What are the three types of regex? Give a description of each
Globbing - Method used by the shell and find to match filenames/paths used by shell and find using wildcards
normal regex - pattern matching syntax used by tools for string matching
extended regex - more advanced pattern matching (enabled with -E in grep and sed) which incldues addition features (+, ?, |, ())
What do the following normal regex do?
.
$
\
*
[]
[^]
. - matches any 1 char
^ -matches beginning of line
$ -matches end of line
\ - negates special meaning of char i.e., .
* - . but repeated ex xy*z would find xyyyyyyz xyyz…
[] - . but with any of the characters specified
[^] - match any character NOT specified in this list ex [^0-9] matches anything not a digit
What shell variable contains the PID of the shell?
Whats it useful for?
$$
Creating unique names
What directory is writable by all users, and can be used to store the output of commands (for a short time)
/tmp