前言
本文主要汇总有关shell脚本常用的知识点,有时候使用忘记某些用法指令,特此汇总方便后续查阅。
一.shell脚本编写的头部定义:
定义的shell脚本头部有多种写法,具体根基实际系统结构处理,如下:
bash"> #!/bin/sh , #!/bin/bash, #!/usr/bin/env bash#sed -i 's/\r$//' #可能脚本中有其他字符,将dos字符替换掉。
二.常用shell脚本的基本知识点
1.常用基本特殊符号含义
bash"> echo "welcome to shell world !"echo "$$" #当前脚本执行时的PIDecho "$#" #传入脚本的参数个数echo "$@" #传入脚本的所有参数(可通for循环访问)echo "$1...n" #着个访问传图的参数echo "$?" #上一条命令执行的返回值(0:success 非零:失败)
2.test命令:用预判断条件表达式的真假
bash">a=1
b=1
if test $a -eq $b
thenecho "True"
elseecho "False"
fi
3.if条件分支
bash">s='nihao'
c='nihao'
if [ $s = $c ];thenecho "True"
elseecho "False"
fi
flag="Y"
if [ $flag == "Y"];thenehco "$flag"
elif [ $flag == "N"];thenecho "$flag"
fi
4while循环以及case分支实例
bash">while [ 1 = 1 ];
do#表示客户端手动输入对应的数据值,加上-p参数,可以带描述信息如下:#read -p "please input your name:" varread var case "$var" in1)echo "1---";;2)echo "2---";;3)echo "3---";;4)break;;esac
done
5.运算符
bash">#!/bin/sh
#运算符
a=1
b=2
c=3
d=5
#expr 计算运算表达式
echo "--- + , - , * , / -------->"
echo "a + b = `expr $a + $b`"
echo "a - b = `expr $a - $b`"
#*需要转移,因为有特殊意义
echo "a * b = `expr $a \* $b`"
echo "a / b = `expr $a / $b`"echo "<--------------------------"echo "---------------------------"#逻辑与算符
echo "逻辑运算符:==, != , %, !, -a 与,-o 或"
if [ $a == $b ];thenecho "True"
elseecho "False"
fiif [ $a != $b ];thenecho "True"
elseecho "False"
fiecho "a % b = `expr $a % $b`"flag=0
if [ !$flag ];thenecho "flag = $flag"
elseecho "flag = $flag"
fiif [ $a != 8 -a $b != 0 ];thenecho "$a != 8 -a $b != 0 --> True"
elseecho "$a != 8 -a $b != 0 --> False"
fiif [ $a > 0 -o $b > 0 ];thenecho "$a > 0 -o $b > 0 --> True"
elseecho "$a > 0 -o $b > 0 --> False"
fiecho "----------------------------"
echo " -eq:等于 -le:小于等于-ge:大于等于-ne:不等于-lt:小于-gt:大于 "if [ $a -eq $b ]
thenecho "true"
elseecho "false"
fiif [ $a -ne $b ]
thenecho "true"elseecho "false"
fiif [ $a -gt $b ]
thenecho "true"
elseecho "false"
fiif [ $a -lt $b ]
thenecho "true"
elseecho "false"
fiif [ $a -ge $b ]
thenecho "true"
elseecho "false"
fiif [ $a -le $b ]
thenecho "true"
elseecho "false"
fiecho "---------------------------"
6.字符串处理
bash">echo "---------------------------"
echo "-z :字符串长度为空返回True,-n:字符串长度不为空返回True !"
mystr='welcome to python world !'echo "mystr len = ${#mystr}"
echo "mystr son str ${mystr:2:4}"
if [ -z $mystr ];thenecho "True"
elseecho "False"
fiif [ -n $mystr ];thenecho "True"
elseecho "False"
fi
7.文件相关操作的判定运算符
bash">echo "---------------------------"
echo " -d:是否为目录"
echo " -r:文件是否可读"
echo " -w:文件dirname="./TTT"
filename="file"是否可写"
echo " -x:文件是否可执行"
echo " -s:文件是否为空(文件大小是否为空,不为空返回true)"
echo " -e:文件是否存在"
dirpath="./TTT/file"
echo "$dirpath"if [ -d $dirname ];thenecho "is dir"
elseecho "not is dir"
fiif [ -r $dirpath ];thenecho "read"
elseecho "not read"
fi
if [ -w $dirpath ];thenecho "write"
elseecho "not write"
fiif [ -x $dirpath ];thenecho "exec"
elseecho "not exec"
fi
if [ -s $dirpath ];thenecho "not empty"
elseecho "empty"
fiif [ -e $dirpath ];thenecho "exist"
elseecho "not exist"
fi
echo "---------------------------"
8.数组操作
bash">echo "array ...."
declare -a array #定义一个数组
array=(1 2 3 4 5 6 7)
for v in ${array[@]}
doecho "$v"
donefor i in `seq 10`
doecho "$i"
donefor i in {1..6}
doecho "$i"
doneecho "---------------------------"
echo "read..."
#-p 制定输入的变量名称
read -p "Please input your name:" name
echo "name = $name"#-t 秒数 等待输入的秒数,-p联合使用
read -t 3 -p "please input second:" sec
echo "sec = $sec"#-s 隐藏输入的内容,-p联合使用
read -s -p "please input passwd:" password
echo "password = $password"