您的位置:首页 > 运维架构 > Shell

Shell 学习杂记二

2016-12-29 08:51 260 查看
测试

test expression || [ expression ] 方括号与表达式之间有空格

判断

if [ expression ]; then
command

else
if [ expression ]; then
command
fi

fi

case "$VAR" IN

var1) command1 ;; #;;结束边界

var2) command2 ;;

*) command ;; #default

esac

FOR循环

for variable in (list)
list:

{1..5}  1 2 3 4 5

$() 替换 ``命令 `seq 1 100` $(seq 1 100) or $(seq 1 2 100) step 2

不代列表循环

for VARIABLE # VARIABLE in $@ 可读性更好

类C的循环

for ((expression1; expression2; expression3)) #可以expression1,expression4;

do
echo -n "$i" #不换行

done

无限循环

for ((;1;)) expression2永远为真

WHILE循环

while [[ expression ]]



cat file.txt | while read LINE 通过管道

done < file.txt 重定向,差别在于管道会比重定向多两个进程

无限循环

while true || while ((1)) || while :

UNTIL循环

until expression #条件不为真时

无限循环

until ((0)) || until false

SELECT循环

select MENU in (list)

do
command # 此处用break跳出 还可以嵌套 case/esac

done

function

function FUNCTION_NAME() { #function关键字可以省略

command 
..

}

导入

. file

source /path/myshell.sh
http://blog.csdn.net/bignoseliu/article/details/5806359
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: