Bash 스크립트에서 함수를 읽을 수 없습니다.

Bash 스크립트에서 함수를 읽을 수 없습니다.

다음과 같이 두 개의 파일이 있습니다

범용 함수.sh

#!/bin/bash
#
# Install .net core
#
function InstallDotNetCore
{
  echo "installeddotnetcore"
}
#
function InstallMdsHooks
{
 echo "installedmdshooks"
}
#
function InstallNodeAgent
{
echo "installnodeagent"
}

메인 디렉토리

#!/bin/bash

echo "BootStrap Started"

source /tmp/genericfunctions.sh

sudo apt-get update

InstallDotNetCore
InstallMdsHooks


echo "Bootstrap complete"

실수:

xxxxxxx@xxxxxxtest2vm:/tmp$ sh main.sh
:All Args:
main.sh: 5: main.sh: source: not found
Hit:1 https://packages.microsoft.com/repos/microsoft-ubuntu-zesty-prod zesty InRelease
Hit:2 https://apt-mo.trafficmanager.net/repos/azurecore trusty InRelease
Hit:3 http://security.ubuntu.com/ubuntu artful-security InRelease
Hit:4 http://azure.archive.ubuntu.com/ubuntu artful InRelease
Hit:5 https://download.docker.com/linux/ubuntu artful InRelease
Get:6 http://azure.archive.ubuntu.com/ubuntu artful-updates InRelease [78.6 kB]
Hit:7 http://azure.archive.ubuntu.com/ubuntu artful-backports InRelease
Fetched 78.6 kB in 0s (111 kB/s)
Reading package lists... Done
main.sh: 9: main.sh: InstallDotNetCore: not found
main.sh: 10: main.sh: InstallMdsHooks: not found

: 부팅 완료:

file1의 어떤 함수도 file2에서 호출되지 않습니다. 나는 단순히 ./tmp/file1.sh를 제공하려고 시도했습니다. file1의 모든 기능을 트리거합니다. 특정 기능만 실행하고 싶습니다.

어떤 도움이라도 대단히 감사하겠습니다.

답변1

두 스크립트 파일 모두 스크립트이므로 쉘 인터프리터 대신 쉘 인터프리터를 bash사용하여 실행해야 합니다.bashsh

#!스크립트에는 올바른 -lines가 있으므로 ./main.sh스크립트가 실행 가능하다면 명령줄()에서 직접 실행하는 것이 올바른 작업을 수행해야 합니다.


sh스크립트 에서 함수는 다음을 사용하여 정의됩니다.

somefuctionname () {
    somefunctionbody
}

키워드를 사용하는 대신 (점) function도 사용하는 경우 스크립트는.source회의sh다른 특수 기능을 사용하지 않는 한 이미 bash. 에서 실행할 수 있습니다 .

관련 정보