linux备忘录-shell脚本

知识

shell执行方式

shell执行方式有

  1. 通过source.

在现在的bash环境中执行脚本
变量等会保持

  1. 通过bash shell.shsh shell.sh

使用一个新的bash环境执行脚本
当子程序完成后,在子程序内的各项变量或动作将会结束而不会回传到父程序中

  1. 通过./shell.sh

只用一个新的bash环境执行脚本
当子程序完成后,在子程序内的各项变量或动作将会结束而不会回传到父程序中

数值运算

$((计算式))可以进行数值运算

total=$(($number1*$number2))

条件判断

  1. test

test -e file 检测file是否存在
test -f file 检测file是否存在且为普通文件f
test -d file 检测file是否存在且为目录d
test -b file 检测file是否存在且为块设备b
test -c file 检测file是否存在且为字符设备c
test -S file 检测file是否存在且为Socket文件s
test -p file 检测file是否存在且为pipeline管道设备p
test -L file 检测file是否存在且为连结档l
档案的权限检测
test -r file 检测file是否存在且有r权限
test -w file 检测file是否存在且有w权限
test -x file 检测file是否存在且有x权限
test -u file 检测file是否存在且有SUID权限
test -g file 检测file是否存在且有SGID权限
test -k file 检测file是否存在且有SBIT权限
test -s file 检测file是否存在且为非空白文档
两个档案之间的比较
test file1 -nt file2 检测file1是否比file2新
test file1 -ot file2 检测file1是否比file2旧
test file1 -ef file2 检测file1和file2是否指向同一inode
两个整数之间判定
test n1 -eq n2 检测n1和n2是否相等
test n1 -ne n2 检测n1和n2是否不等
test n1 -gt n2 检测n1是否大于n2
test n1 -lt n2 检测n1是否小于n2
test n1 -ge n2 检测n1是否大于等于n2
test n1 -le n2 检测n1是否小于等于n2
判定字符串
test -z str 检测str是否为空
test -n str 检测str是否非空
test str1 = str2 检测str1和str2是否相同
test str1 != str2 检测str1和str2是否不同
多重条件判断
test xxx1 -a xxx2 逻辑xxx1和xxx2的与
test xxx1 -o xxx2 逻辑xxx1和xxx2的或
test ! xxx 逻辑xxx的反
例子
echo -e "Please input a filename, I will check the file's type and permission. "
read -p "Input a filename" filename
test -z $filename && echo "You must input a filename" && exit 0
test ! -e $filename && echo "The file${filename} does not exits" && exit 0
test -f $filename && filetype="regular file"
test -d $filename && filetype="${filetype} directory"
test -r $filename && perm="readable"
test -w $filename && perm="${perm} writable"
test -x $filename && perm="${perm} executable"
echo "The filename: ${filename} is a ${filetype}"
echo "And the permissions are: ${perm}"

  1. 利用判断符号 []

[] 两端需要有空格,用来区分bash中的中括号
使用[],最好注意
-- 中括号[]内的每个组件都需要空格来分隔
-- 中括号[]中的变量,最好用双引号" "括起来
-- 中括号中的常数,最好都以单或双引号括起来
用法
[ -z $varname ] -> 判断字符串是否为空
[ $varname1 == $varname2 ] -> 判断varname1是否等于varname2
例子
read -p "Please input (Y/N):" yn
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0
[ "$yn" == "n" -o "$yn" == "N" ] && echo "Oh, interrupt" && exit 0
echo "I do not know what your choice is" && exit 0

$号表达式 用法

  1. ${} 取出变量的值,同时也可以对变量的值作一定的处理
  2. $() 先执行括号内的指定,得到指定通过stdout输出的内容
  3. $(()) 进行算数运算,获得算数运算的结果。

shell 参数与变量

shell参数

  1. $0 -> 代表指令本身
  2. $1 -> 代表第一个参数
  3. $2 -> 代表第二个参数
  4. $3 -> 代表第三个参数

