shell 編程手記
In terminal
=========================================================================
if [##這個地方要有空格不然會被識別成if後的其他命令## ] ;##注意分號##
then echo "do someting";
fi;
=========================================================================
如果是單獨的命令,首行和末行一定要加分號,
In *.sh
每行的分號可以省略
條件命令註記
其實和彙編很像
=========================================================================
[ -n "$ident" ]# (string length) not zero
[ -z "$ident" ]# (string length is) zero
[ -e "$str" ]#"file_str" is exist
[ -d "$str" ]#"str" is dir
[ -f "$str" ]#"str" is file
[ -L "$str" ]#"str" is Link,指軟連接,即file A-> file B
[ -r "$str" ]#"$str" is can be read
[ -w "$str" ]#"$str" is can be writed
[ -x "$str" ]#"$str" is can be excuted
[ num1 -eq num2 ]#equate
[ num1 -eq num2 ]#not equate
[ num1 -lt num2 ]#less than
[ num1 -le num2 ]#less or equate
[ num1 -gt num2 ]#greater than
[ num1 -ge num2 ]#greater or equat
=========================================================================
shift
#腳本文件中單獨的一個,可以去掉第一個輸入參數
IFS=:
=========================================================================
#IFS=:表示分割符是: #Internal Field Separator
#!/bin/bash
OLDIFS="$IFS"
data="1,2,3,4" #我們可以使用IFS讀取變量中的每一個條目
IFS=","
for item in $data
do
echo Item: "$item"
done
IFS="$OLDIFS"
=========================================================================