`

linux 之shell

 
阅读更多

 

1.初认识shell

定义bash的类型    #!/bin/bash

 

查询文件中包含obb字符串的行数是否为3,注意将命令行赋值给变量要加~上面的点而不是单引号

   row_count=`cat testFile | grep obb | wc -l`
   if [ ${row_count} -eq 3 ];
        then echo "true";
  else
        echo "false"; 
  fi
 

   查询文件中某个单词的数量
   grep -o 的意思为将匹配的单独起一行

 

 

   row_count2=`cat test* | grep -o add | wc -l`

   echo ${row_count2};

 

   定义变量

      fruit=apple;

      count=5;

      echo "$count ${fruit}";

 

 

 

echo –e 处理特殊字符

以下省略注释

 

#!/bin/bash

#program:

#       this program shows "hello world" in your screen

#histroy:

#2012/10/8 labreeze first-release

 

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

export PATH

echo -e  "hello world\a\n"

exit  0

 

 

2.定义变量 输出变量

#!/bin/bash

 

#var

var1="apple"

var2=orange

 

echo ${var1},$var1

echo "${var1},$var1"

 

echo ${var2},$var2

echo "${var2},$var2"

 

#array

users=(one two three)

#len=${#users[*]}

len=${#users}

echo $len

exit 0

 

 

 

3.终端读取变量

read –p

-p 标志的文件结束符引起该进程的清除,因此产生另外一个进程。

 

#!/bin/bash

 

#get name

#the first line will wrap but the second is not

echo  "type into your first name:"

read firstName

read -p "type into your last name:"  lastName

#print name

echo -e "your full name is ${firstName}${lastName}"

echo -e "your full name is $firstName$lastName"

exit 0

 

4.if  else

参考http://blog.csdn.net/flowingflying/article/details/5069646

#下面请至于[ ... ],即[后面有一个空格,]前面有一空格,另外$mystack用双引号,表示这代表的是一个字符串。注意到then不放在下一行,与if放在用一行,用;来隔开。

#注意if中空格的问题 双括号则不用考虑空格的问题

If写法

[ "$choice" == "Y" -o "$choice" == "y" ]

if [ "$yn" == "Y" ] || [ "$yn" == "y" ];then

if(("$yn" == "Y")) || (("$yn" == "y"));then

 

 

#!/bin/bash

 

read -p "please input Y/N:" choice

echo $choice

[ "$choice" == "Y" -o "$choice" == "y" ] && echo "yes continue......" && exit 0

[ "$choice" == "N" -o "$choice" == "n" ] && echo "no exit......" && exit 0

echo "i donot judge your choick" && exit 0

 

read -p "type into Y/N:" yn

if [ "$yn" == "Y" ] || [ "$yn" == "y" ] ; then

         echo "yes .....!"

elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then

         echo "stop.....!"

else

         echo "system cannot konw the char"

fi

exit 0

 

ifelse 实现的监测主机的端口是否开启

#!/bin/bash

#Program:

#       this program is to detect servers port

#History:

#       2012-10-09 labreeze First-release

 

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

export PATH

 

#1.give some preparation

echo "now, i will detect your linux server's service!"

echo -e "the WWW,FTP,SSH,and MAIL will be detect!\n"

 

#2.begin to detect and give some information

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 "MAIN is running in your system"

fi

 

exit 0

 

 

 

 

5.function方法的使用

$0输出脚本名称$1,$2..为传入方法的参数

注意学习方法的返回值是如何获取的

 

#!/bin/bash

function getSum(){

         echo $0,$1,$2;

         return $(($1+$2));

}

 

getSum 2 3;

getSum 3 4

sum=$?;

echo "sum:"$sum;

 

function的全局变量和局部变量

#function2 

declare num=1000;

function globalFun(){

         ((num++));

}

 

function localFun(){

         local num=10;

         ((num++));

}

 

globalFun;

echo $num;

 

localFun;

echo $num;

 

 

 

 

 

 

 

 

 

 

6.循环(for while)

#!/bin/bash

 

#while

i=1;

while(($i<100));do

         if(($i%3==0));then

                   echo $i

         fi

         i=$(($i+1))

done

 

#for

for((i=100;i<200;i++));do

         if(($i%3==0));then

                   echo $i

         fi

done

 

#getSum from 1 to 100

sum=0;

for((i=0;i<100;i++));do

         sum=$(($sum+$i)); 

done

echo $sum;

 

users=(one tow three four)

size=${#users[*]}

echo $size

for((i=0;i<$size;i++));do

         echo ${users[$i]}

done

 

 

 

7.从输入的shell脚本中读取参数 如:start/stop/restart等

$0脚本名称 $#参数的数量(不包括0)  $@显示所有的参数(不包括0)

 

#!/bin/bash

#Program:

#       this program is to practise how to accept parameter when run sh

#History

#       2012-10-09 labreeze First-release

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

export PATH

 

echo "the sh-name is $0"

echo "the number of parameter is $#"

echo "all parameters is as follows $@"

echo "$1"

echo "$2"

 

exit 0

 

8.shell debug

sh –n  +filename  不执行脚本仅仅检查语法问题

sh –v  +filename  在执行脚本前先将脚本内容给输出来

sh –x  +filename  将用到的脚本内容输出来

 

 

 

 

0
5
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics