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

Linux学习笔记 第十一课 shell基础知识

[复制链接]

尚未签到

发表于 2018-8-29 10:19:13 | 显示全部楼层 |阅读模式
  一、shell特性
  1、history !! !$ !n !str
  2、tab 命令补全
  3、alias 和 unalias
  4、通配符 * ?  [ ]
  5、输入输出重定向 >, >>, >, &>,&>>
  二、变量
  1、系统变量 set  env export
  2、变量命名规则
  3、取消变量  unset
  4、变量引用:``  ' '  " "
  三、系统和个人环境变量的配置文件
  
  四、shell中的特殊字符 * ? # \ | $ ; &  && ||
  五、常用命令  cut,sort,wc,uniq, tree ,tr,split
  
  一、shell特性
  1、history  命令历史 (history 10  -c !! !$ !n !vi)
  history 10  只列出最近10条记录
  history -c   清空历史命令
  !!   执行上一条命令
  !$  上一条命令的最后一个参数
  !n     第多少条命令
  !vi     最近一条以vi开头的命令
  2、tab 命令补全
  3、alias  vi=vim    取消:unalias myssh
  永久生效,修改/root/.bashrc
  myssh='ssh -i /root/.ssh/1.txt'
  4、*通配所有字符   ?一个任意字符    [  ]之间的一个字符
  ***************************
  [root@localhost tmp]# touch 1.txt 2.txt 111.txt 222.txt
  [root@localhost tmp]# ls *
  111.txt  1.txt  222.txt  2.txt
  [root@localhost tmp]# ls 1?.txt
  ls: 无法访问1?.txt: 没有那个文件或目录
  [root@localhost tmp]# ls ?.txt
  1.txt  2.txt
  [root@localhost tmp]# ls [12].txt
  1.txt  2.txt
  [root@localhost tmp]# ls [12]*.txt
  111.txt  1.txt  222.txt  2.txt
  [root@localhost tmp]# ls [12]?.txt
  ls: 无法访问[12]?.txt: 没有那个文件或目录
  [root@localhost tmp]# ls [12]??.txt
  111.txt  222.txt
  [root@localhost tmp]# ls [1-9].txt
  1.txt  2.txt
  ***********************************
  5、输入输出重定向
  > ,>>,,2>> ,&>,&>>
  &>1.txt   正确和错误重定向到1.txt
  &>>1.txt   正确和错误追加到1.txt
  ***********************************
  [root@localhost tmp]# cat 1.txt
  [root@localhost tmp]# echo 111 >1.txt
  [root@localhost tmp]# cat  1.txt ;done
  [root@localhost tmp]# ls /root
  1.sh   2015-03-24.log  3.sh  5.sh  7.sh  anaconda-ks.cfg      install.log         iptables        php-5.3.27.tar.gz  shift.sh
  1.txt  2.sh            4.sh  6.sh  8.sh  httpd-2.2.16.tar.gz  install.log.syslog  LinuxICv35.iso  sdb1               sh.zip
  [root@localhost tmp]# ls /tmp
  111.txt  1.txt  222.txt  2.txt  a0  a1  a2  a3  a4  a5  a6  a7
  [root@localhost tmp]# ls /tmp | xargs  // 将回车和制表符 转成空格符
  111.txt 1.txt 222.txt 2.txt a0 a1 a2 a3 a4 a5 a6 a7
  [root@localhost tmp]# ls
  111.txt  1.txt  222.txt  2.txt  a0  a1  a2  a3  a4  a5  a6  a7
  [root@localhost tmp]# ls a* | xargs -i mv {} {}.txt     // 将an文件重名为an.txt
  [root@localhost tmp]# ls
  111.txt  1.txt  222.txt  2.txt  a0.txt  a1.txt  a2.txt  a3.txt  a4.txt  a5.txt  a6.txt  a7.txt
  *****************************
  5、sleep 100
  sleep 200
  sleep 300
  jobs
  fg
  bg
  ctrl + z  暂停
  ctrl + c 终止
  ********************************
  [root@localhost tmp]# sleep 100
  ^Z
  [1]+  Stopped                 sleep 100
  [root@localhost tmp]# sleep 200 &
  [2] 20568
  [root@localhost tmp]# sleep 300 &
  [3] 20569
  [root@localhost tmp]# jobs
  [1]+  Stopped                 sleep 100
  [2]   Running                 sleep 200 &
  [3]-  Running                 sleep 300 &
  [root@localhost tmp]# fg
  sleep 100
  ^Z
  [1]+  Stopped                 sleep 100
  [root@localhost tmp]# bg
  [1]+ sleep 100 &
  [root@localhost tmp]# bg 1
  -bash: bg: job 1 already in background
  ***********************************
  二、变量
  1、    系统变量: HONME     PATH  LANG  HOSTNAME
  env    系统内置变量 (它也可以列出全局变量)
  set     显示所有变量 (也包括自定义变量)
  ****************本地变量******************
  [root@localhost tmp]# a=1
  [root@localhost tmp]# echo $a
  1
  [root@localhost tmp]# bash
  [root@localhost tmp]# echo $a
  [root@localhost tmp]# set | grep ^a
  [root@localhost tmp]# exit
  [root@localhost tmp]# set |grep ^a
  a=1
  ****************************************
  [root@localhost ~]# export a=1
  [root@localhost ~]# env | grep ^a
  a=1
  [root@localhost ~]# bash
  [root@localhost ~]# echo $a
  1
  [root@localhost ~]# echo $b
  [root@localhost ~]#
  ******************************************
  2、变量名规则:
  a、变量名=值     中间没有空格
  [root@localhost ~]# a = 1
  -bash: a: command not found
  [root@localhost ~]# a=1
  [root@localhost ~]#
  b、变量名不能以数字开头
  [root@localhost ~]# 3a=1
  -bash: 3a=1: command not found
  [root@localhost ~]# a3=1
  [root@localhost ~]#
  c、变量名只能以大小写,数字和下划线。 但不能以数字开头
  [root@localhost ~]# a=1
  [root@localhost ~]# echo $a
  1
  [root@localhost ~]# unset a
  [root@localhost ~]# echo $a
  [root@localhost ~]#
  3、取消变量 unset a
  4、` `  反引号引用命令的结果
  [root@localhost ~]# rpm -qf `which vi`
  vim-minimal-7.2.411-1.8.el6.x86_64
  5、变量的引用,单引号,双引号
  ********************************
  [root@localhost tmp]# a=hello && echo $a
  hello
  [root@localhost tmp]# b=$a && echo $b
  hello
  [root@localhost tmp]# b=$a123 && echo $b
  [root@localhost tmp]# b=$a"123" && echo $b
  hello123
  [root@localhost tmp]# b=$a 123 && echo $b
  -bash: 123: command not found
  [root@localhost tmp]# b="$a 123" && echo $b   双引号引用$a的值
  hello 123
  [root@localhost tmp]# b='$a 123' && echo $b    单引号直接显示$a
  $a 123
  [root@localhost tmp]# a=3;b=4;echo "$a+$b"=$[$a+$b]
  3+4=7
  [root@localhost tmp]# let c=$a+$b ; echo $c
  7
  [root@localhost tmp]# c=$[$a + $b];echo $c
  7
  三、系统和个人环境变量的配置文件
  /etc/profile  PATH, USER, LOGNAME, MAIL, INPUTRC, HOSTNAME, HISTSIZE, umask等
  /etc/bashrc  $PS1  umask    以后如果设置umask修改 /etc/profile 不要改这个文件
  .bash_profile  用户自己的环境变量, 改文件会调用.bashrc文件
  .bashrc  当用户登录时以及每次打开新的shell时, 执行该文件
  .bash_history  记录命令历史用的
  .bash_logout :当退出shell时,会执行该文件。
  ***********************************************************
  1、设置PS1
  [root@localhost tmp]# echo $PS1   // 设置PS1
  [\u@\h \W]\$
  [root@localhost tmp]# PS1='[\[\e[32m\]#\##\[\e[31m\]\u@\[\e[36m\]\h \w]\$\[\e[m\]'
  [#45#root@localhost /tmp]#ls
  111.txt  222.txt  a4_F-14:01:47.log  a6_F-14:01:47.log  b2015-04-09-14:10:25.log  b.txt
  1.txt    2.txt    a5_F-14:01:47.log  a7_F-14:01:47.log  b2015-04-09-14:10:34.log
  [#46#root@localhost /tmp]#PS1=[\u@\h \W]\$
  -bash: W]$: command not found
  [#47#root@localhost /tmp]#PS1='[\u@\h \W]\$'
  [root@localhost tmp]#PS1='[\u@\h \W]\$ '
  2、~/.bash_profile文件
  [root@localhost tmp]# cat ~/.bash_profile
  # .bash_profile
  # Get the aliases and functions
  if [ -f ~/.bashrc ]; then               // .profile 调用.bashrc文件
  . ~/.bashrc
  fi
  # User specific environment and startup programs
  PATH=$PATH:$HOME/bin
  export PATH
  3、~/.bashrc文件解析
  [root@localhost tmp]# cat ~/.bashrc
  # .bashrc
  # User specific aliases and functions
  alias rm='rm -i'
  alias cp='cp -i'
  alias mv='mv -i'
  alias vi=vim
  # Source global definitions
  if [ -f /etc/bashrc ]; then     // ~/.bashrc 调用/etc/bashrc 文件
  . /etc/bashrc
  fi
  [root@localhost tmp]#
  四、shell中的特殊字符
  * 匹配零个或多个字符
  ?匹配一个字符
  # 注释
  \  退义字符。特殊字符还原为普通字符
  | 管道 。将符号前面命令的结果丢给符号后面的命令,一般针对文档操作的命令比较常用,例如cat, less, head, tail, grep, cut, sort, wc, uniq, tee, tr, split, sed, awk等等
  $ 引用变量,如a=1;echo $a  。 但!$ 是指上条命令的最后参数
  ; 分号,将多行命令在一行写入时,可用分号
  & 让命令后台执行。sleep 500 &
  五、常用命令
  1、cut     -d  -f  -c
  用法:cut [选项]... [文件]...
  从每个文件中输出指定部分到标准输出。
  长选项必须使用的参数对于短选项时也是必需使用的。
  -b, --bytes=列表              只选中指定的这些字节
  -c, --characters=列表         只选中指定的这些字符
  -d, --delimiter=分界符        使用指定分界符代替制表符作为区域分界
  -f, --fields=LIST       select only these fields;  also print any line
  that contains no delimiter character, unless
  the -s option is specified
  -n                      with -b: don't split multibyte characters
  --complement              补全选中的字节、字符或域
  -s, --only-delimited          不打印没有包含分界符的行
  --output-delimiter=字符串 使用指定的字符串作为输出分界符,默认采用输入
  的分界符
  --help            显示此帮助信息并退出
  --version         显示版本信息并退出
  仅使用f -b, -c 或-f 中的一个。每一个列表都是专门为一个类别作出的,或者您可以用逗号隔
  开要同时显示的不同类别。您的输入顺序将作为读取顺序,每个仅能输入一次。
  每种参数格式表示范围如下:
  N   从第1 个开始数的第N 个字节、字符或域
  N-  从第N 个开始到所在行结束的所有字符、字节或域
  N-M 从第N 个开始到第M 个之间(包括第M 个)的所有字符、字节或域
  -M  从第1 个开始到第M 个之间(包括第M 个)的所有字符、字节或域
  当没有文件参数,或者文件不存在时,从标准输入读取
  ***************************
  cut -d ':'  -f 1  /etc/passwd
  cut -d ':'   -f 1,3 /etc/passwd
  cut -d ':'  -f 1-4 /etc/passwc
  cut -c 2-5 /etc/passwd
  cut -c 1 /etc/passwd
  ****************************
  2、sort
  sort -t ':'  -k3 -n /etc/passwd
  sort -t ':'   k3 -n /etc/passwd  | cut -d: -f3
  cut  -d:  -f3  /etc/passwd | sort -n
  -n  数字大小排序
  -r  反向排序
  -u  去重复
  cut -d ':'   -f3 1.txt | sort -nr | uniq -c       //uniq -c 显示行号
  3、wc    -l -m -w
  -c    输出字节数统计
  -m   输出字符数统计
  -l     输出行数统计
  -L    显示最长行的长度
  -w   显示单词计数
  [root@localhost tmp]# cat 2.txt
  a ab
  aa aabb
  [root@localhost tmp]# wc 2.txt
  2  4 13 2.txt
  [root@localhost tmp]# cat -A 2.txt
  a ab$
  aa aabb$
  [root@localhost tmp]# line=`wc -l  /etc/passwd | cut -d '  ' -f2`; echo $line
  4、uniq -c 去重复
  5、tee   类似于 >
  cat 4.txt | tee 1.log    。重定向到文件同时,显示在屏幕上
  [root@localhost tmp]# cat 2.txt | tee 222.txt
  a ab
  aa aabb
  [root@localhost tmp]# cat 222.txt
  a ab
  aa aabb
  [root@localhost tmp]#
  6、tr   替换  。 没sed强大啦
  [root@localhost tmp]# cat 2.txt
  a ab
  aa aabb
  [root@localhost tmp]# cat 2.txt | tr [a-z] [A-Z]
  A AB
  AA AABB
  ls  | tr 'a-z'  'A-Z'
  ls  1.txt | tr 't'  'T'
  7、split
  -a     指定后缀长度为N (默认为2)
  -b     指定每个输出文件的字节大小
  -C     指定每个输出文件里最大行字节大小
  -d     使用数字后缀代替字母后缀
  -l      指定每个输出文件有多少行
  ******************************************************************
  [root@localhost dir1]# for  i in `seq 1 10000`;do cat /etc/passwd >>1.txt;done   复制10000次/etc/passwd 到 1.txt
  [root@localhost tmp]# split -b 1M  1.txt -d -a 1 log  && ls
  111.txt  1.txt  222.txt  2.txt  log0  log1  log2  log3  log4  log5  log6  log7
  [root@localhost dir1]# ls a* | xargs -i mv {} {}.txt
  [root@localhost tmp]# ls log* | xargs -i mv {} {}-`date +%F:%T`   && ls
  111.txt  222.txt  log0-2015-04-09:15:46:03  log2-2015-04-09:15:46:03  log4-2015-04-09:15:46:03  log6-2015-04-09:15:46:03
  1.txt    2.txt    log1-2015-04-09:15:46:03  log3-2015-04-09:15:46:03  log5-2015-04-09:15:46:03  log7-2015-04-09:15:46:03
  [root@localhost tmp]# for i in `ls log*` ;do echo $i;done
  log0-2015-04-09:15:46:03
  log1-2015-04-09:15:46:03
  log2-2015-04-09:15:46:03
  log3-2015-04-09:15:46:03
  log4-2015-04-09:15:46:03
  log5-2015-04-09:15:46:03
  log6-2015-04-09:15:46:03
  log7-2015-04-09:15:46:03
  [root@localhost tmp]# n=0;for i in `ls log*`;do mv $i log$n && let n=$n+1 ;done ;ls  // 将log*的文件重命名回去
  111.txt  1.txt  222.txt  2.txt  log0  log1  log2  log3  log4  log5  log6  log7
  ***************************************************
  5、输入输出重定向
  1、     标准输入输出
设备设备文件名文件描述符类型键盘/dev/stdin0标准类输入显示器/dev/stdout1标准输出显示器/dev/stderr2标准错误输出  2、输出 重定向
类型符号作用标准输出重定向命令 > 文件           覆盖的方式,将命令结构输出到指定的文件标准输出重定向命令 >> 文件追加的方式,将命令结构输出到指定的文件标准错误输出重定向   错误命令2> 文件    覆盖的方式,将命令的错误输出到指定的文件  2与>之间不能有空格
标准错误输出重定向错误命令2>>追加的方式,将命令的错误输出到指定的文件正确输出和错误输出  同时保存
命令 > 文件 2>&1覆盖的方式,将正确和错误输出都保存到  同一个文件
正确输出和错误输出  同时保存
命令 >>文件 2>&1追加的方式,将正确和错误输出都保存到  同一个文件
正确输出和错误输出  同时保存
命令 &>文件覆盖的方式,将正确和错误输出都保存到  同一个文件
正确输出和错误输出  同时保存
命令 &>>文件追加的方式,将正确和错误输出都保存到  同一个文件
正确输出和错误输出  同时保存
命令 >>文件1  2>>文件2
把正确的输出追加到文件1中,把错误的  输出追加到文件2中
  2、xargs
  1. 当你尝试用rm 删除太多的文件,你可能得到一个错误信息:/bin/rm Argument list too long. 用xargs 去避免这个问题
  find ~ -name ‘*.log’ -print0 | xargs -0 rm -f
  本例中xargs将find产生的长串文件列表拆散成多个子串,然后对每个子串调用rm。-print0表示输出以null分隔(-print使用换行);-0表示输入以null分隔。这样要比如下使用find命令效率高的多
  ***********添加100个用户,8位密码随机*****************
  [root@localhost tmp]# for i in `seq 1 100`  ;do useradd user$i &>/dev/null && echo `mkpasswd -l 8`| passwd  --stdin user$i &>/dev/null;done
  ***********删除user开头用户 *************************
  [root@localhost tmp]# for i in `grep ^user  /etc/passwd | cut -d: -f1 `;do userdel -r $i && echo "delete $i success";done


运维网声明 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-558051-1-1.html 上篇帖子: shell遍历一个日期范围 下篇帖子: bash shell之数组使用(牛逼篇)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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