歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Shell混用引發的任務無法啟動

Shell混用引發的任務無法啟動

日期:2017/3/1 10:35:36   编辑:SHELL編程
在使用debian 腳步啟動一個sh時一直報錯,由此引發此文的形成:

在使用bash shell時,我們有這樣的場景:

env.sh -- 定義公共的變量

run1.sh -- 定義運行代碼

run2.sh -- 定義運行代碼


如果env.sh中的內容是根據具體的run*.sh作變化,那麼就需要給env.sh中傳入變量。

很多人可能會采用這種方式,


場景模擬:

env.sh:

[plain]
  1. #!/bin/sh
  2. echo $1

run1.sh:

[plain]
  1. #!/bin/sh
  2. CURRENT_PATH='core'
  3. echo ${CURRENT_PATH}
  4. . /home/admin/tmp/madding.lip/env.sh $CURRENT_PATH
  5. echo "invoke $CURRENT_PATH-service $1"
  6. if [ "$1" = "stop" ] ; then
  7. echo "`date`:stop --------------------------------"
  8. echo "remove $shutdownFile"
  9. elif [ "$1" = "debug" ] ; then
  10. echo "`date`:debug --------------------------------"
  11. fi

執行命令:

sh run.sh start

結果:

在RedHat 5.3中:

core
core
invoke core-service

debian 6:

core

start

invoke core-service



分析:


表面現象問題出在:

[plain]
  1. . /home/admin/tmp/madding.lip/env.sh $CURRENT_PATH

根本:

debian默認使用的:

[plain]
  1. [email protected]:~$ which sh
  2. /bin/sh
  3. [email protected]:~$ ls -l /bin/sh
  4. lrwxrwxrwx 1 root root 4 Jun 6 05:49 /bin/sh -> dash
redhat默認sh使用:

[plain]
  1. [[email protected] ~]$ which sh
  2. /bin/sh
  3. [[email protected] ~]$ ls -l /bin/sh
  4. lrwxrwxrwx 1 root root 4 Nov 16 2010 /bin/sh -> bash

處理意見:

1.shell盡量用各個都兼容的方式書寫,這樣可移植性比較強

2.如果明確使用的shell,盡量在頭部表明,這樣執行./方式執行時能識別具體的shell

3.如果用具體的shell加腳步方式執行的話,建議明確shell腳步的解析器,如dash xx.sh 或者 bash xx.sh

Copyright © Linux教程網 All Rights Reserved