设为首页 收藏本站
查看: 406|回复: 0

[经验分享] 《Linux程序设计第四版》读书笔记

[复制链接]
累计签到:4 天
连续签到:1 天
发表于 2015-10-26 07:20:22 | 显示全部楼层 |阅读模式
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 [conditions]; then
    statements
elif [conditions]; then
   statements
else
   statements
fi
12. &quot;for&quot; statement
for variable in values; do
   statements
done
13. &quot;while&quot; statement
while [condition]; do
   statements
done
14. &quot;until&quot; statement
until [condition]; 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 [path] [options] [tests] [actions]
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 [options] PATTERN [FILES]
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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-130716-1-1.html 上篇帖子: Linux 下篇帖子: Linux命令exit
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表