夏末秋初 发表于 2015-10-26 07:20:22

《Linux程序设计第四版》读书笔记

1. File descriptor
input 0
output 1
error output 2

2. Redirect
Redirect output: ls -l > lsoutput.txt
Redirect output(append): ps >> lsoutput.txt
Redirect error output: kill 2325 >killout.txt 2>killerr.txt
Redirect discard output: kill 4444 > /dev/null, which is a "bit bucket"
Combine redirect outputs: kill 2333 >killout.txt 2>&1
Redirect input: more < 1.txt

3. Pipeps | sort | more
4. Shell
#: Comments
#!/bin/sh: The program following #! responsible for executing the script file
5. Variables key=value
.Value must be delimited by quote &quot;&quot; when contains spaces;
.There CANNOT be any spaces on either side of the equals sign &quot;=&quot;;
.Variable is case-sensitive.
6. &quot;read&quot; command
read key: read user input as value
7. quote &quot;&quot; and ''
&quot;$key&quot; will replace $key to its value;
'$key' will not trigger substitution.
8. Parameter Variables
$0 The script name
$1, $2, ... The params given to the script
$* A list of all the parameters, divided by first char in $IFS
$@ Similar with $*, but will not run params together when $IFS is empty
9. test command or &quot;[&quot;
-n string: True if the string is not null
-z string: True if the string is null (an empty string)
-d file: True if the file is a directory
-f file: True if the file is a regular file
more details refer to &quot;man test&quot;
10. echo -n &quot;string&quot;: remove trailing new line
11. &quot;if&quot; statement
if ; then
    statements
elif ; then
   statements
else
   statements
fi
12. &quot;for&quot; statement
for variable in values; do
   statements
done
13. &quot;while&quot; statement
while ; do
   statements
done
14. &quot;until&quot; statement
until ; do
   statements
done
15. &quot;case&quot; statement
case variable in
   pattern [|pattern] ...) statements;;
   pattern [|pattern] ...) statements;;
...
esac
16. AND and OR list
AND: &&
OR: ||
17. function
function_name() {
   statements
}

18. command &quot;break&quot;
use break for escaping from an enclosing for, while, or until loop before the controlling condition has
been met.
19. command &quot;:&quot;
alias for &quot;true&quot;: while(true) <=> while :
20. command &quot;continue&quot;
makes the enclosing for, while, or until loop continue at the next iteration.
21. command &quot;.&quot;
for example: . ./shell_script
script is executed in the current shell. This enables the script to change environment settings in the current shell, which remains changed even
when the script finishes executing.
22. command &quot;echo&quot;
for example: echo -n &quot;string to output&quot;, -n means don't output the trailing newline
23. command &quot;eval&quot;
eval is a bit like an extra $, it gives you the value of the value of a variable.
for example:
foo=10
y='$'foo
echo $y => $foo
eval y='$'foo
echo $y => 10
24. command &quot;exec&quot;
replace the current shell with a different program.
25. command &quot;exit n&quot;
n is 0~125, 0 is success.
26. command &quot;export&quot;
makes the variable named as its parameter available in subshells.
27. command &quot;expr&quot;
evaluates its arguments as an expression. It’s most commonly used for simple arith-metic.
x=$(expr $x &#43; 1)
echo $(1&#43;2) => 3

28. command &quot;printf&quot;
printf “format string“ parameter1 parameter2 ...
printf “%s %d\t%s” “Hi There” 15 people
conversion specifier is allowed, but floating point is NOT supported

29. command &quot;set&quot;
sets the parameter variables for the shell.
set $(date)
echo The month is $2

30. command &quot;shift&quot;
moves all the parameter variables down by one, so that $2 becomes $1, $3 becomes
$2, and so on. The previous value of $1 is discarded, while $0 remains unchanged.
while [ “$1” != “” ]; do
echo “$1”
shift
done

31. command &quot;trap&quot;
specify the actions to take on receipt of signals, &quot;trap command signal&quot;
INT (2) => Interrupt, Ctrl&#43;C
trap ‘rm -f test.txt’ INT=> when INT signal received, delete test.txt file.

32. command &quot;unset&quot;
removes variables or functions from the environment.

33. command &quot;find&quot;
find
for example:
find . -name &quot;*.o&quot; -exec ls -l {} \; => find all files named as &quot;*.o&quot; in current directory, if found then execute &quot;ls -l&quot; for the result set.

34. command &quot;grep&quot;
You use &quot;find&quot; to search your system for files, but you use &quot;grep&quot; to search files for strings.
grep PATTERN
for example:
grep -c in *.c => list &quot;in&quot; count in every *.c files
find . -name &quot;*&quot; | xargs grep &quot;include&quot; => find all files which contain string &quot;include&quot; and list the lines.

35. parameter substitutions
${param:-default} If param is null, then set it to the value of default.
${#param} Gives the length of param
${param%word} From the end, removes the smallest part of param that matches word and returns the rest
${param%%word} From the end, removes the longest part of param that matches word and returns the rest
${param#word} From the beginning, removes the smallest part of param that matches word and returns the rest
${param##word} From the beginning, removes the longest part of param that matches word and returns the rest

36. here document
more commonly used for outputting large amounts of text from inside a script, as you saw previously, and avoiding having to use echo statements for each line.


         版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: 《Linux程序设计第四版》读书笔记