Shell
shell
*****************
vim test.sh # Linux中脚本名称的后缀为.sh
#!/bin/bash # 指定编辑脚本所用的shell
echo hello # 写入一行或多行命令echo "hello the world"
ls -l /var # 注:使用命令的原名,不用别名
echo over # 建议echo加引号
保存退出
脚本执行需要x(执行)权限
chmod +x test.sh
sh test.sh # 不用执行权限
绝对路径执行脚本 /root/test.sh
相对路径执行脚本 ./test.sh
**********************
变量 # 临时存放数据的工具(代数中的xyz)
shu = 3 # 声明变量 变量名:shu 变量值:3
ming = yushun
echo $shu # 变量名前加“$”表示提取变量的值
echo $ming
vim test1.sh
#!/bin/bash
shu = 3
ming = yushun
echo $shu
( echo ${shu}a )
echo $ming
保存退出
执行脚本验证
输出结果为
3
(3a)
yushun
************************
数学运算
#!/bin/bash
shu1=12 # “=”两端没有空格
shu2=6
let sh1=$shu1+$shu2 # let表示后面使用数学运算(+-*/)
let sh2=$shu1-$shu2 # 符号“+-*/”之间没有空格
let sh3=$shu1*$shu2
let sh4=$shu1/$shu2
echo "$sh1 $sh2 $sh3 $sh4"
#!/bin/bash
shijian=`date` # 将命令的执行结果赋值给变量,反单引括起来的字 符当命令来之行
echo "The time is $shijian"
shijian1=`date "+20%y--%m--%d %H:%M:%S"`
echo "The time is $shijian1"
------------------------
#!/bin/bash
echo "PLEASE INPUT A NUM"
read shu1 # 从键盘输入数据,赋值给变量
echo "PLEASE INPUT A NUM"
read shu2
let sh1=$shu1+$shu2
echo "The sum is $sh1"
给变量赋值的方式:
1.声明变量值 shu=1
2.命令的执行结果赋值 shu=`date`
3.键盘输入赋值 read shu
*****************
判断语句
格式
if [ 条件 ] ;then # 条件的判断 [] 前后必须有空格
//代码1 # 条件成立(满足条件),执行代码1
else //代码2 # 条件不成立,执行代码2
fi
vi 1.sh
#!/bin/bash
echo "Please input your score:"
read chengji
if [ $chengji -gt 60 ];then
echo "jige"
else
echo "bujige"
fi
注:shell脚本中比较数字大小
-gt 大于
-lt 小于
-ge 大于等于
-le 小于等于
-eq 等于
=ne 不等于
[ $ming = "zhangsan" ] # 用=判断字符型变量的值,
格式要求=前后有空格,字符最好用""隐起来
[ -z $shu ] # 判断变量是否为空,变量中没有存数据,
则判断为成立true
#!/bin/bash
read name
if [ -z $name ];then
echo "kong"
else
echo "buweikong"
fi
注:执行脚本直接回车输出为“kong”
[ -d "/boot" ] # 判断目录是否存在
[ -f "/home/tom" ] # 判断文件是否存在
[ -r "/root/1.sh" ] # 判断文件是否具备读权限(-w 写权限-x执行权限)
#!/bin/bash
read name
if [ -r "$name" ];then
echo "you read quanxian"
else
echo "wu read quanxian"
fi
注:在普通用户下使用
当有多个条件判断的时候
if [ 条件1 -a 条件2 ] # -a 与(并且)
# -o 或(或者)
if [ ! 条件 ] # ! 不满足条件时为真 !与条件之间右空格
#!/bin/bash
read name
read name1
if [ -f $name -a $name1 -ge 10 ];then
echo "zhengchang"
else
echo "buzhengchang"
fi
#!/bin/bash
read name
if [ ! -f $name ];then
echo "zhengchang"
else
echo "buzhengchang"
fi
格式2 多级判断
if [ 条件1 ];then
//代码1
elif [ 条件2 ];then
//代码2
else //代码3
fi
实验:成绩 90-优秀 75-良好 60-及格 其他-不及格
#!/bin/bash
echo "Please input your score(0-100):"
read score
if [ $score -ge 100 ];then
echo "请输入0-100之间的数字"
elif [ $score -ge 90 ];then
echo "youxiu"
elif [ $score -ge 75 ];then
echo "lianghao"
elif [ $score -ge 60 ];then
echo "jige"
else echo bujige
-----------------
1.循环语句
while循环
格式:
while [ 条件 ];
do
//代码
done
执行思路:先进行条件的判断---执行代码---第二次条件判断--执行代码--...无限循环,直到条件不满足为止
#!/bin/bash
shu=1
while [ $shu -le 100 ];
do
echo "$shu:hello"
let shu=$shu+1
done
作业:1-100的加和
2.for循环
for 变量 in 值1 值2 值3... ---把变量,依次用后面的数值赋值,带入代码 部分执行
do
//代码
done
for shu in 1 2 3 4 5 6 7 8 9 10 ---shell中只能罗列,适合小次数的循环
do
echo "No. $shu :hello"
done
---------------
多分支语句
case
格式
case $变量 in ---根据变量的值,找到下面的对应项,执行相应代码
值1)代码1
;; ---双分号表示本项代码结束
值2)代码2
;;
.....
*)代码 ---*表示没有和变量对应的值,则执行本代码
;;
esac
#!/bin/bash
read shu
case $shu in
aaa)echo aaa
;;
bbb)echo bbb
;;
*) echo "Please retry"
esac
-----------------
选择结构
select
格式
select 变量 in 值1 值2 值3 ... ------给用户显示选择列表,选择的值赋值给变量,带入代码执行
do
//代码
break
done
#!/bin/bash
select shu in aaa bbb
do
case $shu in
aaa)echo aaa
;;
bbb)echo bbb
;;
*) echo "Please retry"
esac
break
done
---------------------------
用户管理
vi usermanager.sh
#!/bin/bash
echo"==================="
echo "welcom to user Manager System"
echo "==================="
shu=1
while[$shu -eq 1] ;
do
echo "please input your choice"
select xuan in "List all users" "Add a new user" "Delete a user"
"Change a user's password" "exit"
do
case $xuan in
"List all users")
cat /etc/passwd | awk -F ":" ‘{print $1}’;;
"Add a new user")
echo "please input a new name"
read ming
useradd $ming
passwd $ming;;
"Delete a user")
echo "please enter the user you want to delete"
read ming
userdel -r $ming ;;
"Change a user's password")
echo "please enter the user you want to change the passwd"
read ming
passwd $ming ;;
"exit")
echo "Good Bye"
shu=2;;
*) echo "Error" ;;
esac
break
done
echo " "
done
---------------
字符处理
head
tail
awk
sed
cut
shu=abc123
shu2=${shu%%1*} ---%%表示删除右侧的指定字符,shu2=abc
shu3=${shu##*c} ---##表示删除左侧的指定字符,shu3=123
* while read shu ---逐行读取文件内容,每次一行,赋值给变量,带入代码执行
do
//代码
done < f1
-------------
while read shu
do
echo $shu
done < /mnt/f1
f1 文件内容如下:
111
222
333
-------------
while read c1 c2 ---逐行读取文件内容,每次一行,每列的值赋值给对应的变量
do
echo $c2
done < /root/1.txt
1.txt内容如下
1 11
2 22
-----------------