Shell 基础知识--细说linux配套视频
| Shell 基础概括Shell是什么?shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动、挂起、停止甚至是编写一些程序。 shell 还是一个功能强大的编程语言,易编写,易调试,灵活性强。shell是解释型的脚本语言。在shell中可以直接调用Linux系统命令。 Shell的分类
 Linux 支持的Shell
 范例: [root@localhost ~]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin [root@localhost ~]# Shell脚本的执行方式echo 输出命令命令格式 echo [选项] [输出内容] 选项 
 范例: # 输出制表符、换行符 [root@localhost ~]# echo -e 'atbtcndtetf' a b c d e f [root@localhost ~]# # 输出颜色 e[1; 代表开启颜色输出 # e[0m 代表结束颜色输出 # 常用的颜色 30m=黑色,31m=红色,32m=绿色, # 33m=黄色,34m=蓝色,35m=洋红,36m=青色,37m=白色 [root@localhost ~]# echo -e "e[1;31m abcd e[0m" abcd # 红色 [root@localhost ~]# 第一个脚本范例: [root@localhost ~]# vim hello.sh #!/bin/Bash # 并不是注释,标称下面的是shell脚本 #The first program #Author:dyh(E-mail:jiejie_yh@163.com) echo -e "e[1;35m hello world e[0m" 执行: 1.给脚本赋予执行权限,然后用相对路径或绝对路径执行 [root@localhost ~]# ll
    总用量 12
    -rw-------. 1 root root 1262 7月  25 18:59 anaconda-ks.cfg
    -rw-r--r--. 1 root root  107 7月  29 22:13 hello.sh
    -rw-r--r--. 1 root root    9 7月  28 11:59 test.txt
    [root@localhost ~]# chmod 755 hello.sh 
    [root@localhost ~]# ./hello.sh 
    hello world 
    [root@localhost ~]# /root/hello.sh 
    hello world2.通过bash调用脚本执行(该方式不需要执行权限) [root@localhost ~]# bash hello.sh 
    hello world 
    [root@localhost ~]#
 用于在windows中编写shell脚本格式转换成unix格式。 Bash的基本功能历史命令与命令补全
 命令格式: history [选项] [历史命令保存文件] 选项: 
 范例: [root@localhost ~]# ll .bash_history 
-rw-------. 1 root root 15053 7月  29 21:28 .bash_history
[root@localhost ~]# pwd
/root
[root@localhost ~]# history -w
[root@localhost ~]# cat .bash_history 
passwd -l jiejie
vim /etc/shadow
passwd -u jiejie
vim /etc/shadow
passwd -l yh
passwd -u yh
echo "12345" | passwd --stdin user1 
echo "12345"|passwd --stdin user1 
echo "123456"|passwd --stdin jiejie 
vim /etc/shadow
clear
...
[root@localhost ~]# history -c
[root@localhost ~]# history 
    1  history 
