[shell][科普]Shell命令用法

来源:互联网 发布:linux给用户root权限 编辑:程序博客网 时间:2024/06/05 23:39

版本:

#Ver 1.0 【2013/08/11 11:59:20】


一、if

 1、if判断

    if [ ... ]; then    fi

Example:
if [ -f "$file" ]; then #如果file是个文件echo "${file}是一个文件。"fi #if 结束


2、if else判断

if [ ... ]; thenelsefi

Example:
if [ -f "$file" ]; then #如果file是个文件echo "${file}是一个文件。"elseecho "${file}不是一个文件。"fi #if 结束


3、if else if 判断

if [ ... ]; thenelif [ ... ]; thenelsefi

Example:
if [ -f "$file" ]; then #如果file是个文件echo "${file}是一个文件。"elif [ -d "$file" ]; then #如果file是个文件夹echo "${file}是一个文件夹。"elseecho "我也不知到${file}是啥!。"fi #if 结束




二、for 循环

for var in ....; do   ....done

Example:

for var in A B C; doecho "$var"done




三、while 循环

while [ ... ]; dodone

其中
-lt less than        小于-gt great than    大于-ge great equal  大于等于-le less equal     小于等于-eq equal           等于


Example:
x=0while [ "$x" -lt 10 ]; doecho "$x"x=`expr $x + 1` #注意:` (1的左面那个键)和 ' (Enter的左面那个键)是有区别的done




四、Case

case ... in   ...) do something here    ;;esac

Example:
echo "你喜欢哪个水果"for var in "香蕉" "桔子" "苹果" "大丫梨"; do    case $var in    "香蕉")        echo "靠,竟然喜欢$var";;    "桔子")        echo "$var有皮...";;    "苹果")        echo "$var很好吃";;    "大丫梨")        echo "好$var";;    *)        echo "还有这种水果?"    esacdone



五、函数

func(){}
或者如果你闲的无聊,可以这样写
function func(){}

1、没有参数

Example:
hello(){    echo "Hello World."}echo "call function hello..."helloecho "function end..."



2、有参数

Example:
hello(){    echo "Hello World $1"}echo "call function hello..."hello "!!!!!"echo "function end..."


六、一些注意点

由于太多单拎出来。
以下是地址
!这里是地址!
原创粉丝点击