Linux shell指令碼中執行命令結果賦值給變數&&echo輸出變數是否包含換行符的問題
Linux shell指令碼中執行命令結果賦值給變數&&echo輸出變數是否包含換行符的問題
echo $ret 和 echo "$ret" 區別:
如果是echo $ret,輸出結果為一行,沒有換行符
如果是echo "$ret",輸出結果為多行,有換行符
# vim test.sh
#!/bin/sh
CORE_DIR=`pwd`
UI_DIR=$CORE_DIR/../tomcat8
target=$UI_DIR/webapps/ui/META-INF/MANIFEST.MF
newline=$'\n'
newline2=$newline$newline
ret="$newline============ ui Timestamp ===========$newline2"
if [ -e $target ]; then
ret+="$(cat $target | sed -n '/Timestamp/p' )"
else
ret+="File or directory not found."
fi
ret+="$newline2"
ret+="$newline============ core Timestamp ===========$newline2"
now=`date -I`
target_core=$CORE_DIR/logs/$now\.0\.log
if [ -e $target_core ]; then
ret+="$(grep Timestamp $target_core | sed -n '/Timestamp/p' | sed s'/\ \ \ \ //' | sed s'/"//g')"
else
ret+="core log file or directory not found."
fi
ret+=$newline
#echo $ret
echo "$ret"