Shell练习

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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。