2014-09-25 5 views
0

우리 회사의 서버 배치 프로세스를 자동화하기위한 bash 스크립트를 작성하고 있습니다. 이 예제에서는 특정 경로에서 특정 디렉토리를 사용할 수 있는지 확인하기 위해 dir_check라는 함수를 만들었습니다. 디렉토리가 사용 가능하지 않으면 함수는 경로에서 쓰기 권한을 점검하고 쓰기 권한이 사용 불가능한 경우 스니 j이 실행됩니다.chmod 실행 후 Bash 스크립트 실행이 갑자기 멈 춥니 다.

 echo "Changing the permission to writable" 
     exec sudo chmod 775 $1 -R 
     exec sudo chown $USER:$USER -R 

그러나 chmod 실행 후 스크립트가 종료됩니다. 권한을 성공적으로 변경하지만 오류나 힌트없이 실행이 중지됩니다. 누구든지이 문제에 대한 답을 나에게 줄 수 있습니까? 미리 감사드립니다. 전체 코드는

#!/bin/bash -x 

# Author: Sithum Nissanka 
# Date: 25/09/2014 
# Version: 1.0 
# Script performs necessary steps to setup a linux environment for ######. 

echo -e "Which environment are you setting up?" 

function setup_server { 
    echo "Path to the server archive:" 
    read archive_path 
     if [ $(($1)) -eq 2 ]; then 
     echo "Uploading file to the user home in the remote server..." 
     exec scp $archive_path [email protected]$2: 
    else 
     echo "Path to extract:" 
     read extraction_path 
     echo "Directory to extract:" 
     read extraction_dir 
     dir_check $extraction_path $extraction_dir 
    fi 
} 

function dir_check { 
    echo "Checking for the directory" 
    if [ ! -d "$2" ]; then 
     echo "Checking permission for creating directory" 
     if [ ! -w "$1" ]; then 
      echo "Changing the permission to writable" 
      exec sudo chmod 775 $1 -R 
#   exec sudo chown $USER:$USER -R 
     fi 
     echo "Creating the directory $2" 
     exec mkdir -p "$1/$2" 
     exec ls -al $1 
    fi 
} 

    echo -e "[1] local \n[2] remote" 
    echo -n "Select your environment: " 
    read env 

    if [ $((env)) -eq 2 ]; then 
     echo -n "Host:" 
     read host 
     echo -n "User:" 
     read user 
     echo "Setting up Mule standalone server..." 
     setup_server $env $host $user 
    elif [ $((env)) -eq 1 ]; then 
     setup_server $env 
    else 
     echo "Invalid environment selection" 
    fi 

아래에 주어진 다음은 거의 확실 exec을 오용하는 실행

+ echo -e 'Which environment are you setting up?' 
Which environment are you setting up? 
+ echo -e '[1] local \n[2] remote' 
[1] local 
[2] remote 
+ echo -n 'Select your environment: ' 
Select your environment: + read env 
1 
+ '[' 1 -eq 2 ']' 
+ '[' 1 -eq 1 ']' 
+ setup_server 1 
+ echo 'Path to the server archive:' 
Path to the server archive: 
+ read archive_path 
/data/Downloads/Software/mule-standalone-3.4.0.tar.gz 
+ '[' 1 -eq 2 ']' 
+ echo 'Path to extract:' 
Path to extract: 
+ read extraction_path 
/test 
+ echo 'Directory to extract:' 
Directory to extract: 
+ read extraction_dir 
server 
+ dir_check /test server 
+ echo 'Checking for the directory' 
Checking for the directory 
+ '[' '!' -d server ']' 
+ echo 'Checking permission for creating directory' 
Checking permission for creating directory 
+ '[' '!' -w /test ']' 
+ echo 'Changing the permission to writable' 
Changing the permission to writable 
+ exec sudo chmod 775 /test -R 
+1

exec는 현재 쉘을 대체합니다. 단순히 exec를 삭제하십시오. – jm666

+1

고맙습니다 jm666. 지금은 잘 작동합니다. 큰 도움 – sithum

답변

2

의 출력이다. 현재 프로세스를 새 프로세스로 바꿉니다. 첫 번째 프로세스에서는 chmod이 종료됩니다. 스크립트에서 모든 'exec'호출을 제거하고 스크립트없이 명령을 실행하십시오.

exec sudo chmod 775 $1 -R 

단지

sudo chmod 775 $1 -R 

등등.

관련 문제