I'm writing a bash script to automate a server deployment process in our company. In this I have created a function called dir_check for checking if a certain directory is available in certain path. If the directory is not available function checks for the write permission in the path and if the write permission is not available following snippet is run.
echo "Changing the permission to writable"
exec sudo chmod 775 $1 -R
exec sudo chown $USER:$USER -R
But after the execution of chmod the script terminates. It successfully changes the permission but the execution is stopped without any error or hint. Can anyone provide me with an answer for this. Thanks in advance. The entire code is given below
#!/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 $3@$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
Following is the output for the execution
+ 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
You are almost certainly misusing exec
. It replaces the current process with a new one, which does the first chmod
exits. Remove all the 'exec' calls from your script and just run the commands without it.
ie
exec sudo chmod 775 $1 -R
to just
sudo chmod 775 $1 -R
and so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.