Shell理论学习(一)
1.登录主机:[*] 本机登录(7个接口tty1~tty7)
[*] 文本接口(tty1~tty6)
[*] 图形接口(tty7)
[*] 远程登录
2.通配符:
[*] *:代表任意的字符串,可以是空字符串
[*] ?:代表一个字符,但是不可以为空
3.转义字符:\
4.续行符号:\
5.字符集合:
[*] :英文小写字母
[*] :英文大写字母
[*] :英文大小写字母
[*] :数字
[*] :英数字
[*] :x,y或是z
6.括号扩展:{}
{g,nc,s}ftp即:gftp,ncftp,sftp
7.检查script的语法:bash -v test.sh
8.查看script的程序代码:bash -n test.sh
9.追踪script的执行:bash -x test.sh
10.父shell和子shell:
在执行shell script之前,我们身处在一个login shell中,称为父shell.当我们执行某一个shell script时,父shell会根据script程序的第一行#!之后所指定的shell程序开启(此操作称为fork)一个子shell中的script执行完毕,此子shell随即结束,仍然回到父shell中,不会影响父shell原本的环境.
子shell和父shell一样,会开启三个文件:标准输入(键盘),标准输出(屏幕),标准错误(屏幕),同时,子shell会继承父shell的若干变量值的内容,这些变量称为环境变量
11.子shell再开启子shell:
查看当前位于在几层shell中:
echo $SHLVLOR ps axf
12.系统默认开启的文件
标准输入:0
标准输出:1
标准错误:2
13.转向输入和转向输出合用
sort < unsort.txt > sorted.txt
14.登陆(login)
登陆主机时,login shell先执行/etc/profile,接着检查用户的主目录中,是否有.bash_profile或者是否有.profile。若有,则会读取并执行其中一个文件,执行顺序为:
.bash_profile最优先 --> .bash_login其次 --> .profile最后
15.注销(logout)
注销主机时,bash检查主目录中是否有.bash_logout。若有,则读取并执行它.
16.执行新shell
执行新shell(非login shell),可分为两种情况
[*] 执行交互式的shell:例如直接执行bash.产生一个子shell.此时,bash会读取并执行/etc/bash.bashrc,以及主目录中的.bashrc
[*] 执行shell script(即非交互式):例如,执行script文件test.sh,它会检查BASH_ENV变量的内容,若该变量有定义,则执行该变量所定义的启动文件的内容.
假设BASH_ENV的内容指向bash_env.sh如下所示:
export BASH_ENV="/root/tmp/bash_env.sh"
17.printf:依照格式显示参数内容
一个格式的字符对应到一个参数的输出.如果想输出变量值,最好用双引号包括住.假设变量str="Hello world"
用法:
# str="Hello World"
# printf "%-5s\n" "$str"
Hello World
# printf "%20s\n" "$str"
Hello World
18.关于history的三个参数
19.列出登陆主机后,最近执行过的命令
# fc -l
78 /etc/init.d/rabbitmq-server restart
79 /etc/init.d/rabbitmq-server status
80 init 3
81 str="Hello World"
82 printf "%-5s" "$str"
83 printf "%-5s\n" "$str"
84 printf "%5s\n" "$str"
85 printf "%20s\n" "$str"
86 str2="China"
87 printf "%-5s\n,%10s\n" "$str,$str2"
88 printf "%-5s\n,%10s" "$str,$str2"
89 printf "%-5s\n,%20s" "$str,$str2"
90 printf "%-5s\n%20s" "$str$str2"
91 echo $HISTFILESIZE
92 echo $HISTSIZE
93 echo $HISTFILE
20.time在script或指令执行结束后,显示real,user,cpu3种耗用时间的统计
# time yes | cp -rvf test/* /opt/test2/
cp: overwrite `/opt/test2/123.txt'? `test/123.txt' -> `/opt/test2/123.txt'
cp: overwrite `/opt/test2/1.oxo'? `test/1.oxo' -> `/opt/test2/1.oxo'
cp: overwrite `/opt/test2/2.oxo'? `test/2.oxo' -> `/opt/test2/2.oxo'
cp: overwrite `/opt/test2/3.oxo'? `test/3.oxo' -> `/opt/test2/3.oxo'
cp: overwrite `/opt/test2/4.oxo'? `test/4.oxo' -> `/opt/test2/4.oxo'
cp: overwrite `/opt/test2/5.oxo'? `test/5.oxo' -> `/opt/test2/5.oxo'
real 0m0.008s
user 0m0.000s
sys 0m0.008s
21.read
#!/bin/bash
read -p "Please Input Your name:"
echo "you name is $REPLY"
#读入数组:
read -a arr/root/test
# ln -svf /tmp/link_file
`./link_file' -> `/tmp/link_file'
25.basename:取得路径名称中最后的文件名部分
# basename /tmp/link_file/123.txt
123.txt
26.sort -r(降序) -n(以数值大小为标准) -t(定义分隔符) -k(定义字段)
# sort -nr -t: -k3 /etc/passwd 27.cut -d(分隔符) -f3,4(截取第三行,第四行)
# cut -d: -f3,4 /etc/passwd
0:0
1:1
2:2
3:4
28.dirname:取得文件的路径名
# dirname /opt/test2/123.txt
/opt/test2
29.tr转换字符或删除字符
# cat 123.txt
this is a test!
# tr i I < 123.txt
thIs Is a test!
页:
[1]