《Linux程序设计第四版》读书笔记 - 第二章 Shell程序设计

本文介绍了Shell脚本的基础知识,包括文件描述符、重定向、管道、变量、命令读取、引用、参数变量、条件判断、循环结构等核心概念。通过实际示例详细解释了如何在Shell脚本中进行流程控制和数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. Pipe

ps | 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 "" when contains spaces;
.There CANNOT be any spaces on either side of the equals sign "=";
.Variable is case-sensitive.

6. "read" command
read key: read user input as value

7. quote "" and ''
"$key" 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 "["
-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 "man test"

10. echo -n "string": remove trailing new line

11. "if" statement
if [conditions]; then
    statements
elif [conditions]; then
   statements
else
   statements
fi

12. "for" statement
for variable in values; do
   statements
done

13. "while" statement
while [condition]; do
   statements
done

14. "until" statement
until [condition]; do
   statements
done

15. "case" 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 "break"
use break for escaping from an enclosing for, while, or until loop before the controlling condition has
been met.

19. command ":"
alias for "true": while(true) <=> while :

20. command "continue"
makes the enclosing for, while, or until loop continue at the next iteration.

21. command "."
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 "echo"
for example: echo -n "string to output", -n means don't output the trailing newline

23. command "eval"
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 "exec"
replace the current shell with a different program.

25. command "exit n"
n is 0~125, 0 is success.

26. command "export"
makes the variable named as its parameter available in subshells.

27. command "expr"
evaluates its arguments as an expression. It’s most commonly used for simple arith-metic.
x=$(expr $x + 1)
echo $(1+2) => 3


28. command "printf"
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 "set"
sets the parameter variables for the shell.
set $(date)
echo The month is $2

30. command "shift"
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 "trap"
specify the actions to take on receipt of signals, "trap command signal"
INT (2) => Interrupt, Ctrl+C
trap ‘rm -f test.txt’ INT  => when INT signal received, delete test.txt file.

 32. command "unset"
removes variables or functions from the environment.

33. command "find"
find [path] [options] [tests] [actions]
for example: 
find . -name "*.o" -exec ls -l {} \; => find all files named as "*.o" in current directory, if found then execute "ls -l" for the result set.

34. command "grep"
You use "find" to search your system for files, but you use "grep" to search files for strings. 
grep [options] PATTERN [FILES]
for example:
grep -c in *.c => list "in" count in every *.c files
find . -name "*" | xargs grep "include" => find all files which contain string "include" 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值