码迷,mamicode.com
首页 > 系统相关 > 详细

Shell练习

时间:2015-05-23 22:31:43      阅读:838      评论:0      收藏:0      [点我收藏+]

标签:

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/sh
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

 

Shell练习

标签:

原文地址:http://www.cnblogs.com/xuhuaiqu/p/4524949.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!