了解了基本的语法结构进行一般的任务处理就很容易了,读别人的代码也容易的多。
从windows想linux转,学的小东西海真的不少,现在感觉可以告一段落了,相关的东西都知道是怎么回事了,以后碰到的时候再查查资料即可。
************************
注意空格
x=3,=号两边没有空格,有了就不对
************************
;表示语句结束
;;表示直接跳转出当前代码block,相当于break
************************
测试条件的形式如:
if [ -z "" ]; then
...
fi
[]是以空格开始和结尾的
其中-z表示测试类型,这里就是试图测试后面的字符串长度是否为0,
************************
其余测试类型如下:
1) file relatted test
-b file / exist and block file
-c file /exist and character file
-d file /exist and directory file
-e file /test exist only
-f file /exist and formal regular file(正常文件)
-g file /exist and SGID setting file
-h file /exist and symbol link file
-k file /exist and sticky setting file
-p file /exist and named pipe file
-r file /exist and can read (not readonly) file
-s file /exist and size > 0 file ( the file contains at least one bit)
-u file /exist and SUID setting file
-w file /exist and writable file
-x file /exist and executable file
-o file /exist and valid UID file
2) string test
-z "string" true if len() == 0
-n "string" true if len != 0
"string" = "string" (note, = but == )
"string" != "string"
the above = and != is case-sensitive
3) integer comparision, suppose type(a) = int, type(b) = int
a -eq b /true if a == b
a -ne b /true if a != b
a -lt b /trua if a < b
a -le b / true if a <= b
a -gt b /true if a > b
a -ge b /true if a >=b
************************
赋值的时候不需要$x,只有引用的时候才需要,所以
$x=`echo "$x+1" | bc`是错的,
x=`echo "$x+1" | bc`是对的,
假设x=3的话,则echo “$x+1" is
3+1
this expression was feed into bc (bc is calculator), we get 4
`x=4` was executed, because it was wrapped with ``, note ` is not ', ` key is in upper-left of keyboard
************************
各种循环条件
#while
x=0
while [ $x -lt 10 ]
do
echo $x
x=`echo "$x+1" | bc`
done
echo '-------------'
#case
x=2
case $x in
1) echo "1";;
2) echo "2";;
*) echo "none";;
esac
echo '-------------'
#until
until [ $x -eq 5 ]
do
echo $x
x=`echo "$x+1" | bc`
done
echo '-------------'
for i in $HOME/.bash*
do
echo $i
done
break 可以加整数表示退出的层数,continue语义同c/c++等
************************
变量代换
下面的所有语句都有两个方面的影响,一是表达式返回的值是什么,二是变量本身的值是否发生了改变
${para:-word} if para is null or 0, expression return word, otherwise, expression return $para, para keep unchanged
${para:=word} if para is null or 0, expression return word +and+ para was assigned with word, otherwise, expression return $para
${para:?word} if para is null or 0, expression return word and word output to stdout, otherwise, expression return $para, para keep unchanged
${para:+word} if para is null or 0, expression return null; otherwise, expression return word, and para was assigned as word
************************
子函数声明,这个普通的C/C++类似
setPath(){
PATH=${PATH:="/sbin:/bin"};
for _DIR in "$@"
do
if [ -d "$_DIR" ]; then
PATH="$PATH":"$_DIR";
fi
done
export PATH
unset _DIR
}
************************
shell中可以使用环境变量来方便的进行信息共享
************************
一些特殊的shell命令
: 什么都不做的命令,等于C中的;
ls |xargs -n 1 echo + xargs将后面的所有输入当做参数,最后再加上前面的命令的输出,-n 1表示一次只处理一条输出,一般用于参数太长
************************
调试
bin/sh -n 读所有命令但不执行
-v 读入并显示所有行
-x 执行命令时显示所有命令和他们的参数
阅读(1196) | 评论(6) | 转发(0) |