【linux】学习6

鸟哥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 0
test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0
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 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0
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 1
              ;;
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很有用

posted @ 2014-11-19 15:54  匡子语  阅读(225)  评论(0编辑  收藏  举报