shell简单练习

求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 

其他程序,待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值