2014-06-11 5 views
2

을 감안할 때 두 개의 스크립트앱 번들에 패키지 된 셸 스크립트를 어떻게 참조합니까?

foo.sh

/내용에서 복사
bar.sh/.APP 번들에서 자원
foo.sh

#!/bin/bash
. ./bar.sh
$ 1

스크립트가 bar.sh 소스를 시도 라인에

해당 파일이나 디렉토리

의 오류를받을 수 있나요 에코

상대적으로 참조 할 방법이 있습니까 bar.sh?

.app에 bash 스크립트 세트를 번들로 제공하는 다른 방법이 있습니까?

답변

2

당신이 할 수있는 것은 :

  1. 실제 실행중인 스크립트가 (당신의 foo.sh) 저장되어있는 전체 경로 & 디렉토리를 얻을.

    $cat script2 
    #! /usr/bin/env bash 
    echo "Hello World, this is script2" 
    
    $cat script1 
    #! /usr/bin/env bash 
    echo "Hello World from script 1" 
    echo "Full script path: $BASH_SOURCE" 
    echo "extracted directory: $(dirname $BASH_SOURCE)" 
    echo "running script 2" 
    $(dirname $BASH_SOURCE)/script2 && echo "running script 2 successful" || echo "error running script 2" 
    echo "sourcing script 2" 
    source $(dirname $BASH_SOURCE)/script2 && echo "sourcing script 2 successful" || echo "error sourcing script 2" 
    

    시험 :

간단한 예를 https://stackoverflow.com/a/246128/3701456

  • 전화를 참조하거나 1에서 디렉토리에 (당신의 bar.sh) 두 번째 스크립트 소스 (또는 디렉토리를 기준으로)
    $ls /tmp/test 
    script1 script2 
    $pwd 
    /home/michael 
    $/tmp/test/script1 
    Hello World from script 1 
    Full script path: /tmp/test/script1 
    extracted directory: /tmp/test 
    running script 2 
    Hello World, this is script2 
    running script 2 successful 
    sourcing script 2 
    Hello World, this is script2 
    sourcing script 2 successful 
    

    자세한 내용은 위의 링크를 참조하십시오 ...