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

版本:

#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 [ ... ]; then
else
fi

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


3、if else if 判断

if [ ... ]; then
elif [ ... ]; then
else
fi

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




二、for 循环

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

Example:

for var in A B C; do
echo "$var"
done




三、while 循环

while [ ... ]; do
done

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


Example:
x=0

while [ "$x" -lt 10 ]; do
echo "$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 "还有这种水果?"
    esac
done



五、函数

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

1、没有参数

Example:
hello()
{
    echo "Hello World."
}

echo "call function hello..."
hello
echo "function end..."



2、有参数

Example:
hello()
{
    echo "Hello World $1"
}

echo "call function hello..."
hello "!!!!!"
echo "function end..."


六、一些注意点

由于太多单拎出来。
以下是地址
!这里是地址!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值