美文网首页
shell入门

shell入门

作者: iDevOps | 来源:发表于2019-10-11 11:21 被阅读0次
认识Shell

Shell俗称壳,它提供了用户与内核进行交互操作的一种接口,它接收用户输入的命令并把它送入内核去执行

  • 命令分类
    内部命令: 在系统启动时就调入内存,是常驻内存的,所以执行效率高
    外部命令: 是系统软件的功能,用户需要时才从硬盘中读入内存
    如何区分内外部命令?
type 命令
  • "#"和"$"的区别
    "#"表示root用户登陆,S表示普通用户登陆
  • shell类型
# 当前shell,具体用户使用的哪种shell,可以查看/etc/passwd文件
echo $SEHLL
输出: 
/bin/bash

# 可用shell
cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/shclear
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
  • 快捷键
Ctrl + A  光标移动到首部
Ctrl + E  光标移动到尾部
Ctrl + C  终止前台运行的程序
Ctrl + D  退出当前shell
Ctrl + Z  将任务暂停,挂至后台
Ctrl + L  清屏,和clear命令等效
Ctrl + K  删除光标到行末的所有字符
Ctrl + U  删除光标到行首的所有字符
Ctrl + R  搜索历史命令,利用关键字
注: 在命令行前加#,则该命令不会被执行
  • 命令执行流程
1. 判断命令是否通过绝对路径执行
2. 判断命令是否存在别名
3. 判断命令是内部命令还是外部命令
   内部命令: Shell程序自带命令
   外部命令: 在系统PATH变量的某个路径下的可执行程序
4. 内部命令直接执行,外部命令检测是否存在缓存
5. 检测PATH路径
编写一个小脚本
  • test.sh
#!/bin/bash
# this is a shell
echo "hello shell"
mkdir /home/test
touch /home/test/a.txt

注:
#!/bin/bash : 告诉脚本使用的是哪种命令解释器。如不指shell,以当前shell作为执行的shell
# this is a shell : 这是一句注释,不会执行
  • 执行脚本
# 先给脚本执行权限
[root@centos7-base shell]# chmod +x test.sh
# 执行脚本,会输出hello shell, 并且会在home目录下创建test文件夹及test/a.txt文件
[root@centos7-base shell]# ./test.sh 
hello shell

shell脚本的执行通常有以下几种方式
1、/home/shell/test.sh 或者 ./test.sh (当前路径下执行脚本的话要有执行权限chmod +x test.sh)
2、bash test.sh 或 sh test.sh (这种方式可以不对脚本文件添加执行权限)
3、source test.sh (可以没有执行权限)
4、sh < test.sh 或者 cat test.sh |sh(bash)

shell变量
  • 变量类型
    按变量的作用分为4类:
    1.用户自定义变量
    2.环境变量,这种变量中主要保存的是和系统操作环境相关的数据
    3.位置参数变量,这种变量主要是用来向脚本当中传递参数或数据的,变量名不能自定义,变量作用是固定的
    4.预定义变量,是Bash中已经定义好的变量,变量名不能自定义,变量作用也是固定的
    按照变量的作用域分为2类:
    1.全局变量,全局变量是环境变量,其值不随shell 脚本的执行结束而消失
    2.局部变量是shell 程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。包括:用户自定义变量、位置变量和预定义变量

  • 变量的定义和使用

# 定义变量a=1,等号两边不能有空格
[root@centos7-base shell]# a=1
# 使用变量需要在变量前加$
[root@centos7-base shell]# echo $a
1
# 变量拼接
[root@centos7-base shell]# name=test
[root@centos7-base shell]# echo $name-db.log
test-db.log
[root@centos7-base shell]# echo $name.db.log
test.db.log
[root@centos7-base shell]# echo $namedb.log  # 这里不行,需要用${},$name是对${name}的简化版本
.log
[root@centos7-base shell]# echo ${name}db.log
testdb.log
  • 使用$()或反引号替换命令
[root@centos7-base shell]# date
2019年 10月 11日 星期五 10:18:28 CST
[root@centos7-base shell]# echo `date`
2019年 10月 11日 星期五 10:18:32 CST
[root@centos7-base shell]# echo $(date)
2019年 10月 11日 星期五 10:18:42 CST
  • 命令嵌套使用