[root@localhost ~]#历史命令默认会保存1000条,可以在环境变量配置文件 
 
 范例: [root@localhost ~]# history 1 history 2 vim /etc/profile 3 history [root@localhost ~]# !2 vim /etc/profile [root@localhost ~]# !! vim /etc/profile [root@localhost ~]# service httpd restart Redirecting to /bin/systemctl restart httpd.service [root@localhost ~]# !ser service httpd restart Redirecting to /bin/systemctl restart httpd.service [root@localhost ~]# 命令的别名与常用快捷键
 命令格式: # 设定命令别名 alias 别名=‘原命令’ # 查询命令别名 [root@localhost ~]# alias 范例: [root@localhost ~]# alias vi='vim' [root@localhost ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias vi='vim' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@localhost ~]# 
 
 
 上面提到的用命令设置别名的方式只会临时生效,如果需要让别名永久生效,需要写入文件 
 [root@localhost ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias vi='vim' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@localhost ~]# unalias vi [root@localhost ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@localhost ~]# Bash常用快捷键
 输入输出重定向
 
 范例: # 标准输出重定向 [root@localhost ~]# ls > abc [root@localhost ~]# cat abc abc anaconda-ks.cfg hello.sh test.txt [root@localhost ~]# date 2017年 07月 30日 星期日 00:35:29 CST [root@localhost ~]# date > abc [root@localhost ~]# cat abc 2017年 07月 30日 星期日 00:35:38 CST [root@localhost ~]# date >> abc [root@localhost ~]# cat abc 2017年 07月 30日 星期日 00:35:38 CST 2017年 07月 30日 星期日 00:36:12 CST [root@localhost ~]# # 标准错误输出重定向 [root@localhost ~]# lst 2>> abc [root@localhost ~]# cat abc 2017年 07月 30日 星期日 00:35:38 CST 2017年 07月 30日 星期日 00:36:12 CST -bash: lst: 未找到命令 [root@localhost ~]# # 正确输出和错误输出同时保存 [root@localhost ~]# lst >> bcd 2>&1 [root@localhost ~]# cat bcd -bash: lst: 未找到命令 [root@localhost ~]# [root@localhost ~]# date &>>cde [root@localhost ~]# cat cde 2017年 07月 30日 星期日 00:48:27 CST [root@localhost ~]# datesf &>>cde [root@localhost ~]# cat cde 2017年 07月 30日 星期日 00:48:27 CST -bash: datesf: 未找到命令 [root@localhost ~]# [root@localhost ~]# ls &>>/dev/null # 特殊文件,不存在,垃圾箱。不保存任何数据 [root@localhost ~]# ls >> def 2>>efg [root@localhost ~]# cat def abc anaconda-ks.cfg bcd cde def efg hello.sh test.txt [root@localhost ~]# cat efg [root@localhost ~]# 
 命令格式: wc [选项] [文件名] 选项: 
 范例: [root@localhost ~]# wc
2222211 22221  12
12323
343
12
      4       6      31
[root@localhost ~]#
[root@localhost ~]# wc < abc 
  3  15 114
[root@localhost ~]# 
[root@localhost ~]# wc <<hello
> ksdfk
> 12
> 324
> 34543
> hello
 4  4 19
