Shell 学习笔记 (org-mode制作)
2015-07-23 22:32
525 查看
S shell learning note v1.0
Table of Contents
1. what is shell2. hello world program
2.1. src
2.2. add run privilege
2.3. run shell
3. variables in shell
3.1. system variables
3.2. user defined variables
3.2.1. define and use
3.2.2. local vs global variable
4. control structure
4.1. condition judgement
4.1.1. math operation judgement
4.1.2. string comparation
4.1.3. logic judgement
4.1.4. if-else
4.1.5. for
4.1.6. while
4.1.7. case
5. fucntion
6. debug
6.1. echo
6.2. sh -v ./shell or sh -x ./shell
1 what is shell
shell is the middle man of kernel and user.to show shell installed in the system. different shell may has differnet grammar
cat /etc/shells
show current shell used.
echo $SHELL
2 hello world program
2.1 src
#!/bin/sh echo "hello shell"
2.2 add run privilege
$ sudo chmod +x ./helloshell.sh
2.3 run shell
./helloshell.sh
3 variables in shell
variables in shell are system variables and user defined variables.3.1 system variables
use$ set
to show the system variables.
3.2 user defined variables
3.2.1 define and use
variables in shell have no data type. shell will automatically transfer the type. e.g.$ msg="hello world"
Attention: there is no blank spaces before and after '='
3.2.2 local vs global variable
global shell variable stored in ~/.bashrc, otherwise, they are all local shell variables.4 control structure
4.1 condition judgement
use test or [] to judge a condition in shell.4.1.1 math operation judgement
seq | example | explain |
---|---|---|
1 | a -eq b | a == b |
2 | a -ne b | a != b |
3 | a -lt b | a < b |
4 | a -le b | a <= b |
5 | a -gt b | a > b |
6 | a -ge b | a >= b |
4.1.2 string comparation
seq | example | explain |
---|---|---|
1 | str1 = str2 | str1 == str2 |
2 | str1 != str2 | str1 != str2 |
3 | str1 | str1 not null or not defined. |
4.1.3 logic judgement
seq | example | explain |
---|---|---|
1 | ! | not |
2 | exp1 -a exp2 | exp1 && exp2 |
3 | exp1 -o exp2 | exp1 or exp2 |
4.1.4 if-else
#!/bin/sh if [ $# -ne 1 ]; then echo "$0 : you must give one integer" exit 1 fi if [ $1 -gt 0 ]; then echo "$1 is positive" else echo "$1 is negative" fi
4.1.5 for
#!/bin/sh for i in 1 2 3 4 5 do echo "welcome $i" done
4.1.6 while
#!/bin/sh # test while loop if [ $# -ne 1 ]; then echo "usage: $0 integer" exit 1 fi n=$1 i=0 while [ $i -le 10 ] do echo "$n * $i = `expr $i \* $n`" i=`expr $i + 1` done
4.1.7 case
#!/bin/sh # test case statement if [ $# -ne 1 ]; then echo "usage: $0 [a|b|c]" exit 1 fi case $1 in "a" ) echo "a select" ;; "b" ) echo "b select" ;; "c" ) echo "c select" ;; * ) echo "no action" ;; esac
5 fucntion
#!/bin/sh demo() { echo "all function args: $*" echo "the first arg : $1" echo "the second arg : $2" echo "the third arg : $3" } # call the function demo -f foo bar exit 0
6 debug
6.1 echo
use echo to info you.6.2 sh -v ./shell or sh -x ./shell
$ sh -v ./shell print the content shell get$ sh -x ./shell show the command then print it.
Created: 2015-07-23 Thu 22:20
Emacs 24.4.1 (Org mode 8.3beta)
Validate
相关文章推荐
- shell脚本:shell的基本元素-2 变量
- shell学习
- Mac OS X 执行shell以及bash shell的区别。
- 【BASH】自己主动清理rman脚本备份文件
- 功能强大的Windows PowerShell
- Scrapy Shell访问jandan被拒解决方法
- [linux][shell]控制进程不定时重启
- Java调用Shell命令的方法
- shell exit 0 exit 1
- 使用终端shell命令批量修改一个文件下的所有文件的读写权限
- 父 shell,子 shell ,export 与 变量传递
- 父 shell,子 shell ,export 与 变量传递
- SHELL入门教程(7)-编写脚本
- SHELL入门教程(6)-环境
- SHELL入门教程(5)-算术运算
- 使用第三方工具连接linux CentOS 6.5 7,如Xshell连接linux虚拟机很慢
- shell脚本---web监控脚本系列
- shell——for&while&select
- 《转》couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
- 关于XShell上登录跳板机问题及第七天实习记录