[root@centos7-base shell]# test=$(tar zcvf test.tar $(find /home/ -name *.sh))
tar: 从成员名中删除开头的“/”
  • 单引号和双引号的区别
[root@centos7-base shell]# name=sn
[root@centos7-base shell]# echo '$name'
$name
[root@centos7-base shell]# echo "$name"
sn
  • 删除变量
[root@centos7-base shell]# unset name
[root@centos7-base shell]# echo "$name"
  • 环境变量
    在bash shell中,环境变量分为全局变量和局部变量
    全局变量对shell和所有的子shell都有效
# 查看所有的全局变量
[root@centos7-base shell]# env
XDG_SESSION_ID=4
HOSTNAME=centos7-base
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
省略.....

# 查看某一个全局变量
[root@centos7-base shell]# env | grep PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
使用export把局部变量编程全局变量,只在当前连接生效
[root@centos7-base shell]# export name=sn
[root@centos7-base shell]# echo $name
sn
[root@centos7-base shell]# env
XDG_SESSION_ID=4
HOSTNAME=centos7-base
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.5.1 50116 22
SELINUX_USE_CURRENT_RANGE=
OLDPWD=/home
SSH_TTY=/dev/pts/0
name=sn
USER=root
省略......
让变量永久生效

当登录系统或新开启一个ssh连接启动bash进程时,一定会加载这4个配置文件
/etc/profile    #系统全局环境和登录系统的一些配置
/etc/bashrc    #shell全局自义配置文件,用于自定义shell
/root/.bashrc   #用于单独自定义某个用户的bash
/root/.bash_profile  #用户单独自定义某个用户的系统环境

这四个文件的加载顺序?
/etc/profile
/etc/bashrc
/root/.bashrc
/root/.bash_profile

插入永久变量
vim /etc/profile
export name=sn
source /etc/profile
  • shell位置变量
Shell解释执行用户的命令时,将命令行的第一个字符作为命令名,而其它字符作为参数
$0  获取当前执行shell脚本的文件文件名,包括脚本路径,命令本身
$n  获取当前脚本的第n个参数 n=1,2.....n 当n大于9时 用${10}表示
#!/bin/bash
echo "本shell脚本的文件名: $0"
echo "第1个参数:  $1"
echo "第2个参数:  $2"
echo "第3个参数:  $3"
echo "第4个参数:  $4"
[root@centos7-base shell]# ./test.sh a b c d
本shell脚本的文件名: ./test.sh
第1个参数:  a
第2个参数:  b
第3个参数:  c
第4个参数:  d
  • 特殊变量
#!/bin/bash
echo "$*"
echo "$#"
echo "$$"
echo "$!"
echo "$$"
echo "$?"

[root@centos7-base shell]# ./test1.sh a b c d
a b c d
4
4273

4273
0

注:
$* :  表示这个程序的所有参数
$# : 表示这个程序的参数个数
$$ : 表示程序的进程ID
$! : 执行上一个后台指令的PID
$$ : 表示程序的进程ID
$? : 表示上一个程序执行返回结果
shell数学运算
  • expr
# 对数字的基本计算,做比较时,输出结果假为0,1为真;特殊符号用转义符
[root@centos7-base shell]# expr 2 \> 5
0
[root@centos7-base shell]# expr 2 \> 1
1
[root@centos7-base shell]# expr 2 \* 3
6
[root@centos7-base shell]# expr 2 \+ 3
5

# 对字符串操作
[root@centos7-base shell]# expr length "hello"
5
[root@centos7-base shell]# expr substr "hello" 2 3
ell
  • $(( ))
格式:$((表达式1,表达2))
特点:
    1、在双括号结构中,所有表达式可以像c语言一样,如:a++,b--等。a++  等价于 a=a+1 
    2、在双括号结构中,所有变量可以不加入:“$”符号前缀。
    3、双括号可以进行逻辑运算,四则运算
    4、双括号结构 扩展了for,while,if条件测试运算
    5、支持多个表达式运算,各个表达式之间用“,”分开
[root@centos7-base shell]# a=$((1+1))
[root@centos7-base shell]# echo $a
2
[root@centos7-base shell]# echo $((a++))
2
[root@centos7-base shell]# echo $((++a))
4

相关文章

网友评论

      本文标题:shell入门

      本文链接:https://www.haomeiwen.com/subject/nmeoectx.html