shell 编程
if判断
比对大小
#!/bin/bash
num1=300
num2=200
if(($num1>$num2));then
echo "this is $num1 > $num2 !"
else
echo "num1=$num1要小于num2=$num2"
fi
判断目录是否存在
逻辑运算符解析:
-f 判断文件是否存在 eg: if [ -f filename ]
-d 判断目录是否存在 eg: if [ -d dir ]
-eq 等于 应用于:整型比较
-ne 不等于 应用于:整型比较
-lt 小于 应用于:整型比较
-gt 大于 应用于:整型比较
-le 小于或等于 应用于:整型比较
-ge 大于或等于 应用于:整型比较
-a 双方都成立( and) 逻辑表达式 –a 逻辑表达式
-o 单方成立( or) 逻辑表达式 –o 逻辑表达式
-z 空字符串
#!/bin/bash
DIR=/home/shell/0818
if [ ! -d $DIR ];then
mkdir -p $DIR
echo -e "$DIR目录已经创建了"
else
echo -e "$DIR目录已经存在了,请退出"
fi
测试文件是否存在
#!/bin/bash
FILES=/home/shell/1.txt
if [ ! -f $FILES ];then
echo "$FILES文件已经创建了" >> $FILES
else
echo -e "$FILES文件已经存在了,无需创建请退出"
cat $FILES
fi
多条件
#!/bin/sh
scores=$1
if [ -z $scores ]; then
echo "命令后面跟上分数:{$0 60 | 80}"
exit
fi
#scores=80
if [[ $scores -gt 85 ]]; then
echo "优秀"
elif [[ $scores -gt 75 ]]; then
echo "良好!"
elif [[ $scores -ge 60 ]]; then
echo "及格!"
else
echo "不及格!"
fi
if实现mysql备份
#!/bin/sh
#auto backup mysql
#wugk 2012-12-12
#Define PATH 定义变量
BAKDIR=/data/backup/mysql/`date +%Y-%m-%d`
MYSQLDB=webapp
MYSQLPW=backup
MYSQLUSR=backup
#must use root user run scripts 必须使用 root 用户运行, $UID
为系统变量
if
[ $UID -ne 0 ];then
echo This script must use the root user ! ! !
sleep 2
exit 0
fi
#Define DIR and mkdir DIR 判断目录是否存在,不存在则新建
if
[ ! -d $BAKDIR ];then
mkdir -p $BAKDIR
else
echo This is $BAKDIR exists....
fi
#Use mysqldump backup mysql 使用 mysqldump备份数据库
/usr/bin/mysqldump -u$MYSQLUSR -p$MYSQLPW -d
$MYSQLDB >$BAKDIR/webapp_db.sql
echo "The mysql backup successfully "
for语句
for 变量 in 字符串
do
语句q
done
案例1
#!/bin/bash
for i in $(seq 1 15)
do
echo "NUM is $i"
done
案例2
1-100的和
j=0
for (i=1;i<=100;i++)
do
j=$(expr $i+$j)
done
echo $j
while
判断数字
i=1
while [[ $i -lt 10 ]];
do
echo $i
((i++))
done
util循环
满足条件后久退出,否则一直循环
a=10
util [[ $a -lt 0 ]];
do
echo $a
((a--))
done
case选择语句
#!/bin/bash
case $1 in
apache)
echo "等待安装apache"
;;
mysql)
echo "等待安装mysql"
;;
php)
echo "等待安装php"
;;
*)
echo -e "\033[32m输入错误,请输入apache | mysql | php \033[0m"
;;
esac
select选择
PS3="选择你将执行的菜单:"
select i in "apache" "mysql" "php"
系统变量及函数
$0 shell本身名字
$1 第一个参数
$2 第二个参数
$? 输出是否有错
$* 所有参数
$# 参数的个数
#!/bin/sh
echo "shell本身名字:$0"
echo "\$1 is:$1"
echo "\$2 is:$2"
echo "\$? is:" $?
echo "\$* is:" $*
echo "\$# is:" $#
sh test.sh par1 par2
函数
#定义函数
function name(){
xxxx
xxxxx
}
#调用函数
name
案例
#!/bin/bash
nginx_file=nginx-1.6.1.tar.gz
nginx_src=$(echo $nginx_file | sed 's/\.tar\.gz//g')
down_url=http://nginx.org/download
mysql_file=mysql-5.7.23.tar.gz
function nginx_insall(){
wget -c $down_url/$nginx_file
tar xzf $nginx_file;cd $nginx_src
if [ $? =eq 0 ];then
echo "nginx 安装成功"
fi
}
function mysql_install(){
tar zxf $mysql_file; cd mysql-5.7.23
}
function php_install(){
if [ -d /usr/local/mysql ]; then
tar zxf php_file;
else
echo "请先安装mysql"
fi
}
nginx_insall && mysql_install
sed 命令
替换,查找等
替换:sed 's/old/new/g' 预修改 彻底修改需要加 -i 参数 sed -i 's/old/new/g'
每行开头加参数 sed -i 's/^/&id /g' test.txt
每行末尾加参数 sed -i 's/$/& id /g' test.txt
匹配某个参数后进行替换 sed -i 's/zgl/test007/g' test.txt
在某个参数后下一行添加 sed -i '/zgl/a test007' test.txt
打印关键词的一行 sed -n '/zgl/p' test.txt
打印指定行 sed -n '1,5p' test.txt
找出一个文件最大最小的值 cat num.txt | sed 's/ /\n/g' | grep -v "^$"|sort -nr|sed -n '1p;$p'
删除空行 sed -i '/^\s*$/d' test.txt
awk
$1:第一列 awk '{print $1}'
NF:最后一列 awk '{print $NF}'
再切割出来的字符前加上字符 awk '{print "xxx" $NF}'
cat /etc/passwd | sed 's/:/ /g' | awk '{print $1}'
以冒号做分割打印 cat /etc/passwd | awk -F: '{print $1}'
df -h | grep "/$" | awk '{print $5}'
替换%号
df -h | grep "/$" | awk '{print $5}'|sed 's/%//g'
查找出来,删除空行,截取关键词,首位添加字符
cat test.txt |awk '{print $4}' | sed '/^\s*$/d' |sed 's/^/&01: /'
cat test.txt |awk '{print "01:" $4}' | sed '/^\s*$/d'
find命令
find . -name test.txt
find . -maxdepth -1 找第一级目录
find . -maxdepth 1 -type f -name "."
find . -maxdepth 1 -type f -name "*.txt" -mtime -1 -exec rm -rf {} \;
批量修改文件名
创建测试数据
[root@zgl-server shell]# mkdir changefilename
[root@zgl-server shell]# cd changefilename/
[root@zgl-server changefilename]# touch chao_{1..5}_finish.jpg
[root@zgl-server changefilename]# touch chao_{1..5}_finish.png
[root@zgl-server changefilename]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_1_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_1_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_2_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_2_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_3_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_3_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_4_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_4_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_5_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_5_finish.png
1、去掉所有文件的_finish后缀字符信息去掉
思路
1、单个文件去掉字符
[root@zgl-server changefilename]# mv chao_1_finish.jpg chao_1.jpg
[root@zgl-server changefilename]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_1_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_1.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_2_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_2_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_3_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_3_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_4_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_4_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_5_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_5_finish.png
2、利用变了的字串功能去掉后缀
[root@zgl-server changefilename]# f=chao_1_finish.png
[root@zgl-server changefilename]# echo $f
chao_1_finish.png
[root@zgl-server changefilename]# echo ${f}
chao_1_finish.png
[root@zgl-server changefilename]# echo ${f//finish/}
chao_1_.png
[root@zgl-server changefilename]#
3、利用反引号的功能,修改文件名
[root@zgl-server changefilename]# mv $f `echo ${f//finish/}`
[root@zgl-server changefilename]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_1.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_1_.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_2_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_2_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_3_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_3_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_4_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_4_finish.png
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_5_finish.jpg
-rw-r--r-- 1 root root 0 5月 27 21:55 chao_5_finish.png
[root@zgl-server changefilename]#
4、使用for循环批量修改,只修改jpg的文件
[root@zgl-server changefilename]# ls *.jpg
chao_1.jpg chao_2_finish.jpg chao_3_finish.jpg chao_4_finish.jpg chao_5_finish.jpg
[root@zgl-server changefilename]# ls *fin*.jpg
chao_2_finish.jpg chao_3_finish.jpg chao_4_finish.jpg chao_5_finish.jpg
[root@zgl-server changefilename]# ls
chao_1.jpg chao_2_finish.jpg chao_3_finish.jpg chao_4_finish.jpg chao_5_finish.jpg
chao_1_.png chao_2_finish.png chao_3_finish.png chao_4_finish.png chao_5_finish.png
[root@zgl-server changefilename]# echo `ls *fin*.jpg`
chao_2_finish.jpg chao_3_finish.jpg chao_4_finish.jpg chao_5_finish.jpg
[root@zgl-server changefilename]# for file_name in `ls *fin*.jpg`;do echo $file_name;done
chao_2_finish.jpg
chao_3_finish.jpg
chao_4_finish.jpg
chao_5_finish.jpg
[root@zgl-server changefilename]# for file_name in `ls *fin*.jpg`;do mv $file_name `echo ${file_name//finish/}`;done
[root@zgl-server changefilename]# ls *.jpg
chao_1.jpg chao_2_.jpg chao_3_.jpg chao_4_.jpg chao_5_.jpg
[root@zgl-server changefilename]#
shell脚本选择对应条件进行操作
#!/bin/bash
#定义一个目录
path=/home/shell/test_script
#判断目录是否存在,不存在就创建
[ ! -d "$path" ] && mkdir -p $path
#做脚本的正常逻辑
cat <<END
1.[install mysql]
2.[install java]
3.[install nginx]
4.exit
请输入你的操作对应的数字:
END
read num
#echo $num
expr $num + 1 &> /dev/null
#判断上条命令的结果,-ne 表示不等于
#限制用户输入的必须是数字
[[ "$?" -ne "0" ]] && {
echo "你必须输入数字,为{ 1 | 2 | 3 }"
}
#对输入的数字是1,2,3进行判断
#安装mysql
[[ "$num" -eq "1" ]] && {
echo "正在安装mysql……………………,请稍等!!!"
sleep 3;
#执行脚本安装mysql
#source $path/install_mysql.sh
#对文件权限进行判断
[ -x "$path/install_mysql.sh" ] || {
echo "这个文件不存在或者文件没有执行权限"
exit 1
}
$path/install_mysql.sh
#$?接收上面执行的状态
exit $?
}
#安装Java
[[ "$num" -eq "2" ]] && {
echo "正在安装java……………………,请稍等!!!"
sleep 3
#执行脚本安装java
#source $path/install_java.sh
#对文件权限进行判断
[ -x "$path/install_java.sh" ] || {
echo "这个文件不存在或者文件没有执行权限"
exit 1
}
$path/install_java.sh
#$?接收上面执行的状态
exit $?
}
#安装nginx
[[ "$num" -eq "3" ]] && {
echo "正在安装nginx……………………,请稍等!!!"
sleep 3
#执行脚本安装java
#source $path/install_nginx.sh
#对文件权限进行判断
[ -x "$path/install_nginx.sh" ] || {
echo "这个文件不存在或者文件没有执行权限"
exit 1
}
$path/install_nginx.sh
#$?接收上面执行的状态
exit $?
}
#退出
[[ "$num" -eq "4" ]] && {
echo "再见!!"
exit 3
}
#限制用户必须输入1,2,3
#[[ ]]支持正则表达式
#取反
[[ ! "$num" =~ [1-3] ]] && {
echo "你的输入有错误,只能输入{ 1 |2 | 3 }"
exit 4
}
测试结果
[root@zgl-server ~]# bash 22.sh
1.[install mysql]
2.[install java]
3.[install nginx]
4.exit
请输入你的操作对应的数字:
3
正在安装nginx……………………,请稍等!!!
这个文件不存在或者文件没有执行权限
[root@zgl-server ~]# bash 22.sh
1.[install mysql]
2.[install java]
3.[install nginx]
4.exit
请输入你的操作对应的数字:
4
再见!!
[root@zgl-server ~]# bash 22.sh
1.[install mysql]
2.[install java]
3.[install nginx]
4.exit
请输入你的操作对应的数字:
t
你必须输入数字,为{ 1 | 2 | 3 }
你的输入有错误,只能输入{ 1 |2 | 3 }