shell编程部分

shell

shell文件  ./当前目录下

 

 

Shell编程

-n string 如果字符串不为空则结果为真

-z string 如果字符串为null(一个空串)则结果为真

expression1 -eq expression2 如果两个表达式相等则结果为真

expression1 -ne expression2 如果两个表达式不等则结果为真

expression1 -gt expression2 如果expression1大于expression2则结果为真

expression1 -ge expression2 如果expression1大于等于expression2则结果为真

expression1 -lt expression2 如果expression1小于expression2则结果为真

expression1 -le expression2 如果expression1小于等于expression2则结果为真

! expression 如果表达式为假则结果为真,反之亦然

文件条件测试 结  果

-d file 如果文件是一个目录则结果为真

-e file 如果文件存在则结果为真。要注意的是,历史上-e选项不可移植,所以通常使用的是-f选项

-f file 如果文件是一个普通文件则结果为真

-g file 如果文件的set-group-id位被设置则结果为真

-r file 如果文件可读则结果为真

-s file 如果文件的大小不为0则结果为真

-u file 如果文件的set-user-id位被设置则结果为真

-w file 如果文件可写则结果为真

-x file 如果文件可执行则结果为真

 

例一:echo printf

#!/bin/sh

if test -f /bin/bash;then

  echo "file /bin/bash exists"

  echo -e "OK! \c" # -e 开启转义 \c 不换行

echo "It is a test" > myfile   //显示结果定向至文件

fi

if [ -d /bin/bash ]

then

  echo "/bin/bash is a directory"

elif

then echo “condition2” 

else

  echo "/bin/bash is NOT a directory"

Fi

printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg  

printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234

printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543

printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876

例二:

#!/bin/sh

echo "Is it morning? Please answer yes or no"

read timeofday

if [ $timeofday = "yes" ]; then            if和then放一行的话要加;

  echo "Good morning"

else

  echo "Good afternoon"

fi

 

exit 0

例子三  循环

val=`expr $a + $b`

echo "a + b : $val"

val=`expr $a - $b`

echo "a - b : $val"

(1)while

i=3

while [ i -lt 10] //小于

do

  i=`expr $i + 1`

  echo i

done

  1. for

for loop in 1 2 3 4 5  //loop 的价值

do

    echo "The value is: $loop"

done

 

例子四 case

#!/bin/sh

echo "Is it morning? Please answer yes or no"

read timeofday

case "$timeofday" in

    yes)  echo "Good Morning";;

no )  echo "Good Afternoon";; //注意是两个;

    y  )  echo "Good Morning";;

    n  )  echo "Good Afternoon";;

    *  )  echo "Sorry, answer not recognized";;

esac

exit 0

case "$timeofday" in

 yes | y | Yes | YES )      echo "Good Morning";;

    n* | N* )                  echo "Good Afternoon";;

* )                        echo "Sorry, answer not recognized";;

esac

case "$timeofday" in

    yes | y | Yes | YES )

           echo "Good Morning"

           echo "Up bright and early this morning"

           ;;

    [nN]*)

           echo "Good Afternoon"

           ;;

    *)

           echo "Sorry, answer not recognized"

           echo "Please answer yes or no"

           exit 1

           ;;

esac

 

函数

foo() {

 

    local sample_text="local variable" //local 声明局部变量

    echo "Function foo is executing"

    echo $sample_text

}

echo "script starting"

echo $sample_text

foo

  1. 返回值

func(){

return 1;

}

func

echo $?

 

$0 当前脚本的文件名

$n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。

$# 传递给脚本或函数的参数个数。

$* 传递给脚本或函数的所有参数。

$@ 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到。

$? 上个命令的退出状态,或函数的返回值。

$$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。

 

命令:

(1)Break; continue;  

  1. :表示空命令相当于true   if [ -f fred ]; then  :
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值