shell命令解释器
shell脚本语言 可以直接调用linux系统命令
shell分类
1.CShell
2.Bourne Shell
Bash shell (linux默认shell)
查看linux支持的shell
查看/etc/passwd
1 系统登录用户root后的/bin/bash表示root登入用户为bash shell
2 系统命令shutdown后的/sbin/shutdown表示只能执行shutdown
3 为用户后的/nologin表示不许登录的伪用户
hello
!有特殊用法
echo “hello world” 有空格要加双引号
echo hello 打印没有多个空格的
echo ‘hello’ 里有特殊字符
[root@VM_47_240_centos ~]# echo “ab\bc”
ac
Administrator@PC MINGW32 /
$ echo -e “\e[1;31m abcd\e[0m”
\e[1;开启颜色输出
\e[0m 结束颜色输出
31m 红色
abcd 内容
[root@VM_47_240_centos ~]# echo -e “\x61”
a
shell脚本执行
scp -r root@123.202.153.134:/root/mq /d/git/c++/Test/mooc/
赋予执行权限chmod 755 hello.sh
./hello.sh
或bash hello.sh
windows和linux换行符的不同
windows的换行符是^M$
[mq@mq 1]$ cat -A git.txt
540 $ ssh -T git@github.com^M$
linux换行符是$
[mq@mq ~]$ cat -A bashhistory
cd /usr/local/games/$
dos2linux
[mq@mq ~]$ dos2unix git.txt
dos2unix: 正在转换文件 git.txt 为Unix格式``
[mq@mq ~]$ !984 //执行历史记录中984条命令这里是ls
ls
2 '2018-02-06 20-45-53 的屏幕截图.png' bashhistory downlond hello.sh OS rpm 模板
'2018-01-21 14-31-08 的屏幕截图.png' '2018-02-06 21-03-45 的屏幕截图.png' bb git movies picture Wallpapers 文档
'2018-02-06 20-00-58 的屏幕截图.png' '2018-02-09 13-45-09 的屏幕截图.png' Downloads git.txt music program 公共 桌面
[mq@mq ~]$ !! *//执行上一条命令 ls*
ls
2 '2018-02-06 20-45-53 的屏幕截图.png' bashhistory downlond hello.sh OS rpm 模板
'2018-01-21 14-31-08 的屏幕截图.png' '2018-02-06 21-03-45 的屏幕截图.png' bb git movies picture Wallpapers 文档
'2018-02-06 20-00-58 的屏幕截图.png' '2018-02-09 13-45-09 的屏幕截图.png' Downloads git.txt music program 公共 桌面
[mq@mq ~]$ !l *执行最近以l开头的子命令*
ls
2 '2018-02-06 20-45-53 的屏幕截图.png' bashhistory downlond hello.sh OS rpm 模板
'2018-01-21 14-31-08 的屏幕截图.png' '2018-02-06 21-03-45 的屏幕截图.png' bb git movies picture Wallpapers 文档
'2018-02-06 20-00-58 的屏幕截图.png' '2018-02-09 13-45-09 的屏幕截图.png' Downloads git.txt music program 公共 桌面
输入输出重定向
标准输出重定向
- > 覆盖并将正确输出结果写入 date > a.txt
- >> 追加并将正确输出结果写入 data >>a.txt
标准错误输出重定向
- 2> 覆盖并将错误输出结果写入 lt 2> a.txt
- 2>> 追加并将错误输出结果写入 lt 2>>a.txt
正确和错误同时保存
- > a.txt 2>&1 覆盖的方式
- >> a.txt 2>&1 追加方式
- &>a.txt 覆盖
- &>>a.txt 追加
- >>A.TXT 2>>B.TXT 正确结果输出到A 错误入B
输入重定向
将文件作为输入
[mq@mq shell]$ wc <a.txt
2 6 58
[mq@mq shell]$ wc <<hello
> aa
> aa
> hello 再次输入hello结束
2 2 6
多命令顺序执行
多命令执行符 | 格式 | 作用 |
---|---|---|
; | cmd1;cmd2 | 多个命令顺序执行,没有逻辑关系 |
&& | cmd1&&cmd2 | 只有cmd1成功执行,才执行cmd2 |
ll | cmd1llcmd2 | 只有cmd1执行错误,才执行cmd2 |
l | cmd1lcmd2 | 命令1的正确输出作为命令2的操作对象 |
&& .configure&&make&&make install
判断命令是否正确执行 命令 &&echo yes||echo no
|管道 ls /etc|more
grep 命令过滤
[root@mq mq]# netstat -an |grep "ESTABLISHED"
tcp 0 0 192.168.1.106:43114 108.61.127.13:3000 ESTABLISHED
tcp 0 0 192.168.1.106:43088 108.61.127.13:3000 ESTABLISHED
通配符
通配符 | 作用 |
---|---|
? | 匹配一个任意字符 |
[] | 匹配括号中任意一字符 |
[a-z] | 匹配a-z任意一个字符 |
[^0-9] | 匹配一个不是数字的一个字符 |
bash中的其他特殊符号
符号 | 作用 |
---|---|
” | 在单引号中任何特殊符号都不起作用 |
“” | 处$(调用变量的值) ‵`(引用命令) \(转义字符)外的其他字符都没有特殊含义 |
“ | 用来引用系统变量 |
$() | 和“一样引用系统变量 |
# | 以#开头表示注释 |
$ | 调用变量的之 |
反斜线 | 转义符,其后的字符将失去特殊含义,变成普通符号输出 |
[mq@mq ~]$ name=mq
[mq@mq ~]$ echo "$name" //双引号
mq
[mq@mq ~]$ echo '$name' //单引号
$name
[mq@mq ~]$ echo $name
mq
[mq@mq ~]$ name=`date` //反引号
[mq@mq ~]$ echo $name
2018年 02月 10日 星期六 15:57:04 CST
[mq@mq ~]$ echo $name
2018年 02月 10日 星期六 15:59:11 CST
[mq@mq ~]$ echo \$name
$name
在bash中默认的变量类型全部为字符串型
用户自定义变量
又称为本地变量,只在当前shell起作用
mq@mq ~]$ name=mq 变量定义赋值
[mq@mq ~]$ echo $name查看变量值
mq
[mq@mq ~]$ unset name删除变量值
[mq@mq ~]$ echo $name
环境变量 在当前shell和和其子shell中起作用,如果将其写入配置文件,对所有shell起作用
设置环境变量 export 变量名=变量值
查询 env
删除 unset
[mq@mq ~]$ name=mq
[mq@mq ~]$ export sex=man
[mq@mq ~]$ age=9
[mq@mq ~]$ export age
[mq@mq ~]$ set
[mq@mq ~]$ bash
[mq@mq ~]$ echo $sex
man
[mq@mq ~]$ echo $name
[mq@mq ~]$ unset sex
[mq@mq ~]$ unset age
path 系统命令查找路径
[mq@mq ~]$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/mq/.local/bin:/home/mq/bin
PATH路径添加
[mq@mq ~]$ PATH="$PATH":/home/mq/ 暂时的设置,没有写入配置文件
[mq@mq ~]$ echo "$PATH"
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/mq/.local/bin:/home/mq/bin:/home/mq/
PS1
[mq@mq ~]$ echo $PS1
[\u@\h \W]\$\
[mq@mq ~]$PS1='[\u@\d\A\w\#]\$ '
[mq@一 2月 1220:25~6]$ PS1='[\u @ \d\A \w \#]\$ '
[mq @ 一 2月 1220:26 ~ 7]$ PS1='[\u @ \d \A \w \#]\$ '
[mq @ 一 2月 12 20:26 ~ 8]$ su
[root@mq mq]# PS1='[\u@\d\A\w\#]\$ '
[root@一 2月 1220:27/home/mq3]#
[mq@mq ~]$ PS1='[\u @ \@ \d \A \w \#]\$ '
[mq @ 08:31 下午 一 2月 12 20:31 ~ 3]$
PS1:定义系统提示符的变量
\d:显示日期,格式为 星期 月 日
\h:显示简写的主机名。默认的主机名 locallhost
\t:显示24小时制时间,格式为 “HH:MM:SS”
\T:显示24小时制时间,格式为 “HH:MM:SS”
\A:显示24小时制时间,格式为 “HH:MM”
\u:显示当前用户名
\w:显示当前所在目录的绝对路径
\W:显示当前所在目录的最后一个目录
#:执行历史记录的第几个命令
$:提示符。root显示为#,普通为$
位置参数变量
[mq @ 08:56 下午 一 2月 12 20:56 ~/git/brolinux/shell 14]$ ./shell.sh 1
./shell.sh
1
[mq @ 08:56 下午 一 2月 12 20:56 ~/git/brolinux/shell 15]$ cat shell.sh
#!/bin/bash
echo $0
echo $1
[mq @ 09:30 下午 一 2月 12 21:30 ~/git/brolinux/shell 27]$ cat shelL3.sh
#!/bin/bash
for i in "$*"
#S*中所有参数看成一个整体,所以这个循环只是一次
do
echo "The parameter is $i"
done
x=1
for y in "$@"
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次
do
echo "The parameter$x is $y"
x=$(($x+1))
done
[mq @ 09:30 下午 一 2月 12 21:30 ~/git/brolinux/shell 26]$ ./shelL3.sh 1 2 3 4
The parameter is 1 2 3 4
The parameter1 is 1
The parameter2 is 2
The parameter3 is 3
The parameter4 is 4
$n | 0代表命令本身, 0 代 表 命 令 本 身 , 1- 9代表第一到第九个参数,十个以上的参数用 9 代 表 第 一 到 第 九 个 参 数 , 十 个 以 上 的 参 数 用 {10}表示 |
$* | 这个变量代表命令行中所有的参数,$*把所有的参数看成整体 |
$@ | 这个变量代表命令行中所有的参数,$@把每个参数区分看待 |
$# | 这个变量代表命令行中参数的个数,不包括命令本身 |
预定义变量
预定义变量名 | 作用 |
$? | 最后一次执行命令的返回状态 |
$$ | 当前进程的PID |
$! | 后台运行最后一个进程的PID |
[mq@mq shell]$ cat shell4.sh
#!/bin/bash
#Author:maqaing
#输出当前进程的PID,脚本执行时,生成的PID
echo "The current process is $$"
find /home/mq -name hello.sh &
#查找root目录下的hello.sh &意思是把命令放入后台执行,
echo "The last one Daemon process is $!"
ls
echo "正在执行 $?"
[mq@mq shell]$ ./shell4.sh
The current process is 4818
The last one Daemon process is 4819
a.txt shell2.sh shelL3.sh shell4.sh shell.sh test.txt wo.txt
正在执行 0
[mq@mq shell]$ /home/mq/hello.sh
接受键盘输入的read
read [选项] 【变量名】
选项
-p 输出提示信息 -p “please input ….”
-t 秒数,指定等待时间、
-n 数字 指定输入字符的长度 ,然后执行
-s 隐式输入
[mq@mq shell]$ cat shell5.sh
#!/bin/bash
read -t 30 -p "please input your name:" name
#提示“请输入姓名”并等待30秒,把用户输入保存变量name中,
echo "Name is $name"
read -s -t 30 -p "Please enter your age:" age
#年龄采用隐式输入的方式 “-s”选项
echo -e "\n"
echo "Age is $age"
read -n 1 -t 30 -p "please select your gander[M/F]" gender
#使用“-n 1”选项只接受一个输入字符就会执行(都不用输入回车)
echo -e "\n"
echo "sex is $gender"
[mq@mq shell]$ ./shell5.sh
please input your name:mq
Name is mq
Please enter your age:
Age is 9
please select your gander[M/F]M
sex is M
数值运算和运算符
[root@mq shell]# chmod 755 oper1.sh
[root@mq shell]# ./oper1.sh
1+2
[root@mq shell]# cat oper1.sh
#!/bin/bash
a=1
b=2
echo "$a+$b"
因为linux shell 中默认变量类型是字符串型的,所以在对变量进行数值运算就不可以直接运算
declare声明变量类型
[root@mq shell]# cat oper1.sh
#!/bin/bash
a=1
b=2
echo "$a+$b"
#echo $a+$b
declare -p a
[root@mq shell]# ./oper1.sh
1+2
1+2
declare -- a="1" #--表示没有属性
数值运算
方法1
[root@mq shell]# aa=1
[root@mq shell]# bb=2
[root@mq shell]# declare -i cc=$aa+$bb
[root@mq shell]# echo $cc
3
方法2 expr或let数值运算工具
[mq@mq shell]$ cat shell6.sh
#!/bin/bash
a=1
b=2
echo "第一种方式declare"
declare -i c=$a+$b
echo "$c"
echo -e "\n"
echo "第二种方式expr或let表达式方式"
#+号两边各有一个空格,$(expr )表示把命令结果
d=$(expr $a + $b)
echo "$d"
[mq@mq shell]$ ./shell6.sh
第一种方式declare
3
第二种方式expr或let表达式方式
3
第三种方式
$((表达式))或者 $[运算式]
[mq@mq shell]$ cat shell6.sh
#!/bin/bash
a=1
b=2
echo "第一种方式declare"
declare -i c=$a+$b
echo "$c"
echo -e "\n"
echo "第二种方式expr或let表达式方式"
#+号两边各有一个空格,$(expr )表示把命令结果
d=$(expr $a + $b)
echo "$d"
echo "第三种方式输出$(())或者$[]"
e=$(($a+$b))
echo "$e"
f=$[$a+$b]
echo "$f"
//执行结果如下
[mq@mq shell]$ ./shell6.sh
第一种方式declare
3
第二种方式expr或let表达式方式
3
第三种方式输出0或者0
3
3
运算符
[mq@mq shell]$ chmod 755 shell7.sh
[mq@mq shell]$ ./shell7.sh
14
1
0
[mq@mq shell]$ cat shell7.sh
#!/bin/bash
a=1
b=2
#()可以调整运算顺序
c=$(( (3+4)*6/3))
echo "$c"
echo -e "\n"
d=$((16%3))
echo "$d"
echo -e "\n"
e=$((1&&0))
echo "$e"
echo -e "\n"