欢迎访问www.showerlee.com, 您的支持就是我前进的动力.

[SHELL]shell scripts笔记(1)

showerlee 2013-08-27 18:57 SHELL 阅读 (7,041) 抢沙发

自己没事总结了点基础的东西,以备不时之需。

一.判断式:

1.判断符号 &&及||

检测test是否存在:

# test -e /haha

注:test是一个判断文件属性的命令,-e参数判断目录是否存在,执行结果不会显示任何信息,但最后我们可以透过$?或&&及||来展现整个结果

例如:

# test -e /haha && echo "exist" ||echo "not exist"

注: -f参数判断是否为文件,-d判断是否为目录

实例:

1).让使用者输入一个文件名,判断这个文件名是否存在,若不存在给予一个filename does not exist的信息,并中断程序。

2).若这个文档存在,则判断他是文档还是目录,结果输出filename is regular file 或 filename is directory

3).判断执行者对这个文件或目录所拥有权限,并输出权限目录:

# vi sh05.sh

--------------------

#!/bin/bash

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

export PATH

# 判断是否输入字符串:

echo -e "Please input a filename,I will check the filename's type and permission. \n\n"

read -p "Input afilename:"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"

--------------------

2.判断符号[]

判断$HOME变量是否为空:

# [ -z "$HOME" ];echo $?

注: 中括号因为用在很多包括通配符,正则表达式中,所以用在shell判别式时,为了与其他用法区分,必须在括号两端用"空格"间隔。

判断两个变量是否相同

# [ "$HOME" == "$MAIL" ]

实例

1.当执行一个程序时,程序会让用户选择Y或N

2.如果用户输入Y或y时,就显示OK continue

3.如果用户输入n或N时,显示oh,interrupt!

4.如果输入其他字符,就显示 I don't know what fucking your choice is

利用[] && ||实现

# vi sh06.sh

---------------------

#!/bin/bash

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

export PATH

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 don't know what fucking your choice is" && exit 0

----------------------

传参:

实例:

1.程序文件名为何?

2.共有几个参数?

3.若参数个数小雨2则告知使用者参数数量太少

4.全部的参数为何?

5.第一个参数为何?

6.第二个参数为何?

# vi sh07.sh

---------------------

#!/bin/bash

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

export PATH

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 2st parameter  ==> $2"

----------------------

条件判断:

利用if...then

sh06修改版:

# vi sh08.sh

---------------------

#!/bin/bash

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

export PATH

read -p "Please input (Y/N):" yn

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

        echo "OK,continue"

        exit 0

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

         echo "oh,interrupt!"

         exit 0

       else

       echo "I don't know what fucking your choice is"

       exit 0

       fi

fi

----------------------

实例1:

1.判断$1是否为hello,如果是,显示 hello,how are you?

2.如果没加参数,提示用户必须使用参数

3.如果加入的参数不是hello,就提示用户只能用hello作为参数

# vi sh09.sh

---------------------

#!/bin/bash

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

export PATH

if [ "$1" == "hello" ];then

echo "Hello,how are you?"

elif [ "$1" == "" ];then

echo "You must input parameters,example:{$0 someword}"

else

echo "The only parameter is 'hello',example:{$0 hello}"

fi

---------------------

实例2:

判断主机常见21,22,25,80是否开启

# vi sh10.sh

---------------------

#!/bin/bash

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

export PATH

PORT=$(netstat -tunl |grep "$1"| sed -n '1p' | awk '{print $4}'| sed 's/^.*://g')

VAR=$(echo "$1" |bc 2>/dev/null)

if [ "$1" == "" ]; then

       echo "Please input a port.example:{sh $0 80}"

       elif [ "$VAR" != "$1" ];then

       echo "Please input the right port.example:{sh $0 80}"

       elif [ "$PORT" == "21" ];then

       echo "ftp is running in your system"

       elif [ "$PORT" == "22" ];then

       echo "ssh is running in your system"

       elif [ "$PORT" == "25" ];then

       echo "smtp is running in your system"

       elif [ "$PORT" == "80" ];then

       echo "www is running in your system"

       else

       echo "the port:$1 is not running"

fi

----------------------

利用if...esac判断

sh09修改版:

# vi sh11.sh

---------------------

#!/bin/bash

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

export PATH

case $1 in

"hello")

       echo "Hello,how are you?"

       ;;

"")

       echo "You must input parameters,example:{sh $0 someword}"

       ;;

*)

       echo "Usage:{sh $0 hello}"

       ;;

esac

---------------------

实例2:

# vi sh12.sh

---------------------

#!/bin/bash

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

export PATH

case $1 in

 "one")

echo "Your choice is ONE"

;;

 "two")

echo "Your choice is TWO"

;;

 "three")

echo "Your choice is "THREE"

;;

  *)

echo "Usage $0 {one|two|three}"

esac

---------------------

function功能

实例1:

# vi sh13.sh

---------------------

#!/bin/bash

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

export PATH

function printit(){

echo -n "Your choice is" # -n表示不断行在同行显示

}

echo "This program will print your selection!"

case $1 in

 "one")

printit;echo $1 | tr 'a-z' 'A-Z' # 将参数做大小写转换

;;

 "two")

printit;echo $1 | tr 'a-z' 'A-Z'

;;

 "three")

printit;echo $1 | tr 'a-z' 'A-Z'

;;

  *)

echo "Usage $0 {one|two|three}"

;;

esac

--------------------------

实例1变种:

# vi sh14.sh

---------------------

#!/bin/bash

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

export PATH

