Linux shell脚本笔记-One
前言
本文主要汇总有关shell脚本常用的知识点,有时候使用忘记某些用法指令,特此汇总方便后续查阅。
一.shell脚本编写的头部定义:
定义的shell脚本头部有多种写法,具体根基实际系统结构处理,如下:
#!/bin/sh , #!/bin/bash, #!/usr/bin/env bash
#sed -i 's/\r$//' #可能脚本中有其他字符,将dos字符替换掉。
二.常用shell脚本的基本知识点
1.常用基本特殊符号含义
echo "welcome to shell world !"
echo "$$" #当前脚本执行时的PID
echo "$#" #传入脚本的参数个数
echo "$@" #传入脚本的所有参数(可通for循环访问)
echo "$1...n" #着个访问传图的参数
echo "$?" #上一条命令执行的返回值(0:success 非零:失败)
2.test命令:用预判断条件表达式的真假
a=1
b=1
if test $a -eq $b
then
echo "True"
else
echo "False"
fi
3.if条件分支
s='nihao'
c='nihao'
if [ $s = $c ];then
echo "True"
else
echo "False"
fi
flag="Y"
if [ $flag == "Y"];then
ehco "$flag"
elif [ $flag == "N"];then
echo "$flag"
fi
4while循环以及case分支实例
while [ 1 = 1 ];
do
#表示客户端手动输入对应的数据值,加上-p参数,可以带描述信息如下:
#read -p "please input your name:" var
read var
case "$var" in
1)
echo "1---"
;;
2)
echo "2---"
;;
3)
echo "3---"
;;
4)
break
;;
esac
done
5.运算符
#!/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 ];then
echo "True"
else
echo "False"
fi
if [ $a != $b ];then
echo "True"
else
echo "False"
fi
echo "a % b = `expr $a % $b`"
flag=0
if [ !$flag ];then
echo "flag = $flag"
else
echo "flag = $flag"
fi
if [ $a != 8 -a $b != 0 ];then
echo "$a != 8 -a $b != 0 --> True"
else
echo "$a != 8 -a $b != 0 --> False"
fi
if [ $a > 0 -o $b > 0 ];then
echo "$a > 0 -o $b > 0 --> True"
else
echo "$a > 0 -o $b > 0 --> False"
fi
echo "----------------------------"
echo " -eq:等于
-le:小于等于
-ge:大于等于
-ne:不等于
-lt:小于
-gt:大于 "
if [ $a -eq $b ]
then
echo "true"
else
echo "false"
fi
if [ $a -ne $b ]
then
echo "true"
else
echo "false"
fi
if [ $a -gt $b ]
then
echo "true"
else
echo "false"
fi
if [ $a -lt $b ]
then
echo "true"
else
echo "false"
fi
if [ $a -ge $b ]
then
echo "true"
else
echo "false"
fi
if [ $a -le $b ]
then
echo "true"
else
echo "false"
fi
echo "---------------------------"
6.字符串处理
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 ];then
echo "True"
else
echo "False"
fi
if [ -n $mystr ];then
echo "True"
else
echo "False"
fi
7.文件相关操作的判定运算符
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 ];then
echo "is dir"
else
echo "not is dir"
fi
if [ -r $dirpath ];then
echo "read"
else
echo "not read"
fi
if [ -w $dirpath ];then
echo "write"
else
echo "not write"
fi
if [ -x $dirpath ];then
echo "exec"
else
echo "not exec"
fi
if [ -s $dirpath ];then
echo "not empty"
else
echo "empty"
fi
if [ -e $dirpath ];then
echo "exist"
else
echo "not exist"
fi
echo "---------------------------"
8.数组操作
echo "array ...."
declare -a array #定义一个数组
array=(1 2 3 4 5 6 7)
for v in ${array[@]}
do
echo "$v"
done
for i in `seq 10`
do
echo "$i"
done
for i in {1..6}
do
echo "$i"
done
echo "---------------------------"
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"