[Shell]輸入參數
擷取shell指令碼的輸入參數,並且判斷得到的參數。
#!/bin/bash#title: testPT.sh#atuhor: orangleliu#date: 2014-08-08#desc: 輸入2個參數,第二個位元字,如果正確就列印出來,錯誤提示出來#=====================#Function Check#=====================Check(){if [ $# -ne 2 ] #charge params numthen echo "You must input two params"return 1elseall="$@" #get all input valuesecd=`echo $all|awk '{print $2}'`expr 1 + $secd > /dev/null 2>&1if [ $? -eq 0 ]then echo "$all"elseecho "Sorry age is a number" return 1fifi}#===================#Function main()#==================Main(){Check $1 $2if [ $? -eq 1 ]then exitfi}Main $1 $2
執行結果
[orangle@localhost shell]$ bash testPT.sh
You must input two params
[orangle@localhost shell]$ bash testPT.sh orangle 13
orangle 13
[orangle@localhost shell]$ bash testPT.sh orangle lzz
Sorry age is a number
tips:
if兩邊一定要有空格,if中的符號使用
變數複製的時候,= 兩邊不要有空格
參考教程
本文出自 “orangleliu筆記本” 部落格,請務必保留此出處http://blog.csdn.net/orangleliu/article/details/38449559