shell 编程例子—第9章源码

28 篇文章 0 订阅
shell 编程例子—第9章源码


++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example
cd /root/txtfile/
echo "----------------" >> bac_ztg.txt
date >> bac_ztg.txt
cat ztg* >> bac_ztg.txt
echo "" >> bac_ztg.txt



++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for break
echo 请在命令后输入一个数字n,然后会得到1+2+...+n之和
if [ $# -ne 1 ]
then
    echo 请输入并且仅输入一个数字
    exit 1
fi
TOTAL=0
VAR=$1
while true
do
    TOTAL=$[TOTAL+VAR]
    VAR=$[VAR-1]
    if [ $VAR -eq 0 ]
    then
        break
    fi
done
echo $TOTAL



++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for case
cd /root/txtfile
echo please give your choice to display a file:
echo "1) display ztg1.txt"
echo "2) display ztg2.txt"

echo enter your choice:
read var

case $var in
    1) cat ztg1.txt;;
    2) cat ztg2.txt;;
    *) echo wrong;;
esac



++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for continue
echo 请在命令后输入一个数字n,然后会得到1-n之间奇数之和
if [ $# -ne 1 ]
then
    echo 请输入并且仅输入一个数字
    exit 1
fi
TOTAL=0
VAR=$1
while true
do
    if [ $VAR -eq 0 ]
    then
        break
    elif [ $[VAR%2] -eq 0 ]
    then
        VAR=$[VAR-1]
        continue
    elif true
    then
        TOTAL=$[TOTAL+VAR]
        VAR=$[VAR-1]
    fi
done
echo $TOTAL



++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for for
for num in 1 2 3 4 5 6
do
    echo $num的平方:
    expr $num \* $num
#   { echo $num的平方:;expr $num \* $num;}
done



++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for function
display()
{
    if [ $# -ne 1 ]
    then
        echo 请在命令后输入一个文件名或目录名
        exit 1
    fi
    if [ -d $1 ]
    then
        dir $1
    elif [ -f $1 ]
    then
        cat $1
    elif true
    then
        echo 没有该文件名或目录名
    fi
    echo 请在命令后输入一个文件名或目录名
}
display $1




++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for if
cd /root/txtfile/
if [ -f ztg1.txt ]
then
    echo ztg1.txt is a file:
    cat ztg1.txt
elif [ -d /root/txtfile ]
then
    echo in /root/txtfile is:
    dir /root/txtfile
fi



++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for shift
if [ $# -eq 0 ]
then
    echo no number!
    exit 1
fi

TOTAL=0

until [ $# -qe 0 ]
do
    TOTAL=expr $TOTAL + $1
    shift
done

echo $TOTAL


++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for shift_add
if [ $# -eq 0 ]
then
    echo no number!
    exit 1
fi
export TOTAL=0
until [ $# -eq 0 ]
do
    TOTAL=$[TOTAL+$1]
    shift
done
echo $TOTAL



++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for until
cd /root/txtfile
echo 请输入文件名:
read FNAME
echo 请输入文件内容,输入end!退出:
read VAR
until [ $VAR = end! ]
do
    echo $VAR >> $FNAME
    echo 请输入文件内容,输入end!退出:
    read VAR
done



++++++++++++++++++++++++++++++++++++++++++


#!/bin/bash
#this is a example for while
echo "请输入数字(大于等于100将退出):"
read VAR
while [ $VAR -lt 100 ]
do
    echo $VAR的平方:
    expr $VAR \* $VAR
#   { echo $VAR的平方:;expr $VAR \* $VAR;}
    echo "请输入数字(大于等于100将退出):"
    read VAR
done



++++++++++++++++++++++++++++++++++++++++++




<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(101) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值