P.19
Bash and Command Types
The bash shell understands the following types of commands:
‧ Aliases such as ll
‧ Keywords such as if
‧ Functions (user defined functions such as genpasswd)
‧ Built in such as pwd
‧ Files such as /bin/date
The type command can be used find out a command type.
List of command bash keywords and built in commands
‧ JOB_SPEC & ‧ (( expression )) ‧ . filename ‧ [[:]] ‧ [ arg... ] ‧ expression ‧ alias
‧ bg ‧ bind ‧ builtin ‧ caller ‧ case ‧ command ‧ compgen
‧ complete ‧ continue ‧ declare ‧ dirs ‧ disown ‧ echo ‧ enable
‧ eval ‧ exec ‧ exit ‧ export ‧ false ‧ fc ‧ fg
‧ for ‧ getopts ‧ hash ‧ help ‧ history ‧ if ‧ jobs
‧ kill ‧ let ‧ local ‧ logout ‧ popd ‧ printf ‧ pushd
‧ pwd ‧ read ‧ readonly ‧ return ‧ select ‧ set ‧ shift
‧ shopt ‧ source ‧ suspend ‧ test ‧ time ‧ times ‧ trap
‧ true ‧ type ‧ typeset ‧ ulimit ‧ umask ‧ unalias ‧ unset
‧ until ‧ variables ‧ while
P.22
bashrc, etc/profile.d
P39
Use of builtin command
set -x
set -v
set -n
P44
echo "$PATH" can print out the default path
P45(P38)
Assign values to shell variables
P47
THe := syntax (default value)
P48
variable name must with alphanumeric character or underscore character (_)
variable names are case-sensitive
Don't put spaces on equal
ex. no=10 (O)
no = 10 (X)
P51
printf format directives
P53
Quoting (single / double), backslash
in single quote, the variable won't show the value
P54
backslash escaped characters
P56
export statement
use export command to export variables and functions to child processes
P59
IFS: Internal Field Separator
shell 預設所使用的內部欄位分隔符號
P62
mathematical operators and order
P63
create an integer variable: use the declare command
(why we need this?)
P66
bash hell environment
How to view / export local variable / enviroment variables
P68
Common Environment Variables
Home / PATH / PWD
P73
Wildcards: base supports the following three simple wildcards
1. * Matches any string, including the null string
2. ? Matches any single (one) character.
3. ... Matches any one of the enclosed characters.
P75
alias, modify ~/.bashrc to make it permanently
P76
the tilde expansion (~)
P77
startup scripts related info
script execution order
P88
using aliases: edit bashrc to add alias
CH4
P93
test command (if it's useful?)
P94
if structures to execure code based on a condition
if condition
then
xxx
ooo
fi
P96
if command
then
xxxx
else
oooo
fi
P100
Multilevel if then else
if condition
then
oooo
xxxx
elif condition1
then
oooo
xxxx
elif condition2
then
oooo
xxxx
...
...
fi
P102
exit status is
0: OK
1: minor problems
2: serious trouble
P104
rm /tmp/filename && echo "File deleted."
The echo command will only run if rm exits successfully with a status of zero
P105
cat /etc/shadow 2>/dev/null || echo "Failed to open file"
'Failed to open file' will be displayed cat command failed to open the
file.
combine both logical operators
cat /etc/shadow 2>/dev/null && echo "File successfully opened." || echo
"Failed to open file."
P108
Codition expression using [
It's used for:
File arrtibutes comparisions
Perform string comparisions
Arithmetic comparisons
[ condition ]
[ ! condition ]
[ condition ] && true-command
[ condition ] || false-command
[ condition ] && true-command || false-command
P109
Numeric comparison
eq: equal
ge: greater than or equal to
gt: greater than
le: less than or equal to
lt: less than
ne: not equal to
P111
String comparison
equal:
STRING1 = STRING2
not equal:
STRING1 != STRING2
The length of STRING is zero (useful to see if variable is empty or not)
-z STRING
P112
File attributes comparisons
-a file
True if file exists.
-b file
True if file exists and is a block special file. (what is block special file?)
-c file
True if file exists and is a character special file.
-d dir
True if file exists and is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-g file
True if file exists and is set-group-id.
-h file
True if file exists and is a symbolic link.
-k file
True if file exists and its ‘‘sticky’’ bit is set.
-p file
True if file exists and is a named pipe (FIFO).
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than zero.
-t fd
True if file descriptor fd is open and refers to a terminal.
-u file
True if file exists and its set-user-id bit is set.
-w file
True if file exists and is writable.
-x file
True if file exists and is executable.
-O file
True if file exists and is owned by the effective user id.
-G file
True if file exists and is owned by the effective group id.
-L file
True if file exists and is a symbolic link.
-S file
True if file exists and is a socket.
-N file
True if file exists and has been modified since it was last read.
P120
A Note About $@ and $*
$@ expanded as "$1" "$2" "$3" ... "$n"
$* expanded as "$1y$2y$3y...$n", where y is the value of $IFS variable
P121
Parameters set by the shell
All command line parameters or arguments can be accessed via $1, $2, $3,..., $9.
$* holds all command line parameters or arguments.
$# holds the number of positional parameters.
$- holds flags supplied to the shell.
$? holds the return value set by the previously executed command.
$$ holds the process number of the shell (current shell).
$! hold the process number of the last background command.
P122
Add usage functionality to the script
P126
case $variable-name in
pattern1)
xxxx
xxxx
;;
pattern2)
xxxx
xxxx
;;
*)
esac
case $variable-name in
pattern1|pattern2|pattern3)
xxxx
;;
esac
P130
Dealing with case sensitive pattern
1. use: tr '[:upper:]' '[:lower:]' to convert a pattern to lowercase
2. Use regex with case patterns
3. Turn on nocasematch
---------- CH5 for loop ----------
P134
for var in item1 item2 ... itemN
do
command1
command2
....
done
or
for var in list-of-values
for var in file1 file2 file3
for var in $fileNames
for var in (( EXP1; EXP2; EXP3 ))
P136
for loop, The lists or values are normally:
1. Strings
2. Numbers
3. Command line arguments
4. File names
5. Linux command output
P142 while loop
while [condition]
do
command1
command2
...
done
read from file
while IFS= read -r line
do
command1
command2
...
done < "/ptah/to/filename"
while IFS= read -r field1 field2 field3...
do
command1
command2
...
done < "/path/to dir/file name with space"
P143 Using ((expression)) Format With The While Loop
f the value of the expression is non-zero,
the return status is 0; otherwise the return status is 1.
while [ $n -le 5 ] = while (( $n <= 5 ))
P144
while IFS= read -r f1 f2
use IFS to separete f1, f2
input string: nameserver 127.0.0.1
f1: nameserver, f2:127.0.0.1
while IFS=: read -r user enpass uid gid desc home shell
do
....
....
done < "$file"
read file and separate different column
P146
Use of : to set infinite while loop
You can use : special command with while loop to tests or set an infinite loop or an endless loop
PS. : is command
if [xxxxxxx] ; then
:
else
:
fi
use : first, or it there is nothing if loop, it will fail
: ${var:=default}
if var is null, var will be default
: > 123.txt
clean out 123.txt
: can be comment line in .sh
P148
The until loop statement
Just like while loop, until loop is also based on a condition.
The while loop vs the until loop
1. The until loop executes until a nonzero status is returned.
2. The while command executes until a zero status is returned.
3. The until loop always executes at least once.
P150
The select loop statement
(not study)
P155
Using the break statement
Use the break statement to exit from within a FOR, WHILE or UNTIL loop
PS.
use for loop to get file in the folder
for f in /etc/*
P157
Using the continue statement
for i in something
do
[ condition ] && continue
....
....
done
while true
do
[ condition1 ] && continue
......
......
[ condition2 ] && break
done
P160
Command substitution
You can use the grave accent (`) to perform a command substitution. The syntax is:
`command-name` or $(command-name)
---------- CH6 Shell Redirection----------
P164
Before acommand is executed, its input and output may be redirected using a special notation interpreted
by the shell. For example, sending output of date command to a file instead of to the screen.
Changing the default path of input or output is called redirection.
In Linux everything is a file.
Your hardware is also a file:
‧ 0 - Input - Keyboard (stdin)
‧ 1 - Output - Screen (stdout)
‧ 2 - Error - Screen (stderr)
The above three numbers are standard POSIX numbers and also known as file descriptors (FD). Every Linux
command at least open the above streams to talk with users or other system programs.
P165
Standard input
< is input redirection symbol and syntax is: command < filename
Standard output
> is output redirection symbol and syntax is: command > output.file.name
Standard error
2> is input redirection symbol and syntax is:
command 2> errors.txt
P168
Empty file creation
To create empty file use the following syntax:
>newfile.name
> operator redirects output to a file. If no command given and if file doesn't exist it will create empty file.
P169
/ dev/ null discards unwanted output
All data written on a /dev/null or /dev/zero special file is discarded by the system.
Use /dev/null to send any unwanted output from program/command and syntax is:
command >/dev/null
This syntax redirects the command standard output messages to /dev/null where it is ignored by the shell. OR
command 2>/dev/null
This syntax redirects the command error output messages to /dev/null where it is ignored by the shell. OR
command &>/dev/null
This syntax redirects both standard output and error output messages to /dev/null where it is ignored by the shell.
grep vivek /etc/passwd >/dev/null && echo "Vivek found" || "Vivek not found"
to show only found or not found, do not print out the grep result
P171
Here documents
To create a here document use the following syntax:
command <<HERE
text1
text2
testN
$varName
HERE
(HERE can be replaced by other word)
ex.
wc -w <<EOF
> This is a test.
> Apple juice.
> 100% fruit juice and no added sugar, colour or preservative.
> EOF
This type of redirection tells the shell to read input from the current source (HERE) until a line containg
only word (HERE) is seen.
P173
Here strings
Here strings is just like here documents and syntax is:
command <<<$word
or
command arg1 <<<"$word"
it can use for grepping into a shell variable
grep "nor" <<<$var >/dev/null && echo "Found" || echo "Not found"
below is the same meaning
echo $var | grep -q "nor" && echo "Found" || echo "Not found"
However, here strings looks more logical and easy to read.
P175
Redirect Script Errors
You can redirect script error to a log file called scripts.err:
./script.sh 2>scripts.err
P177
Appending redirected output
You can append the output to the same file using >> operator
(> will overwrite original content)
Redirection of both standard error and output
redirect both stdout and stderr to file using the following syntax:
command-name &>filename
command-name >cmd.log 2>&1
command-name >/dev/null 2>&1
P178
Writing output to files
use the redirection symbol, >, to send data to a file.
Use the >> redirection symbols, to append to a file
P180
Assigns the file descriptor (fd) to file for output
assign a file descriptor to an output file with the following syntax:
exec fd> output.txt
where, fd >= 3
ex.
exec 3> /tmp/output.txt
echo "This is a test" >&3
# Close fd # 3
exec 3<&-
P181
Assigns the file descriptor (fd) to file for input
To assign a file descriptor to an input file use the following syntax:
exec fd< input.txt
where, fd >= 3.
ex.
exec 3< /etc/resolv.conf
# Close fd # 3
exec 3<&-
P182
To closes the file descriptor use the following syntax:
exec fd<&-
To close fd # 5, enter:
exec 5<&-
Opening the file descriptors for reading and writing
to open file for both reading and writing on file descriptor:
exec fd<>fileName
File descriptor 0 is used if fd is not specified.
If the file does not exist, it is created.
This syntax is useful to update file.
FILENAME="/tmp/out.txt"
exec 3<>$FILENAME
echo "Today is $(date)" >&3
# close fd # 3
exec 3>&-
P183
Reads from the file descriptor (fd)
read -u fd var1 var2 ... varN
while IFS= read -u fd -r line
do
command1 on $line
command2 on $line
done
P186
Executes commands and send output to the file descriptor (fd)
to run or execute commands and send output to the file descriptor:
command-name >& fd
#!/bin/bash
exec 4> /tmp/out.txt
free -m >&4
(did not understand this part too much)
---------- CH7 Pipes and Filters----------
Under bash you can create a sequence of one or more commands separated by one of the following operators:
; Separates commands that are executed in sequence.
date ; pwd
& The shell executes the command in the background in a subshell.
The shell does not wait for the command to finish, and the return status is 0.
The & operator runs the command in background while freeing up your terminal for other work.
find / -iname "*.pdf" >/tmp/output.txt &
&& command2 is executed if, and only if, command1 returns an exit status of zero
|| command2 is executed if and only if command1 returns a non-zero exit status
| Linux shell pipes join the standard output of command1 to the standard input of command2.
P195 Putting jobs in background
You can use the following command to control the job:
fg - Place job in the foreground.
bg - Place job in the background.
jobs - Lists the active jobs on screen.
P196 How do I put commands in background?
command &
command arg1 arg2 &
command1 | command2 arg1 &
command1 | command2 arg1 > output &
The command which runs in background is called a job.
P197 Pipes
A shell pipe is a way to connect the output of one program to the input of another program without any temporary
file.
command1 | command2 | commandN
format_data_command > output.data.file
The vertical bar (|) is the pipe symbol.
The data path only works in one direction: command1 output -> commande2 input
(there are lots of useful example here)
P200
Input redirection in pipes
command1 < input.txt | command2
P203
Commonly used filter commands
awk
cut
grep
gzip
head
paste
perl
sed
sort
split
strings
tac
tail
tee
tr
uniq
wc
---------- CH8 Traps----------
P205 Signals
A signal is nothing but some sort of inter-process communication
(techniques for the exchanging data among multiple threads in one or more processes or commands)
A signal is sent to a process or command in order notify an event that occurred
For example, while running a command called 'ls -R /, you may hit CTRL+C (or Break) to cancel command
execution. As soon as you hit CTRL+C, a signals called SIGINT (2) sent to indicate interrupt from keyboard.
When, SIGINT is sent to ls command, Linux interrupts the process's normal flow of execution. In this example, ls
command get terminated.
You need to use the trap command to catch signals and handle errors under Linux shell scripts.
P206 Parent and Child Processes
A parent process is a Linux process that has created one or more child processes.
A process can fork a child i.e create a child process.
In UNIX, every process is created using fork and exec method. However, this model results in a waste of system
resources.
Under Linux, the fork method is implemented using copy-on-write pages, so the only penalty that it incurs is the
time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.
P207 Process States
The status of the process which can be one of the following:
1. D (uninterruptible sleep)
Process is sleeping and cannot be bring back until an event such as I/O occurred.
For example, process foo is a process waiting for keyboard interrupt.
2. R (running)
Process is running or executing.
3. S (sleeping)
Process is not running and is waiting for an event or a signal.
4. T (traced or stopped)
Process is stopped by signals such as SIGINT or SIGSTOP.
5. Z (zombie or defunct)
Processes marked <defunct> are dead processes (so-called "zombies") that remain because their parent has not
destroyed them properly. These processes will be destroyed by init if the parent process exits.
P211 Sending signal to Processes
kill - send a signal to a process
Particularly useful signals include:
1. SIGHUP (1) - Hangup detected on controlling terminal or death of controlling process.
2. SIGINT (2) - Interrupt from keyboard.
3. SIGKILL (9) - Kill signal i.e. kill running process.
4. SIGSTOP (19) - Stop process.
5. SIGCONT (18) - Continue process if stopped.
To send a kill signal to PID # 1234 use:
kill -9 1234
kill -KILL 1234
kill -SIGKILL 1234
killall - kill processes by name
If no signal name is specified, SIGTERM is sent.
killall processName
killall firefox-bin
killall -s SIGKILL firefox-bin
pkill - kill process
The pkill command is another command with additional options to kill process by its name, user name, group name,
terminal, UID, EUID, and GID
(not study)
P213 Terminating Processes
To terminate unwanted background process use kill command with -9 signal
kill -TERM pid
kill -KILL pid
To stop (suspend) a foreground process hit CTRL+Z (hold down CTRL key and press z). To resume the
foreground process use the fg command, enter:
fg jobid
fg 1
fg %
P216 Shell signal values
Some signals can never be caught. For example, the signals SIGKILL (9) and SIGSTOP (19) cannot be caught,
blocked, or ignored.
(signal table here, very important!)
P217 The trap statement
the trap command captures an interrupt.
P219 How to clear trap
P221 Include trap statements in a script
P223 Use the trap statement to catch signals and handle errors
(not study trap related topic)
P227
What is a Subshell?
Use exec command to avoid subshell
The . (dot) Command and Subshell
Compound command
Exec command
(not study)
---------- CH9 Functions----------
P232
funcion() {......}
One line functions inside { ...; } must end with a semicolon
P233 Displaying functions
declare -f funciton_name
Removing functions
unset -f functionName
P235 Defining functions
function name {
.....
.....
}
or
function name() {
.....
.....
}
P238
Writing functions
All shell functions are treated as a command.
You must define a function at the start of a script.
A function must be created before calling.
P240
a full sample case
P244 Pass arguments into a function
Shell functions have their own command line argument.
Use variable $1, $2..$n to access argument passed to the function.
The syntax is as follows:
name() {
arg1=$1
arg2=$2
command on $arg1
}
To invoke the the function use the following syntax:
name foo bar
Where,
1. name = function name.
2. foo = Argument # 1 passed to the function (positional parameter # 1).
3. bar = Argument # 2 passed to the function.
Function shell variables
‧ All function parameters or arguments can be accessed via $1, $2, $3,..., $N.
‧ $0 always point to the shell script name.
‧ $* or $@ holds all parameters or arguments passed to the function.
‧ $# holds the number of positional parameters passed to the function.
P247 Local variable
By default all variables are global.
Modifying a variable in a function changes it in the whole script.
You can create a local variables using the local command and syntax is:
local var=value
local varName
or
function name(){
local var=$1
command1 on $var
}
P250
Returning from a function
P255
Source command
P256
Recursive function
Recursive functions are slow under bash.
Avoid using recursive functions if possible.
P258
Putting functions in background
沒有留言:
張貼留言