作者 主題: 請教script拿裡錯了  (閱讀 2323 次)

0 會員 與 1 訪客 正在閱讀本文。

jjchiou

  • 懷疑的國中生
  • **
  • 文章數: 68
    • 檢視個人資料
請教script拿裡錯了
« 於: 2014-12-18 12:02 »
最近在寫一些簡單的shell script 抄鳥哥的script範例做的如下
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

echo "This program will print your selection !"
# read -p "
[root@Cen5 .bin]# cat testservice.sh
#!/bin/bash
# Program:
#    Using netstat and grep to detect WWW,SSH,FTP and Mail services.
# History:
# 2005/08/28   VBird   First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 1. echo "Now, I will detect your Linux server's services!"
echo -e "The www, ftp, ssh, and mail will be detect! \n"

test80=$(netstat -tuln | grep ":80")
test22=$(netstat -tuln | grep ':22')
test21=$(netstat -tuln | grep ':21')
test25=$(netstat -tuln | grep ':25')

if [ $test80 != "" ]; then
   echo " www service is on "
elif [ $test22 != "" ] ; then
  echo " ssh service is on "
elif [ $test21 != "" ] ; then
   echo " ftp service is on "
elif [ $test25 != "" ] ; then
   echo " mail service is on "
else
  echo " no www ssh ftp mail service on"
fi

出現/testservice.sh: line 17: [: too many arguments
./testservice.sh: line 19: [: too many arguments
./testservice.sh: line 21: [: !=: unary operator expected
./testservice.sh: line 23: [: !=: unary operator expected
等錯誤請問這是那裡錯了

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
Re: 請教script拿裡錯了
« 回覆 #1 於: 2014-12-18 12:33 »
在 [  ] 中的變數請加雙引號,如:
if [ "$test80" != "" ]; then
or:
if [ "$test80" ]; then

jjchiou

  • 懷疑的國中生
  • **
  • 文章數: 68
    • 檢視個人資料
Re: 請教script拿裡錯了
« 回覆 #2 於: 2014-12-19 12:31 »
Very thank netman reply my issue :

Now I have another script :
[root@218 agi-bin]# cat iptone.sh
#!/bin/bash
# Program:
#       Using username to assign callerID num.
# History:
# 2014/12/18    JJChiou   First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

NAME="$1"
set NUM=07010994416

echo " my name is $NAME " > /tmp/var.txt

if [ "$NAME" = "ceo.*" ]; then
   $NUM=07010994411
elif [ "$NAME" = "boss.*" ] ; then
  $NUM=07010994412
elif [ "$NAME" = "dayze.*" ] ; then
  $NUM=07010994415
elif [ "$NAME" = "kevin.*" ] ; then
  $NUM=07010994418
elif [ "$NAME" = "ava.*" ] ; then
  $NUM=07010994419
elif [ "$NAME" = "carol.*" ] ; then
  $NUM=07010994414
elif [ "$NAME" = "leo.*" ] ; then
  $NUM=07010994422
elif [ "$NAME" = "vincent.*" ] ; then
  $NUM=07010994421
elif [ "$NAME" = "gary.*" ] ; then
  $NUM=07010994417
elif [ "$NAME" = "kt.*" ] ; then
  $NUM=07010994413
elif [ "$NAME" = "service.*" ] ; then
  $NUM=07010994416
else
  $NUM=07010994410
fi

echo " my number is $NUM " >> /tmp/var.txt


When I cat /tmp/var.txt
"
 my name is $CallerIDName
 my number is 

It's seen the variance NUM is not work , who can tell me how to modify it ?
thank you

baldur

  • 懷疑的國中生
  • **
  • 文章數: 55
    • 檢視個人資料
Re: 請教script拿裡錯了
« 回覆 #3 於: 2015-01-13 16:27 »
NUM =  值
不需要 $ , 也不需要 set
用 case 會簡單點