Linux Shell编程快速入门
Posted JintuZheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux Shell编程快速入门相关的知识,希望对你有一定的参考价值。
目录
- 章节1:变量
- 章节2:逻辑和算术运算
- 章节3:流程控制
- 章节4:函数
- 章节5:其他
附:题目实例讲解
1. 变量
#!/bin/bash
# 初始赋值和变量使用
a=12
echo $a # echo ${a}
# 只读变量
a="hello"
readonly a
# 删除变量
unset a
# 字符串拼接
a="hello"
b="world"
echo "jt,"$a$b
# 获取字符串长度,获取数组长度
a="hello"
echo ${#a}
a=(12 34 89 90)
echo ${#a[@]}
# 提取字符串
a="i love you"
echo ${a:2:4}
# 数组赋值和修改
a=(12 90 90 89)
echo ${a[0]}
a[0]=34
echo ${a[0]}
b=${a[2]}
# 数组遍历
a=("H" 10 90 89)
for((i=0;i<${#a[@};i++));do
echo ${a[$i]}
done
# 某个变量统一大小写(-u,-l)
typeset -u opt
opt="hello"
echo $opt
# 变量内嵌等价(下面两种方式一样的)
opt="hello"
echo "$opt world"
opt="hello"
echo $opt" world"
# 显示执行结果
echo `date +"%Y-%m-%d %H:%M:%S"`
# printf的格式化输出
printf "%-10s %-8s %-4.2f %d\\n" 郭靖 男 66.1234 20
>>#输出:郭靖 男 66.12 20
# 判断字符串是否为空
if [ $a ]
then
..
# 判断字符串长度是否为0
if [ -z $a ]
then
..
2. 逻辑和算术运算
#!/bin/bash
# 算术运算方式1 expr
val=`expr 2 + 2`
echo $val
val=`expr $a + $b`
echo $val
# 算术运算方式2
a=123
b=23
c=$[a+b]
echo $c
d=$[40 + 30]
echo $d
# 算术运算方式3 let(最直观)
a=12
b=20
let c=a+b
echo $c
# 文件是否存在
if test -e ./files/1.txt
then
echo "Y"
else
echo "N"
fi
# 与或非(-a与,-o或,!非)
if test -e ./notFile -o -e ./bash
then
echo "至少有一个文件存在!"
else
echo "两个文件都不存在"
fi
# 与或(&&与,||或)
if [[ $num1 -gt $num2 && $num3 == $num4 ]]
then
...
# 等于,不等于(方式1)
num1=100
num2=200
if test $[num1] -eq $[num2]
then
echo "Equal"
else
echo "Not Equal"
fi
# 等于,不等于,大于小于(方式2)
num1=100
num2=200
if [ $num1 == $num2 ]
then
echo "Equal"
else
echo "Not Equal"
fi
3. 流程控制
#!/bin/bash
# 条件语句
a=10
b=20
if [ $a -lt $b ]
then
echo "Y1"
elif [ $a -gt $b ]
then
echo "Y2"
else
echo "Eq"
fi
# case判断
read opt
case $opt in
1)
echo "you input 1"
;;
"q")
echo "you input q"
;;
*)
echo "others"
;;
esac
# 循环语句(直接遍历)
for v in 12 43 43
do
echo $v
done
# 示例:遍历目录
for f in `ls /usr`
do
echo $f
done
# 循环语句(C风格)
a=(12 43 434)
for((i=0;i<${#a[@]};i++));
do
echo ${a[$i]}
done
# 循环语句(while 条件)
idx=0
while((idx<5))
do
echo $idx
let idx++
done
# 循环读入
typeset -l num
while read num
do
echo $num
done
# break和continue
while read opt
do
if [ $opt == "q" ]
then
echo "out"
break
fi
done
4. 函数
#!/bin/bash
# 函数书写与传参和返回
add(){
echo $1
b=`expr $1 + $2`
return $b
}
add 12 90
c=$?
echo $c
# bool判断(0代表true,其他数值代表false,一般用-1更好区分)
func(){
if [ $1 == $2 ]
then
return 0
else
return -1
fi
}
if func 12 90
then
echo "true"
else
echo "false"
fi
5. 其他补充
# 用于查看安装的shells
cat /etc/shells
# 给脚本增加执行权限
chmod +x run.sh
# 大于小于,大于等于, 小于等于(数字)
-gt -lt -ge -le
# 判断字符串是否相等,只有一个等号
=
# 跟踪模式执行某个脚本
/bin/bash -x run.sh
附:示例编写
例子1:
#!/bin/bash
options_show(){
echo "Use one of the following options:"
echo "P:To display current directory"
echo "S:To display the name of running file"
echo "D:To display today's date and present time(如:2017-04-26 05:45:12) "
echo "L:To see the list of files in your present working directory"
echo "W:To see who is logged in"
echo "I:To see the ip address of this local machine"
echo "echo \\"Q:To quit this program"
echo "Enter your option and hit:"
}
typeset -l opt
options_show
while read opt
do
case $opt in
"p")
echo `pwd`
;;
"s")
echo $0
;;
"d")
echo `date +"%Y-%m-%d %H:%M:%S"`
;;
"l")
echo `ls .`
;;
"w")
echo `whoami`
;;
"i")
echo `ifconfig`
;;
"q")
break
;;
esac
options_show
done
例子2
#!/bin/bash
show(){
echo "Please input your score:"
}
show
while read score
do
if [ $score -lt 0 ]
then
break
elif [ $score -lt 60 ]
then
echo "Failed"
elif [ $score -ge 60 -a $score -le 69 ]
then
echo "Passed"
elif [ $score -ge 70 -a $score -le 79 ]
then
echo "Medium"
elif [ $score -ge 80 -a $score -le 89 ]
then
echo "Good"
elif [ $score -ge 90 -a $score -le 100 ]
then
echo "Excellent"
fi
show
done
例子3
#!/bin/bash
show(){
echo "Please input your dir-path:"
}
while read dir
do
if [ $dir = "q" ]
then
break
fi
if [ -e $dir ]
then
idx=0
fns=(`ls $dir`) # 这个括号非常重要,可以转成数组
ls -l $dir | while read x
do
if [ $idx == 0 ]
then
let idx++
continue
else
echo "/"${fns[$idx-1]}"_"${x:0:1}
let idx++
fi
done
else
echo "This path is not exists!"
fi
show
done
这里,如果是带有空格的行就需要用read的方式来分割,如果是空格的分割,只需要用数组转换法即可
例子4
#!/bin/bash
while read opt
do
for((i=$opt;i>=1;i--));do
for((j=$i;j>=1;j--));do
echo -n "$j " #不换行
done
echo
done
done
例子5
需求1:启动和关闭ftp服务:
#!/bin/bash
systemctl start vsftpd
if [ $?!=0 ];then
echo "start ftp error" | mail -s "start ftp error" root
else
pinfo=`ps -ef | grep vsftpd | head -n 1`
date=`date +"%Y-%m-%d"`
echo $pinfo>>/var/ftp/${date}.log
fi
#!/bin/bash
systemctl stop vsftpd
if [ $?!=0 ];then
echo "Stop ftp error" | mail -s "Stop ftp error" root
fi
需求2:ping Baidu:
#!/bin/bash
date=`date +"%Y-%m-%d"`
ping -c 4 >>/var/ftp/${date}.log
需求3:打包日志:
#!/bin/bash
date=`date +"%Y-%m-%d"`
tar -czvf "/root/${date}.tar.gz" /var/ftp
chmod 400 "/root/${date}.tar.gz"
rm -rf /var/ftp/*
以上是关于Linux Shell编程快速入门的主要内容,如果未能解决你的问题,请参考以下文章