shell简单编程

  • 赋值与循环分支
$x 用于取变量x的值
$1 表示第一个参数,$#表示参数个数,shift移动一个参数。
let i=$x+$y,为整数操作赋值,所以必须有两个以上操作数.当只有一个操作数时使用,i=$x.

#!/bin/bash
echo "please input num:"
read num
echo "the num is $num"
#声明变量,不能有空格
sum=0
i=0
# while循环,需要留出空格
while [ $num -gt $i ]
do
  let mo=$i%2
  # 开始条件需要留出空格
  # then,else 要单独留出空行
  if [ $mo -eq 0 ]
  then
    3 赋值不能有空格
    let sum=$sum+$i
  else
    touch $i
    tar cvf - `find . -mtime -1 -type f -print` > x.tar
  # 结束条件
  fi
  let i=$i+1
# 结束循环
done
echo "the sum is $sum"
  • for循环以及数组
#!bin/bash
echo "my student number is :1133710513, my name is zhiwei huang"
city[1]=Beijing
city[2]=Shanghai
city[3]=Wuxi
city[4]=Wenzhou
city[5]=Berkeley
# for z in 1 2 7
for i in ${city[*]}
do
        echo $i
done
  • 打印
#!/bin/bash
echo "my student number is :1133710513, my name is zhiwei huang"
i=0
for ((k=0;k<10;k++))
do
        for((j=0;j<=k;j++))
        do
                echo -n "$j "
        done
        echo " "
done
posted @ 2016-05-27 14:41  fcat  阅读(137)  评论(0编辑  收藏  举报