標籤:linux linux bash crlf
bash 編程中遇到的問題:
首先,建議在linux 下編寫bash,如果你要用windows 編寫bash 的話,那麼,你要特別注意幾個問題:
第一:編碼問題,用utf-8 編碼,
第二:分行符號問題,windows是斷行符號換行(CRLF)結尾,linux 是換行(LF)結尾
第三:使用utf-8無bom。
這三點是在windows 下編寫bash 的注意事項。
至於文法,我這邊就不多做講了,我也是初次編寫bash,文法基本沒啥說的,有一年編程基礎的都能看懂。
http://pan.baidu.com/s/1o6C1rnW,
分享一個bash 片段吧。這是從maven 私服 nexus 中擷取的,其功能就是擷取絕對路徑
# Resolve the true real path without any sym links.
CHANGED=true
while [ "X$CHANGED" != "X" ]
do
# Change spaces to ":" so the tokens can be parsed.
SAFESCRIPT=`echo $SCRIPT | sed -e ‘s; ;:;g‘`
#echo "script path is:[$SCRIPT]"
#echo "SAFESCRIPT path is:[$SAFESCRIPT]"
# Get the real path to this script, resolving any symbolic links
TOKENS=`echo $SAFESCRIPT | sed -e ‘s;/; ;g‘`
#echo "TOKENS path is:[$TOKENS]"
REALPATH=
for C in $TOKENS; do
# Change any ":" in the token back to a space.
C=`echo $C | sed -e ‘s;:; ;g‘`
REALPATH="$REALPATH/$C"
# If REALPATH is a sym link, resolve it. Loop for nested links.
while [ -h "$REALPATH" ] ; do
LS="`ls -ld "$REALPATH"`"
LINK="`expr "$LS" : ‘.*-> \(.*\)$‘`"
if expr "$LINK" : ‘/.*‘ > /dev/null; then
# LINK is absolute.
REALPATH="$LINK"
else
# LINK is relative.
REALPATH="`dirname "$REALPATH"`""/$LINK"
fi
done
done
if [ "$REALPATH" = "$SCRIPT" ]
then
CHANGED=""
else
SCRIPT="$REALPATH"
fi
done
# Change the current directory to the location of the script
cd "`dirname "$REALPATH"`"
REALDIR=`pwd`
bash 基礎文法_下載連結:http://yun.baidu.com/share/link?shareid=2510088394&uk=4043945255
bash 的幾個樣本:
1、自動打包。http://pan.baidu.com/s/1bniv5W7
2、linux 服務運行 java shell。http://yun.baidu.com/share/link?shareid=3338177448&uk=4043945255
3、linux 運行java 命令,包含環境變數的配置,jvm 參數的設定。http://yun.baidu.com/share/link?shareid=3334612191&uk=4043945255
&& 再來個windows 運行java 命令,跟3一樣的功能http://yun.baidu.com/share/link?shareid=3335077510&uk=4043945255
linux bash 編程