Home

zhangyiqun

Thoughts, stories and ideas.

Notes Blog Archives About
24 Mar 2009

shell编程的基本框架

基础

Case

#!/bin/bash

case $1 in

    1)

        echo baidu.com

        ;;

    2)

        echo google.com

        ;;

    *) 

        echo zhangyiqun.cn

        ;;

esac

 

continue

#!/bin/bash

for ((i=1;i<=1000;i++))

do

    if ((i%5 == 0))

    then

        continue

    fi

    echo $i

done

 

制作死循环并设置终止信号

#!/bin/bash

trap ‘echo bye;exit 0;’ 1 2 9 15 #接受信号

while :

do

    sleep 1

done

while break

#!/bin/bash

num=1

while :

do

    echo $num

    ((num++))

    if ((num == 10))

    then

        break

    fi

done

for in

#!/bin/bash

for num in ‘seq 1 10’

do

    echo $num

done

妙用

#!/bin/bash

for i in ‘locate -r /passwd

do

    echo $i

    ls -l $i

done

for的常规用法

#!/bin/bash

 

for (( i=1; i<=10; i++))

do

    echo $i

done

test文件

#!/bin/bash

if test -f /etc/fstab

then

    echo YES

fi

test 文件夹

#!/bin/bash

if [ -f $1 ]

then

    echo $1 is a file

elif [ -d $1 ]

then

    echo $1 is dir

fi

read

#!/bin/bash

read -p “name” username

read -s -p “password” passwd

echo

case $username@$passwd in

    jean@123)

        echo YES

        ;;

    *)

        echo NO

        ;;

esac

Notes Blog Archives About