[root@localhost ~]#多命令顺序执行与管道符
 
 范例: [root@localhost ~]# ls;date;cd /user;pwd abc anaconda-ks.cfg bcd cde def efg hello.sh test.txt 2017年 07月 30日 星期日 01:30:26 CST -bash: cd: /user: 没有那个文件或目录 /root [root@localhost ~]# 
 
 命令格式: dd if=输入文件 of=输出文件 bs=字节数 count=个数 选项: 
 范例: [root@localhost ~]# date;dd if=/dev/zero of=/root/testfile bs=1k count=100000;date 2017年 07月 30日 星期日 01:40:11 CST 记录了100000+0 的读入 记录了100000+0 的写出 102400000字节(102 MB)已复制,0.195148 秒,525 MB/秒 2017年 07月 30日 星期日 01:40:11 CST [root@localhost ~]# ll -h 总用量 98M -rw-r--r--. 1 root root 114 7月 30 00:38 abc -rw-------. 1 root root 1.3K 7月 25 18:59 anaconda-ks.cfg -rw-r--r--. 1 root root 42 7月 30 00:47 bcd -rw-r--r--. 1 root root 74 7月 30 00:48 cde -rw-r--r--. 1 root root 54 7月 30 01:09 def -rw-r--r--. 1 root root 0 7月 30 01:09 efg -rwxr-xr-x. 1 root root 107 7月 29 22:22 hello.sh -rw-r--r--. 1 root root 98M 7月 30 01:40 testfile -rw-r--r--. 1 root root 9 7月 28 11:59 test.txt [root@localhost ~]# 
 命令格式: # 命令1的正确输出作为命令2的操作对象 命令1 | 命令2 范例: [root@localhost ~]# ll -a /etc | more 总用量 1236 drwxr-xr-x. 78 root root 8192 7月 29 23:01 . dr-xr-xr-x. 19 root root 262 7月 28 12:04 .. -rw-r--r--. 1 root root 16 7月 25 18:58 adjtime -rw-r--r--. 1 root root 1518 6月 7 2013 aliases -rw-r--r--. 1 root root 12288 7月 25 19:06 aliases.db drwxr-xr-x. 2 root root 236 7月 25 18:51 alternatives -rw-------. 1 root root 541 3月 31 2016 anacrontab -rw-r--r--. 1 root root 55 11月 5 2016 asound.conf drwxr-x---. 3 root root 43 7月 25 18:51 audisp drwxr-x---. 3 root root 83 7月 25 19:05 audit drwxr-xr-x. 2 root root 22 7月 25 18:51 bash_completion.d -rw-r--r--. 1 root root 2853 11月 6 2016 bashrc drwxr-xr-x. 2 root root 6 11月 7 2016 binfmt.d -rw-r--r--. 1 root root 38 11月 30 2016 centos-release -rw-r--r--. 1 root root 51 11月 30 2016 centos-release-upstream drwxr-xr-x. 2 root root 6 11月 6 2016 chkconfig.d -rw-r--r--. 1 root root 1165 11月 15 2016 chrony.conf -rw-r-----. 1 root chrony 62 7月 25 19:05 chrony.keys drwxr-xr-x. 2 root root 21 7月 25 18:50 cron.d drwxr-xr-x. 2 root root 57 7月 26 23:14 cron.daily -rw-------. 1 root root 0 3月 31 2016 cron.deny drwxr-xr-x. 2 root root 22 6月 10 2014 cron.hourly --More-- [root@localhost ~]# netstat -an | grep ESTABLISHED tcp 0 0 192.168.60.188:22 192.168.60.235:57322 ESTABLISHED udp 0 0 192.168.60.188:50066 85.199.214.100:123 ESTABLISHED udp 0 0 192.168.60.188:35766 193.228.143.23:123 ESTABLISHED udp 0 0 192.168.60.188:34918 85.199.214.101:123 ESTABLISHED 
 命令格式: grep [选项] "搜索内容" 文件名 选项: 
 范例: [root@localhost ~]# grep 'root' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost ~]# grep -n --color=auto "root" /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [root@localhost ~]# grep -i --color=auto "root" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost ~]# grep -i --color=auto "ROot" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost ~]# [root@localhost ~]# netstat -an | grep ESTABLISHED tcp 0 0 192.168.10.217:22 192.168.10.238:63332 ESTABLISHED udp 0 0 192.168.10.217:57794 193.228.143.23:123 ESTABLISHED [root@localhost ~]# 通配符与其它特殊符号
 
 范例 [root@localhost ~]# cd /tmp/ [root@localhost tmp]# ll 总用量 12 -rw-rw-r--. 1 yh yh 0 7月 27 00:01 a -rwx------. 1 root root 836 7月 25 18:59 ks-script-7AxIKHhttpd.service-RMis7k drwxrwsrwx. 2 root root 28 7月 28 08:53 test -rw-------. 1 root root 0 7月 25 18:47 yum.log [root@localhost tmp]# rm -rf * [root@localhost tmp]# ll 总用量 0 [root@localhost tmp]# touch abc [root@localhost tmp]# touch abcd [root@localhost tmp]# touch 012 [root@localhost tmp]# touch 0abc [root@localhost tmp]# ls ?abc 0abc [root@localhost tmp]# ls [0-9]* 012 0abc [root@localhost tmp]# ls [^0-9]* abc abcd [root@localhost tmp]# 
 
 范例: [root@localhost tmp]# name=yh [root@localhost tmp]# echo '$name' $name [root@localhost tmp]# echo "$name" yh [root@localhost tmp]# echo '$(date)' $(date) [root@localhost tmp]# echo "$(date)" 2017年 07月 30日 星期日 06:12:34 CST [root@localhost tmp]# echo "`date`" 2017年 07月 30日 星期日 06:12:44 CST [root@localhost tmp]# echo '`date`' `date` [root@localhost tmp]# 
 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