shell特殊变量

  1. $# -> 代表后接的参数个数
  2. $@ -> 代表"$1" "$2" "$3" "$4"..
  3. $* -> 代表 $1c$2c$3c$4..

参数号码偏移

shift -> 代表左移,代表在左边拿掉几个参数

shift n -> 左移几位,在左边拿掉几个参数

shell结构

条件判断语句

-- 第一种方式 --
if[ 条件判断式 ]; then
当条件成立时执行的指令
fi
可以用[ "$yn" == "Y" ] || [ "$yn" == "y" ] 代替 [ "$yn" == "Y" -o "$yn" == "y" ]

-- 第二种方式 --
if[ 条件判断式 ]; then
当条件成立时执行的指令
else
当条件不成立时执行的指令
fi

-- 第三种方式 --
if [ 条件判断式1 ]; then
当条件1成立时执行的指令
elif [ 条件判断式2 ]; then
当条件2成立时执行的指令
elif [ 条件判断式3 ]; then
当条件3成立时执行的指令
else
当所有条件不成立时执行的指令
fi

case语句

case $变量名称 in
"第一个比对值")
符合第一个比对值的执行命令。
;;
"第二个比对值")
符合第二个比对值的执行命令。
;;
"第三个比对值")
符合第三个比对值的执行命令。
;;
*)
exit 1
esac

函数function功能

function 函数名称(){

}
-- 函数写在最前面
-- 函数內建变量
---- $0代表函数名
---- $1代表第一个变量等等

循环

---- 第一种 while do done ----
while [ condition ]
do
while内的执行指令
done

---- 第二种 until do done ----
until [ condition ]
do
until内的执行指令
done
--这种循环方式与while相反,当条件满足时就结束

---- 第三种 for do done ----
for var in con1 con2 con3...
do
for内的执行命令
done
-- var不用加$号

---- 另一种for ----
for(( 初始值;限制值;执行步阶))
do
for内的执行指令
done
-- 例如 for(( i=1;i<100;i++ ))

shell的 debug

sh [-nvx] script.sh
-n 不执行script,只查询语法问题
-v 执行script前先输出script的内容
-x 将运行过的script内容输出到屏幕

散乱知识

/etc/init.d 中的脚本是linux服务启动的接口

/etc/init.d/syslogd restart 重新启动系统注册表档

/etc/rc.d/rc.local 开机加载程序项目的数据

shell脚本

  1. 从上到下,从左往右分析执行

  2. 空格和tab都会被忽略

  3. 按下Enter(CR)就会开始执行该命令

  4. 通过反斜杠可以跳脱Enter,进行换行书写

  5. #号进行批注

  6. shell脚本文件执行必须具备rx权限

  7. 可通过绝对路径/bin/script.sh和相对路径的方式执行

  8. 可以通过PATH路径搜寻到而执行

  9. 可以通过bash程序来执行 bash shell.shsh shell.sh

通过第一行的 #!/bin/bash 宣告这个脚本使用的shell名称

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

通过exit 0 回传0值给系统。

命令

  1. echo (在屏幕输出)

echo -e 对转移字符进行解析
echo -n 不换行,在同一行显示

  1. netstat (查询目前主机开启的网络服务端口)

netstat -tuln 查看目前主机启动的服务
-- Local Address字段 --
---- IP:port
---- 127.0.0.1代表针对本机开放
---- 0.0.0.0或:::代表对整个Internet开放
---- 80端口 -> WWW
---- 22端口 -> ssh
---- 21端口 -> ftp
---- 25端口 -> mail
---- 111端口 -> RPC (远程过程调用)
---- 631端口 -> CUPS (打印服务功能)

  1. id (查看用户的uid gid等)

id username

  1. finger (查看用户的特殊参数)

finger username

  1. ping (网络侦测)
  2. seq (用于产生序列)

seq 1 100 产生1 2 .. 100的序列,可用于for循环

  1. whoami (查看自己的用户名)

参考

鸟哥的Linux私房菜 第三版

原文地址:https://www.cnblogs.com/smallpi/p/7351901.html