shell 非_Shell基本操作(一)

linux
• 内部命令和外部命令
• Shell不需要启动一个单独的进程来运行内部命令
• Shell需要创建(fork)和执行(exec)一个新的子进程来运行外部命令
• 尽量使用内部命令有助于性能的提升
bash-->cd --解析内部命令的流程
bash-->forks-->bash(子进程)-->ls --解析外部命令的流程
# type ls --外部命令
ls is aliased to `ls --color=tty'
# type cd --内部命令
cd is a shell builtin
1、文件名
• 你可以用任何你认为合适的可见字符或不可见字符为文件或目录命名,但不要给自己找麻烦
• 从安全和方便角度
• 大小写字符
• 下划线
• 圆点
• 逗号
• 尽量不要出现“空格”
• 扩展名
• 方便分类,非必须
• 命令通过文件内结构判断其类型(file)
-type c
File is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link; this is never true if the -L option or the -fol-
low option is in effect, unless the symbolic link is broken.
If you want to search for symbolic links when -L is in effect,
use -xtype.
s socket
D door (Solaris)

linux
2、通配符
• ? 任意单个字符,不能匹配空
• * 任意零个或多个字符组(不能匹配以点开头的文件)
• [ab] a或者b
• [a-z] a到z之间的任意字符,包括端点在内
• 是Shell 而非命令本身处理通配符,命令后的通配符会在命令执行前就被代换了
• 如果需要命令而非Shell处理通配符,请用“"将通配符转义,跳脱字符
# ll /dev/sd?
brw-r----- 1 root disk 8, 0 03-14 09:03 /dev/sda
# ll /dev/sd*
-rw-r--r-- 1 root root 0 03-14 11:30 /dev/sd
brw-r----- 1 root disk 8, 0 03-14 09:03 /dev/sda
brw-r----- 1 root disk 8, 1 03-14 09:03 /dev/sda1
brw-r----- 1 root disk 8, 2 03-14 09:03 /dev/sda2
brw-r----- 1 root disk 8, 3 03-14 09:03 /dev/sda3
brw-r----- 1 root disk 8, 4 03-14 09:03 /dev/sda4
brw-r----- 1 root disk 8, 5 03-14 09:03 /dev/sda5
# ll /dev/sd?[1-5]
brw-r----- 1 root disk 8, 1 03-14 09:03 /dev/sda1
brw-r----- 1 root disk 8, 2 03-14 09:03 /dev/sda2
brw-r----- 1 root disk 8, 3 03-14 09:03 /dev/sda3
brw-r----- 1 root disk 8, 4 03-14 09:03 /dev/sda4
brw-r----- 1 root disk 8, 5 03-14 09:03 /dev/sda5
# ll /dev/tty[1-5][0-9]
# ll /dev/sda[13]
brw-r----- 1 root disk 8, 1 03-14 09:03 /dev/sda1
brw-r----- 1 root disk 8, 3 03-14 09:03 /dev/sda3
# ll /dev/sda[1-3]
brw-r----- 1 root disk 8, 1 03-14 09:03 /dev/sda1
brw-r----- 1 root disk 8, 2 03-14 09:03 /dev/sda2
brw-r----- 1 root disk 8, 3 03-14 09:03 /dev/sda3
3、转义
# touch test*?[].sh
4、跳脱字符
# ls
> /root
> a
> /b
5、bash中的引号
双引号 “ ” :允许通过$符号引用其他变量值,会把引号的内容当成整体来看待
单引号 ‘ ’ :禁止引用其他变量值,shell中特殊符号都被视为普通字符,会把引号的内容当成整体来看待
6、反撇号 ``
$() 在执行命令的过程中会优先执行
; 可对一行命令进行分割,在执行过程中不考虑上一个命令执行是否是正确的
&& 可对一行命令进行分割,在执行过程中考虑上一个命令执行是否是正确的
||
7、! 命令历史
# echo "This system is "HOSTNAME""
This is system is HOSTNAME
# echo "This system is "$HISTNAME""
This is system is
# echo "This system is "$HOSTNAME""
This is system is desktop8.example.com
# echo 'This system is "$HOSTNAME"'
This is system is "$HOSTNAME"
[root@desktop8 ~]# echo $5.00
.00
[root@desktop8 ~]# echo $5.00
$5.00
# host=`ifconfig eth0 |grep 'inet addr'|cut -d : -f2|cut -d ' ' -f1`
# host2=$(ifconfig eth0 |grep 'inet addr'|cut -d : -f2|cut -d ' ' -f1)
8、运算符
$[]
# a=$((1 + 10))
# echo $a
# a=$[1 + 100]
# echo $a
9、shell的配置文件(软件+配置文件)
• csh
• $HOME/.cshrc
• 登录时执行$HOME/.login
• 登出时执行$HOME/.logout
• bash
用户加载shell配置流程:
# user01-->login-->bash-->/etc/profile-->$HOME/.bash_profile-->$HOME/.bashrc-->/etc/bashrc
全局配置文件:
/etc/profile --bash工作环境的配置(环境变量)
/etc/profile.d/*.sh --/etc/profile的扩展配置文件
/etc/bashrc --bash的配置文件
针对每个用户的配置文件:
$HOME/.bash_history--存放命令历史
$HOME/.bash_logout--注销/退出shell的时候执行的脚本
$HOME/.bash_profile
$HOME/.bashrc
10、命令的编辑
ctrl + u --删除当前光标至行首内容
ctrl + k --删除当前光标至行尾内容
ctrl + c --中断
ctrl + l --清屏
ctrl + a --跳到行首HOME
crtl + e --路到行尾END
ctrl + r --快速搜索history命令
ctrl + z --转入后台运行 fg bg
Ctrl + d --退出shell,logout
↑(Ctrl+p) 显示上一条命令
↓(Ctrl+n) 显示下一条命令
alias--查询系统中所有已经存在的别名
alias 别名=‘真名’
unalias--取消系统中的别名
unalias 别名
unalias -a删除所有的别名
临时:
alias la='ls -a'
固定:
可以写至以下文件,定义完成需要使用source来刷新,或者注销重新登录用户:
/etc/profile
$HOME/.bash_profile
$HOME/.bashrc
/etc/bashrc
/etc/profile.d/*.sh
11、命令历史
HISTSIZE=1000设置命令历史的条数
history 查询当前用户用过的所有命令历史(内存)
cat $/HOME/.bash_history | grep xx(硬盘)
history -w 同步内存中的命令至硬盘($/HOME/.bash_history)
自动同步:exit/注销
history -c
echo "" > $HOME/bash_history--清空命令历史
# :> $HOME/bash_history
调用命令历史:
!101通过编号
!!调用最后一条命令历史
!vim调用离我最近一条以vim开头的命令历史
!$调用最后一条命令历史中的参数
命令字 + [选项] + 参数
ls -l /
ctrl + r查找命令历史
12、bash的特殊符号
< << > >> &> |
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
