原創部分:
1、獲取返回值
#This is a shell to Deploy Project
#!/bin/bash
check_results=`ps -ef | grep "java"`//變量獲取語句執行結果
check_results=`cat a.sh`
echo "command(ps-ef) results are: $check_results"
2、獲取當前文件所在路徑
#This is a shell to Deploy Project
#!/bin/bash
#this files directory
basepath=$(cd `dirname $0`; pwd)
echo "current directory are: $basepath"
if [ "$basepath"x = "/home/zhangxu"x ]//特別注意空格規則,等號兩邊,中括號內都有一個空格
then
echo "yes"
else
echo "no"
fi
3、寫完了,等晚上試一下:
#This is a shell to Deploy Project
#!/bin/bash
#this files directory
basepath=$(cd `dirname $0`; pwd)
echo "current directory are: $basepath"
if [ "$basepath"x = "/usr/local/apache-tomcat-8.5.29"x ]
then
echo "begin>>>>>"
echo "stop java >>>>>"
killall -9 java
echo "remove webapp File >>>>>"
rm -rf webapps/zztabc
rm -rf webapps/zztabc.war
rm -rf webapps/zztManager
rm -rf webapps/zztManager.war
echo "add new webapp File >>>>>"
cp -rf /home/zhangxu/zztabc.war /usr/local/apache-tomcat-8.5.29/webapps/
cp -rf /home/zhangxu/zztManager.war /usr/local/apache-tomcat-8.5.29/webapps/
echo "startup >>>>>"
sh bin/startup.sh |tail -f logs/catalina.out
else
echo "please put this file to /usr/local/apache-tomcat-8.5.29"
fi
測試通過~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~啪啪啪~~
4、添加一些詳細的驗證,如文件是否存在,以及適當詢問繼續有否的判斷(v2.0)
5、直接使用ideamaven配置自動化部署(那我折騰個毛線!),不過這就是另一篇博客了~
==============================================================
轉載部分(參考過得資料,或多或少參考了其中的內容):
(貼過來的目的是供以后自己參考,侵刪)
下面是參考的一些相關資料,以后會整理一些用的頻繁的以及易出問題的地方:
https://blog.csdn.net/mdx20072419/article/details/9381339:
shell中函數的調用方式有兩種,如下圖:
shell中函數調用的第一種方式,是將標准輸出傳遞給主程序的變量,而不是返回值!
#!/bin/sh
check_user()
{
n=`cat /etc/passwd | cut -d ":" -f 1 | grep "^$1$" -Rn | cut -d ":" -f 1`
echo $n
#這里是使用echo語句,將結果輸出到標准輸出上,所以在主程序中可以使用變量接收
}
userinfo()
{
userinfo=`head -$1 /etc/passwd | tail -1 | cut -d ":" -f 3,4`
echo $userinfo
}
while true
do
read username
m=`check_user $username`
#使用變量接收函數check_user傳遞的值
if [ -n "$m" ]
then
userinfo $m //調用userinfo並且將m傳入
exit
else
echo "$username is not exit!"
fi
done
而函數的第二種調用方式,是使用$?來接收上一程序的返回值狀態,也就是return返回的值。
下面程序中,if判斷后,return 0 或者 1,在這里,我們就可以使用$?接收return的值,然后
存儲下來,繼而進行下一步的判斷!
check_user()
{
n=`cat /etc/passwd | cut -d ":" -f 1 | grep -n "^$1$"| cut -d ":" -f 1`
if [ -z "$n" ]
then
return 0
else
return 1
fi
}
show_userinfo()
{
userinfo=`head -$n /etc/passwd | tail -1 | cut -d ":" -f 1,3,4`
echo $userinfo
}
echo "input username : "
read username
check_user $username
num=$?
if [ $num -eq 0 ]
then
echo "The user '$username' is not exist."
exit
else
show_userinfo $n
fi
上面兩個程序的執行結果是一樣的,但是要注意着兩種函數的不同調用方式,以及其返回給
主調程序的到底是什么。明白了這一點,才能准確知道到底使用何種方式接收返回值!
https://blog.csdn.net/ithomer/article/details/7954577
#!/bin/bash -
function mytest()
{
echo "arg1 = $1"
if [ $1 = "1" ] ;then
return 1
else
return 0
fi
}
echo
echo "mytest 1"
mytest 1
echo $? # print return result
echo
echo "mytest 0"
mytest 0
echo $? # print return result
echo
echo "mytest 2"
mytest 2
echo $? # print return result
echo
echo "mytest 1 = "`mytest 1`
if mytest 1 ; then
echo "mytest 1"
fi
echo
echo "mytest 0 = "`mytest 0`
if mytest 0 ; then
echo "mytest 0"
fi
echo
echo "if fasle" # if 0 is error
if false; then
echo "mytest 0"
fi
echo
mytest 1
res=`echo $?` # get return result
if [ $res = "1" ]; then
echo "mytest 1"
fi
echo
mytest 0
res=`echo $?` # get return result
if [ $res = "0" ]; then
echo "mytest 0"
fi
echo
echo "end"
http://www.jb51.net/article/33971.htm
比較兩個字符串是否相等的辦法是:
if [ "$test"x = "test"x ]; then
這里的關鍵有幾點:
1 使用單個等號
2 注意到等號兩邊各有一個空格:這是unix shell的要求
3 注意到"$test"x最后的x,這是特意安排的,因為當$test為空的時候,上面的表達式就變成了x = testx,顯然是不相等的。而如果沒有這個x,表達式就會報錯:[: =: unary operator expected
二元比較操作符,比較變量或者比較數字.注意數字與字符串的區別.
整數比較
-eq 等於,如:if [ "$a" -eq "$b" ]
-ne 不等於,如:if [ "$a" -ne "$b" ]
-gt 大於,如:if [ "$a" -gt "$b" ]
-ge 大於等於,如:if [ "$a" -ge "$b" ]
-lt 小於,如:if [ "$a" -lt "$b" ]
-le 小於等於,如:if [ "$a" -le "$b" ]
大於(需要雙括號),如:(("$a" > "$b"))
>= 大於等於(需要雙括號),如:(("$a" >= "$b"))
小數據比較可使用AWK
字符串比較
= 等於,如:if [ "$a" = "$b" ]
== 等於,如:if [ "$a" == "$b" ],與=等價
注意:==的功能在[[]]和[]中的行為是不同的,如下:
1 [[ $a == z* ]] # 如果$a以"z"開頭(模式匹配)那么將為true
2 [[ $a == "z*" ]] # 如果$a等於z*(字符匹配),那么結果為true
3
4 [ $a == z* ] # File globbing 和word splitting將會發生
5 [ "$a" == "z*" ] # 如果$a等於z*(字符匹配),那么結果為true
一點解釋,關於File globbing是一種關於文件的速記法,比如"*.c"就是,再如~也是.
但是file globbing並不是嚴格的正則表達式,雖然絕大多數情況下結構比較像.
!= 不等於,如:if [ "$a" != "$b" ]
這個操作符將在[[]]結構中使用模式匹配.
大於,在ASCII字母順序下.如:
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
注意:在[]結構中">"需要被轉義.
具體參考Example 26-11來查看這個操作符應用的例子.
-z 字符串為"null".就是長度為0.
-n 字符串不為"null"
注意:
使用-n在[]結構中測試必須要用""把變量引起來.使用一個未被""的字符串來使用! -z
或者就是未用""引用的字符串本身,放到[]結構中。雖然一般情況下可
以工作,但這是不安全的.習慣於使用""來測試字符串是一種好習慣.