function printit(){

echo  "Your choice is $1"

}

echo "This program will print your selection!"

case $1 in

 "one")

printit 1 # printit 后可接参数

;;

 "two")

printit 2

;;

 "three")

printit 3

;;

  *)

echo "Usage $0 {one|two|three}"

;;

esac

--------------------------

while do done,until do done(不定循环)

# vi sh15.sh

---------------------

#!/bin/bash

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

export PATH

while [ "$yn" != "yes" -a "$yn" != "YES" ]

do

       read -p "Please input yes/YES to stop this program:" yn

done

echo "OK,you input the correct answer."

----------------------

# vi sh16.sh

---------------------

#!/bin/bash

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

export PATH

until [ "$yn" == "yes" -o "$yn" == "YES" ]

do

       read -p "Please input yes/YES to stop this program:" yn

done

echo "OK,you input the correct answer."

----------------------

计算1+2+...+100=?

# vi sh17.sh

---------------------

#!/bin/bash

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

export PATH

s=0

i=0

while [ "$i" != "100" ]

do

i=$($i+1)

s=$($s+$i)

done

echo "The result of '1+2+3+...+100'=" $s

-----------------------

for...do...done (固定循环)

# vi sh18.sh

---------------------

#!/bin/bash

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

export PATH

for animal in dog cat elephant

do

echo "There are ${animal}s..."

done

---------------------

# vi sh19.sh

---------------------

#!/bin/bash

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

export PATH

users=$(cat /etc/passwd | awk 'BEGIN {FS=":"} {print $1}')

for username in $users

do

id $username

# finger $username

done

---------------------

实例1:

利用for循环ping网内主机状态

# vi sh20.sh

---------------------

#!/bin/bash

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

export PATH

network="172.24.30"

for host in $(seq 1 254)

 do

       ping -c 1 -w 1 ${network}.${host}&> /dev/null && result=1 || result=0

 case "$result" in

   "1")

       echo "Server ${network}.${host} is UP."

       ;;

   "0")

       echo "Server ${network}.${host} is DOWN."

       ;;

 esac

done

-----------------------

实例2:

让用户输入某个文件名,找出该目录内的文件名的权限

# vi sh21.sh

---------------------

#!/bin/bash

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

export PATH

read -p "Please input a directory:" dir

if[ "$dir" == "" -o ! -d "$dir" ];then

echo "The $dir is NOT exist in your systemn."

exit 1

fi

filelist=$(ls $dir)

for filename in $filelist

do

perm=""

test -r "$dir/$filemane" && perm="$perm readable"

test -w "$dir/$filename" && perm="$perm writable"

test -x "$dir/$filename" && perm="$perm executable"

echo "The file $dir/$filename's permission is ${perm}."

done

-----------------------

# vi sh22.sh

---------------------

#!/bin/bash

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

export PATH

read -p "Please input a number,I will count for 1+2+..+{your_input}:" nu

s=0

for((i=1;i<=$nu;i=i+1))

do

       s=$(($s+$i))

done

echo "The result of '1+2+3+...+$nu' is ===> $s"

----------------------

debug

# sh -x sh22.sh

习题

建立一个script,当你执行script时,该script可以显示:

1.你目前的身份(whoami)

2.你锁在目录

# vi test01.sh

---------------------

#!/bin/bash

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

export PATH

echo -e "Your name is $(whoami)"

echo -e "The current directory is $(pwd)"

---------------------

让用户输入一个数字,程序可以由1+2+3+....一直累加到用户输入的数字为止

# vi test02.sh

---------------------

#!/bin/bash

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

export PATH

read -p "Please input a number,I will count for 1+2+...+{your_input}:" nu

s=0

for (( i=1;i<=$nu;i=i+1 ))

do

       s=$(($s+$i))

done

echo "The answser of "1+2+...+$nu =" $s"

---------------------

写一个程序

1.先查看/root/test/logical这个文件是否存在

2.若不存在,则建立一个文档,使用touch建立,建立完成后离开

3.如果存在,判断是否为文件,若为文件则删除后建立一个目录,名为logical,之后离开

4.如果存在,则移除此目录

# vi test02.sh

---------------------

#!/bin/bash

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

export PATH

cd /root/test

if [ ! -e logical ];then

       touch logical

       echo "Just make a file logical"

       exit 1

elif [ -e logical ]&&[ -f logical ];then

       rm -rf logical && mkdir logical

       echo "Just make a directory logical"

       exit 1

elif [ -e logical ]&&[ -d logical ];then

       rm -rf logical

       echo "Just delete a directory logical"

       exit 1

fi

-----------------------

写一个程序,将/etc/passwd的第一栏取出,而每一栏都以一行字符串[The 1 account is "root"]来显示,那个1表示行数。

# vi test04.sh

---------------------

#!/bin/bash

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

export PATH

var=$(cat /etc/passwd | awk 'BEGIN {FS=":"} {print $1}')

for account in $var

do

declare -i i=$i+1

echo "The $i account is $account"

done

---------------------

----------------------

#!/bin/bash

awk 'BEGIN {FS=":"} {printf "The %s account is %s\n",NR,$1}' /etc/passwd

----------------------

----------------------

#!/bin/bash

awk 'BEGIN {FS=":"} {print "The " NR " account is "$1}' /etc/passwd

----------------------

正文部分到此结束
版权声明:除非注明,本文由(showerlee)原创,转载请保留文章出处!
本文链接:http://www.showerlee.com/archives/597

继续浏览:SHELL

还没有评论,快来抢沙发!

发表评论

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif