1   在终端下运行程序,首先清屏,然后提示:“Input a file or directory name, please!”。从键盘输入一个字符串(如:xxx),如果该字符串是目录,则显示:“xxx is a directory.”;如果该字符串是文件(如:xxx),则显示:“xxx is a regular file.”;如果既不是目录也不是文件,则显示:“This script cannot get the file/directory xxx information!”。

#! /bin/bash
echo "Input a file or directory name, please!"
read file
if [ -d $file ]
then 
echo "$file is a directory."
elif [ -f $file ]
then
echo "$file is a regular file."
else
echo "This script cannot get the file/directory xxx information!"
fi

2  在终端下运行程序,首先清屏,然后提示:“Input your age!”。从键盘输入你的年龄(如:22),如果年龄在20-29,则输出“Please go to room 101!”;如果年龄在30-39,则输出“Please go to room 201!”;如果年龄在40-49,则输出“Please go to room 301!”;如果年龄在50-59,则输出“Please go to room 401!”;如果年龄在60-69,则输出“Please go to room 501!”;如果年龄不在上述范围,则输出“Please wait at the door!”;

 

#! /bin/bash
clear
echo "please input your age!"
read age
 a=` expr $age / 10 `
case $a in
    2)
     echo "Please goto room 101"
    ;;
    3)
     echo "Please goto room 201"
    ;;
    4)
     echo "Please goto room 301"
    ;;
    5)
     echo "Please goto room 401"
    ;;
    6)
     echo "Please goto room 501"
    ;;
    *)
     echo "Please wait at the door!"
    ;;
esac

3  程序中循环列表为某一目录下的所有子目录和文件,运行程序,列出该目录下的所有文件。(这个需要使用 bash ***.sh执行,上面两个可以直接sh ***.sh)

#! /bin/bash
function ergodic() {
    for file in ` ls $1 `
    do 
        if [ -d $1"/"$file ]
        then
            ergodic $1"/"$file
        else
            echo $1"/"$file
        fi
    done
}
INIT_PATH="./test"
ergodic $INIT_PATH

 

 4 运行文件时,显示文件后所带的参数。例如所编辑的文件名为 shi3.sh,运行该文件:

Shi1.sh She He It
显示:
She
He
It

牵扯到通配符

http://blog.csdn.net/qzwujiaying/article/details/6371246

在手工处理方式中,首先要知道几个变量,还是以上面的命令行为例:
* $0 : ./test.sh,即命令本身,相当于C/C++中的argv[0]
* $1 : -f,第一个参数.
* $2 : config.conf
* $3, $4 ... :类推。
* $# 参数的个数,不包括命令本身,上例中$#为4.
* $@ :参数本身的列表,也不包括命令本身
* $* :和$@相同,但"$*" 和 "$@"(加引号)并不同,"$*"将所有的参数解释成一个字符串,而"$@"是一个参数数组。

#! bin/bash

for arg in "$@"
do
    echo $arg
done

 5 运行文件时,指定备份当前目录下的目录或文件。例如:当前目录下有目录 test_dir

和文件 file1.h ,执行备份功能的脚本文件 beifeng.sh
./ beifeng.sh test_dir file1.h
显示:
Backup Process Begins
2015-05-20-16-35-55 SUCCESS in backup file/directory(test_dir)
2015-05-20-16-35-55 SUCCESS in backup file/directory(file1.h)
Backup Process Ends同时将显示的信息保存到日志文件里,日志文件的名称由备份时的时间确定,如
2015-05-20-16-35-55.log
日志文件和备份文件存放在定义好的备份目录里。

#! /bin/sh

#get time
YY=`date +%Y`
MO=`date +%m`
DD=`date +%d`
HH=`date +%H`
MU=`date +%M`
SS=`date +%S`
TIME=$YY-$MO-$DD-$HH-$MU-$SS
#tar 
for arg in "$*"
do
    ORINGIN=${arg}            
done
echo $ORINGIN
tar -zcvf $TIME.tar.gz $ORINGIN
#log
echo "Backup Process Begins"
for arg in "$@"
do
    echo "$TIME SUCCESS in backup file/directory($arg)"
    echo "$TIME SUCCESS in backup file/directory($arg)">>$TIME.log
        
done
mv $TIME.tar.gz ./bak
mv $TIME.log ./log

 

 posted on 2015-05-23 21:56  瀚海  阅读(1025)  评论(0编辑  收藏  举报