shell简单练习

来源:互联网 发布:局域网斗地主软件 编辑:程序博客网 时间:2024/06/06 15:37

求1~100的和

#!/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 is ===> $s" //输出



用下面的程序认识shell的默认变量

#!/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 "too few parameter,stop here." && exit 0
echo "the whole parameter is ==> '$@'" #所有参数
echo "the 1st parameter is      ==> $1"
echo "the 2nd parameter is      ==> $2"


输入命令sh test.sh one two three

有如下结果:
the script name is    ==> test.sh
total parameter number is    ==> 3
the whole parameter is ==> 'one two three'
the 1st parameter is    ==> one
the 2nd parameter is    ==> two 

其他程序,待续。。。