作者 主題: 請教script變數設定的問題  (閱讀 1545 次)

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

jjchiou

  • 懷疑的國中生
  • **
  • 文章數: 65
    • 檢視個人資料
請教script變數設定的問題
« 於: 2014-12-19 13:54 »
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 my name is service.104.mobile3
 my number is 

It's seen the variance NUM is not work , who can tell me how to modify it ?
thank you
修改訊息
« 上次編輯: 2014-12-22 12:06 由 jjchiou »

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17465
    • 檢視個人資料
    • http://www.study-area.org
Re: 請教script變數設定的問題
« 回覆 #1 於: 2014-12-22 12:53 »
你跑 bash -x  iptone.sh 看看是否有抓到值?

HaWay

  • 大隻佬!
  • 老人組
  • 俺是博士!
  • *****
  • 文章數: 3980
    • 檢視個人資料
Re: 請教script變數設定的問題
« 回覆 #2 於: 2014-12-22 17:16 »
set NUM :
 NUM=abc123

Get NUM:
 echo $NUM

ERROR:
 $NUM=07010994411

代碼: [選擇]
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
我做人那麼 nice, 肯定有什麼誤會.....

jjchiou

  • 懷疑的國中生
  • **
  • 文章數: 65
    • 檢視個人資料
Re: 請教script變數設定的問題
« 回覆 #3 於: 2014-12-22 17:39 »
Dear netman :

It's strange : the shell script is executed between a call dialplan , it just for demo callerid number .

when call out , execute  "cat /tmp/var.txt "

 my name is service.104.mobile3
 my number is 

then execute
 "bash -x  iptone.sh"

+ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin
+ export PATH
+ NAME=
+ set NUM=07010994416
+ echo ' my name is  '
+ '[' '' = 'ceo.*' ']'
+ '[' '' = 'boss.*' ']'
+ '[' '' = 'dayze.*' ']'
+ '[' '' = 'kevin.*' ']'
+ '[' '' = 'ava.*' ']'
+ '[' '' = 'carol.*' ']'
+ '[' '' = 'leo.*' ']'
+ '[' '' = 'vincent.*' ']'
+ '[' '' = 'gary.*' ']'
+ '[' '' = 'kt.*' ']'
+ '[' '' = 'service.*' ']'
+ =07010994410
iptone.sh: line 37: =07010994410: command not found
+ echo ' my number is  '
then I execute  "cat /tmp/var.txt "
 my name is 
 my number is 

why it is so strange ?

thanks !


jjchiou

  • 懷疑的國中生
  • **
  • 文章數: 65
    • 檢視個人資料
Re: 請教script變數設定的問題
« 回覆 #4 於: 2014-12-23 10:09 »
Thanks HaWay :

I think must something wrong in my srcipt :

it execute "cat /tmp/var.txt "
 my name is service.104.mobile3
 my number is abc123

not get my needed caller number :

the script now is :
#!/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:
NUM=abc123

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

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

HaWay

  • 大隻佬!
  • 老人組
  • 俺是博士!
  • *****
  • 文章數: 3980
    • 檢視個人資料
Re: 請教script變數設定的問題
« 回覆 #5 於: 2014-12-23 10:28 »
Set, Let NUM:
 NUM=07010994411


ERROR:
 $NUM=07010994411


#!/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:
NUM=abc123

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

Get NUM:
echo " my number is $NUM " >> /tmp/var.txt
我做人那麼 nice, 肯定有什麼誤會.....

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17465
    • 檢視個人資料
    • http://www.study-area.org
Re: 請教script變數設定的問題
« 回覆 #6 於: 2014-12-23 11:29 »
哈~~ 小問題:

定義變數的時候不能加$在前面啦~

$NUM=
全部改為: NUM=

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17465
    • 檢視個人資料
    • http://www.study-area.org
Re: 請教script變數設定的問題
« 回覆 #7 於: 2014-12-23 11:40 »
還有一個小bug:在例子的test中不會比對.*這樣的regx

或許換成case:
代碼: [選擇]
#!/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

case "$NAME" in
 ceo*)  NUM=07010994411 ;;
 boss*) NUM=07010994412 ;;
 dayze*) NUM=07010994415 ;;
 kevin*) NUM=07010994418 ;;
 ava*) NUM=07010994419 ;;
 carol*) NUM=07010994414 ;;
 leo*) NUM=07010994422 ;;
 vincent*) NUM=07010994421 ;;
 gary*) NUM=07010994417 ;;
 kt*) NUM=07010994413 ;;
 service*) NUM=07010994416 ;;
 *) NUM=07010994410 ;;
esac

echo " my number is $NUM " >> /tmp/var.txt
« 上次編輯: 2014-12-23 12:40 由 netman »

jjchiou

  • 懷疑的國中生
  • **
  • 文章數: 65
    • 檢視個人資料
Re: 請教script變數設定的問題
« 回覆 #8 於: 2014-12-23 17:30 »
謝謝兩位大大:
執行結果已經正確了
 cat /tmp/var.txt
 my name is service.104.mobile3
 my number is 07010994416

剩下怎麼把變數帶回原來dialplan環境  我就自己想辦法了

謝謝