xshell
xshell概述
Linux 提供的 Shell 解析器有
[root@localhost shells]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
[root@localhost shells]#
Shell 脚本入门
脚本以#!/bin/bash 开头(指定解析器)
输出测试
[root@localhost shells]# cat helloword.sh
#!/bin/bash
echo "helloword"
[root@localhost shells]# sh helloword.sh
helloword
[root@localhost shells]#
脚本的常用执行方式
第一种:采用 bash 或 sh+脚本的相对路径或绝对路径(不用赋予脚本+x 权限) sh+脚本的相对路径
sh+脚本的绝对路径
bash+脚本的相对路径
bash+脚本的绝对路径
使用./ 也可以执行 需要赋权
变量
常用系统变量
$HOME、$PWD、$SHELL、$USER
查看系统变量的值
[root@localhost shells]# echo $HOME
/root
自定义变量
定义变量:变量名=变量值,注意,=号前后不能有空格
撤销变量:unset变量名
[root@localhost shells]# A=5
[root@localhost shells]# echo $A
5
[root@localhost shells]# unset A
[root@localhost shells]# echo $A
声明静态的变量 B=2,不能 unset
[root@localhost shells]# readonly
[root@localhost shells]# readonly B=2
[root@localhost shells]# echo $B
2
[root@localhost shells]# B=9
bash: B: readonly variable
[root@localhost shells]#
变量的值如果有空格,需要使用双引号或单引号括起来
[root@localhost shells]# C="I LOVE JAVA"
[root@localhost shells]# echo $C
I LOVE JAVA
特殊变量
$n
(功能描述:n 为数字,$0 代表该脚本名称,$1-$9 代表第一到第九个参数,十以 上的参数,十以上的参数需要用大括号包含,如${10}
[root@localhost shells]# cat test1.sh
# !/bin/bash
echo '=====$n==='
echo $0
echo $1
echo $2
[root@localhost shells]# ./test1.sh
=====$n===
./test1.sh
[root@localhost shells]# ^C
[root@localhost shells]#
$#
$# (功能描述:获取所有输入参数个数,常用于循环,
判断参数的个数是否正确以及 加强脚本的健壮性)
$*、
$@
$* (功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体) $@ (功能描述:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待)
# !/bin/bash
echo '=====$n==='
echo $0
echo $1
echo $2
echo '=========$#========='
echo $#
=========$*============
echo $*
=========$@=============
echo $@
[root@localhost shells]# ./test1.sh a b c f g
=====$n===
./test1.sh
a
b
=========$#=========
5
./test1.sh: line 14: =========a: command not found
a b c f g
./test1.sh: line 16: =========a: command not found
a b c f g
$?
$? (功能描述:最后一次执行的命令的返回状态。
如果这个变量的值为 0,证明上一 个命令正确执行;如果这个变量的值为非 0(具体是哪个数,由命令自己来决定),
则证明 上一个命令执行不正确了。)
[root@localhost shells]# ./helloword.sh
helloword
[root@localhost shells]# echo $?
0
[root@localhost shells]#
运算符
“$((运算式))” 或 “$[运算式]”
计算(2+3)* 4 的值
[root@localhost shells]# S=$[(2+3)*4]
[root@localhost shells]# echo $S
20
条件判断
-eq 等于(equal)
-ne 不等于(not equal)
-lt 小于(less than)
-le 小于等于(less equal)
-gt 大于(greater than)
-ge 大于等于(greater equal)
注:如果是字符串之间的比较 ,用等号“=”判断相等;用“!=”判断不等
按照文件权限进行判断
-r 有读的权限(read)
-w 有写的权限(write)
-x 有执行的权限(execute)
案例实操
23 是否大于等于 22
[root@localhost shells]# [ 23 -ge 22 ]
[root@localhost shells]# echo $?
0
[root@localhost shells]# [ -w helloworld.sh ]
[root@localhost shells]# echo $?
1
流程控制
if判断
单分支
if [ 条件判断式 ];
then 程序
fi
多分支
if [ 条件判断式 ]
then
程序
elif [ 条件判断式 ]
then
程序
else
程序
fi 注
注意事项:
①[ 条件判断式 ],中括号和条件判断式之间必须有空格
②if 后要有空格
案例
# !/bin/bash
if [ $1 -eq 1 ]
then
echo "nihao"
elif [ $1 -eq 2 ]
then
echo "nibuhao"
fi
[root@localhost shells]# chmod 777 if.sh
[root@localhost shells]# ./if.sh 1
nihao
[root@localhost shells]#
case语句
基本语法
case $变量名 in
"值 1")
如果变量的值等于值 1,则执行程序 1
;;
"值 2")
如果变量的值等于值 2,则执行程序 2
;;
…省略其他分支…
*) 如果变量的值都不是以上的值,则执行此程序
;;
esac
注意事项:
(1)case 行尾必须为单词“in”,每一个模式匹配必须以右括号“)”结束。 (2)双分号“;;”表示命令序列结束,相当于 java 中的 break。
(3)最后的“*)”表示默认模式,相当于 java 中的 default。
案例
输入一个数字,如果是 1,则输出 nihao,如果是 2,则输出 cls,如果是其它,输出 buhao 否则 douhao
[root@localhost shells]# cat case.sh
# !/bin/bash
case $1 in
"1")
echo "nihao"
;;
"2")
echo "buhao"
;;
*)
echo "douhao"
;;
esac
[root@localhost shells]# ./case.sh 1
nihao
[root@localhost shells]#
for 循环
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
从 1 加到 100
[root@localhost shells]# cat foreach.sh
# !/bin/bash
sum=0
for((i=0;i<=100;i++))
do
sum=$[$sum+$i]
done
echo $sum
[root@localhost shells]# ./foreach.sh
5050
[root@localhost shells]#
for 变量 in 值 1 值 2 值 3…
do
程序
done
案例实操
[root@localhost shells]# cat for2.sh
# !/bin/bash
for i in cls mlu wls
do
echo "I LOVE JAVA"
done
[root@localhost shells]# chmod 777 for2.sh
[root@localhost shells]# ./for
for2.sh foreach.sh
[root@localhost shells]# ./for2.sh
I LOVE JAVA
I LOVE JAVA
I LOVE JAVA
[root@localhost shells]#
比较$*和$@区别
$*和$@都表示传递给函数或脚本的所有参数,不被双引号“”包含时,都以$1 $2 …$n
# !/bin/bash
echo '=======$*======='
for i in $*
do
echo "nihao"
done
echo '=====$@====='
for j in $@
do
echo "buhao"
done
[root@localhost shells]# ./for3.sh cls mly wls
=======$*=======
nihao
nihao
nihao
=====$@=====
buhao
buhao
buhao
当它们被双引号“”包含时,$*会将所有的参数作为一个整体,
以“$1 $2 …$n”的形式输 出所有参数;
$@会将各个参数分开,以“$1” “$2”…“$n”的形式输出所有参数。
[root@localhost shells]# cat for4.sh
# !/bin/bash
echo '======$*======'
for i in "$*"
#$*中的所有参数看成是一个整体,所以这个 for 循环只会循环一次
do
echo "nihao"
done
echo '====$@===='
for j in "$@"
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次"
do
echo "buhao"
done
[root@localhost shells]# chmod 777 for4.sh
[root@localhost shells]# ./for4.sh cls mly wls
======$*======
nihao
====$@====
buhao
buhao
buhao
[root@localhost shells]#
while 循环
while [ 条件判断式 ]
do
程序
done
[root@localhost shells]# cat while.sh
# !/bin/bash
sum=0
i=1
while [ $i -le 100 ]
do
sum=$[$sum+$i]
i=$[$i+1]
done
echo $sum
[root@localhost shells]# chmod 777 while.sh
[root@localhost shells]# ./while.sh
5050
read 读取控制台输入
read (选项) (参数)
①选项:-p:指定读取值时的提示符;
-t:指定读取值时等待的时间(秒)如果-t 不加表示一直等待
②参数
变量:指定读取值的变量名
[root@localhost shells]# cat read.sh
# !/bin/bash
read -t 7 -p "Enter your name in 7 seconds" :" NN
echo $NN
函数
系统函数
basename
截取 路径的文件名称。
[root@localhost shells]# ls
case.sh for2.sh for4.sh helloword.sh nihao.txt test1.sh while.sh
demo.sh for3.sh foreach.sh if.sh read.sh test.sh
[root@localhost shells]# basename /opt/nihao.txt .txt
nihao
dirname
获取文件路径
[root@localhost shells]# ls
case.sh for2.sh for4.sh helloword.sh nihao.txt test1.sh while.sh
demo.sh for3.sh foreach.sh if.sh read.sh test.sh
[root@localhost shells]# dirname /opt/nihao.txt
/opt
自定义函数
[ function ] funname[()]
{
Action;
[return int;]
}
[root@localhost shells]# ls
case.sh for2.sh for4.sh fun.sh if.sh read.sh test.sh
demo.sh for3.sh foreach.sh helloword.sh nihao.txt test1.sh while.sh
[root@localhost shells]# chmod 777 fun.sh
[root@localhost shells]# ./fun.sh
Please input the number1: 2
Please input the number2: 5
[root@localhost shells]# cat fun.sh
#!/bin/bash
function sum() {
s=0 s=$[$1+$2] echo "$s"
}
read -p "Please input the number1: " n1;
read -p "Please input the number2: " n2;
sum $n1 $n2;
正则表达式入门
常规匹配
[root@localhost shells]# cat /etc/passwd |grep mxx
mxx::1000:1000:孟祥鑫:/home/mxx:/bin/bash
常用特殊字符
特殊字符:^
cat /etc/passwd | grep ^a
特殊字符:$
cat /etc/passwd | grep t$
^$ 匹配什么
cat /etc/passwd | grep r..t
特殊字符:*
不单独使用,他和上一个字符连用,表示匹配上一个字符 0 次或多次
文本处理工具
cut 制表符
-f 列号,提取第几列
-d 分隔符,按照指定分隔符分割列,默认是制表符“\t”
-c 按字符进行切割 后加加 n 表示取第几列 比如 -c 1
[root@localhost shells]# cat cut.txt
dong shen
guan zhen
wo wo
lai lai
le le
切割 cut.txt 第一列
[root@localhost shells]# cut -d " " -f 1 cut.txt
dong
guan
wo
lai
le
切割 cut.txt 第二、三列
[root@localhost shells]# cut -d " " -f 2,3 cut.txt
shen
zhen
wo
lai
le
[root@localhost shells]#
在 cut.txt 文件中切割出 guan
cat cut.txt |grep guan | cut -d " " -f 1 guan
切割 ifconfig 后打印的 IP 地址
ifconfig ens33 | grep netmask | cut -d " " -f 10 192.168.145.136
awk
-F 指定输入文件分隔符
-v 赋值一个用户定义变量
sudo cp /etc/passwd ./ passwd 数据的含义 用户名:密码(加密过后的):用户 id:组 id:注释:用户家目录:shell 解析器
搜索 passwd 文件以 root 关键字开头的所有行,并输出该行的第 7 列。
awk -F : '/^root/{print $7}' passwd /bin/bash
搜索 passwd 文件以 root 关键字开头的所有行,并输出该行的第 1 列和第 7 列, 中间以“,”号分割。
awk -F : '/^root/{print $1","$7}' passwd root,/bin/bash