鸟哥13章的东西

shell script速度较慢,适合用于系统管理,但不适合处理大量数值运算

var=$((运算内容)) 可以用来做变量的加减乘除求余运算

total=$(($firstnum*$secnu))

declare -i total="$firstnum*$secnu"

上面两句功能一样,建议用第一种

echo -e "\nYour full name is: $firstname $lastname"

-e表示后面 \表示转义,例子表示了在echo中显示两个变量的方法

file1=${filename}${date1}

一个变量是另两个变量的连接的写法

执行script

sh scriptname 在子进程中执行,变量不会出现在父进程

source scriptname 在父进程中执行,变量会出现在父进程

test :测试

test -e /test && echo "exist" || echo "Not exist"  测试文件名/test是否存在 目录文件都可以

test -f sh03.sh && echo "exist" || echo "Not exist" 测试文件是否存在 必须是文件

test -d abc && echo "exist" || echo "Not exist" 测试目录是否存在 必须是目录

下面代码测试文件是否存在 以及文件的执行权限

read -p "Please input a filename: " filename
test -z $filename && echo "You MUST input a filename." && exit
test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit
test -f $filename && filetype="regulare file"
test -d $filename && 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"

[]: 表判断,但是挨着括号的两端必须都是空格

[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, CONTINUE" && exit 0 判断yn等于Y或y,任意一个都返回true

脚本后面带参数:

$0 代码文件名 $1代码后面第一个参数  $2代码后面第二个参数....

$@ 代表除文件名之外的所有参数  $# 表后面接的参数个数

下面例子输入少于两个参数会退出 会显示所有参数和第一第二个参数

echo "The script name is --> $0"
echo "Total parameter number is --> $#"
[ "$#" -lt ] && echo "The number of parameter is less than 2. Stop here." && exit
echo "Your whole parameter is --> '$@'"
echo "The 1st parameter --> $1"
echo "The 2nd parameter --> $2"

shift num: 移除后面num个变量

条件判断式

if [] ; then

elif []; then

else

fi

if [ "$1" == "hello" ]; then
echo "Hello, how are you?"
elif [ "$1" == "" ]; then
echo "You Must input parameters, ex> {$0 someword}"
else
echo "The only parameter is 'hello', ex> {$0 hello}"
fi

netatat -tuln 获得目前主机启动的服务

80:www

22:ssh

21:ftp

25:mail

检测常见端口

echo "Now, I will detect your linux server's services!"
echo -e "The www, ftp, ssh and mail will be detect!\n" testing=$(netstat -tuln | grep ":80 ")
if [ "$testing" != "" ]; then
echo "WWW is running in your system."
fi testing=$(netstat -tuln | grep ":22 ")
if [ "$testing" != "" ]; then
echo "SSH is running in your system."
fi testing=$(netstat -tuln | grep ":21 ")
if [ "$testing" != "" ]; then
echo "FTP is running in your system."
fi testing=$(netstat -tuln | grep ":25 ")
if [ "$testing" != "" ]; then
echo "MAIL is running in your system."
fi

case 条件判断

case $变量名称 in
"第一个变量内容“
程序段
;;
"第二个变量内容“
程序段
;;
*)
    其他变量内容的程序段
   exit
;;

esac

函数:

function fname(){
  程序段
}    

后面接内置参数和shell的内置参数方法一样 也是$1 $2 ...

循环:

买足条件就循环

while [ condition ]
do
  程序段
done

满足条件就结束循环

until [ condition ]
do
  程序段
done

for循环

for var in con1 con2 con3 ...
do
  程序段
done
for ((初始值;限制值;执行步长))
do
  程序段
done

调试script

sh [-nvx] scriptname.sh

-n 不执行,仅检查语法

-v 执行前把script内容输出到屏幕

-x 将使用到的script内容输出到屏幕   debug很有用

最新文章

  1. IOS UITableview代理方法总结
  2. PHP数组合并 array_merge 与 + 的差异
  3. Jquery.Datatables td宽度太长的情况下,自动换行
  4. Java使用默认浏览器打开指定URL
  5. HDU1071 The area 【积分】
  6. 实现BUG自动检测 - ASP.NET Core依赖注入
  7. Android------自定义ListView详解
  8. HDU1035 Robot Motion
  9. Activity切换动画---点击哪里从哪放大
  10. 51 nod 1188 最大公约数之和 V2
  11. 为django项目配置celery的后台启动
  12. Java 使用 Redis存储系统
  13. SpringBoot 2.0集成spring-data-elasticsearch
  14. rhadoop linear regression 问题
  15. JS--理解call、apply和bind
  16. 一道cf水题
  17. Linq EF 添加数据执行事务处理
  18. HTML5 Geolocation API地理定位整理(二)
  19. android适配pad和部分手机底部虚拟按键+沉浸式状态栏
  20. JS函数的几种用法

热门文章

  1. Git工作流总结
  2. C#5.0 特性
  3. BSA混合分离分析法
  4. 大数据之tachyon(未完版)
  5. post提交表单
  6. MySQL监控系统MySQL MTOP的搭建(转VIII)
  7. keepalived安装
  8. [转载]MySQL5.5 配置文件 my.ini 1067错误
  9. PHP四种基础算法详解
  10. Java 重写(Overriding)和重载(Overloading)