Hi,
I have a below shell script with below functions written in it. My Shell script asks user's inputs to take user options. My script runs perfectly without using CHEF. I am facing issue in calling "if" statement of shell script.
Issue list:
I want to call shell script using CHEF. I am using "EOS" and "EOH" in CHEF to call shell script and to supply predefined user inputs. After calling shell script from CHEF it takse values correctly and also prints values(echo) from script but it is failing in "if" statement in the shell script.
For e.g. it takse values and prints correct values using echo from shell script
echo "you are in backup_files function"
echo "Cell path is:" $cellPath
echo "Cell name is:" $cellName
For e.g. at below point it is failing. What could be the reason if it takes values correctly from CHEF and prints values using echo then why it is not taking it from "if" statement.
if [[ -f $cellPath/test1.xml && -f $cellPath/test2.xml && -f $cellPath/test3xml && -f $cellPath/test4.xml ]];
then
After running chef-client below is the output.
Files backup program
1 - Enable backup
2 - Back-out
0 - exit program
Enter selection:
Enter Cell Name : -
You have entered name : test_appCell
Enter Cell Config Path : -
you have entered path : /test_repo/was-runtime/test_app/profiles/test_app/test_appDmgr/config/cells/test_appCell
Validating cell path <<<<<<
Directory /path/to/dir exists.
Validating now files and taking backup<<<<<<
you are in backup_files function
Cell path is: /test_repo/was-runtime/test_app/profiles/test_app/test_appDmgr/config/cells/test_appCell
Cell name is: test_appCell
one or more files misssing.......can not proceed
backup function failed......can not continue configuring LDAP security
- execute "bash" "/tmp/chef-script20161128-16340-1uswpmm"
Running handlers:
Running handlers complete
Chef Client failed. 1 resources updated in 3.307005186 seconds
CHEF RECIPE to call shell script
Cookbook Name:: backup
require 'socket'
hostname = Socket.gethostname
alias_name = 'default'
appname = 'test_app'
bash 'run_backup_script' do
user 'wasone'
cwd '/root/scripts/backup'
code <<-EOH
alias_n=hostname -a | awk '{print $2}'
app_path="/test_repo/was-runtime/$alias_n/profiles/*"
appname=echo $app_path | awk -F"/" '{print $NF}'
cellName=$appname'Cell'
cellPath=$app_path/$appname'Dmgr'/config/cells/$cellName
/root/scripts/backup/backup.sh <<EOS
1
$cellName
$cellPath
EOS
EOH
end
Shell script code: backup.sh
function backup_files ()
{
echo "you are in backup_files function"
echo "Cell path is:" $cellPath
echo "Cell name is:" $cellName
if [[ -f $cellPath/test1.xml && -f $cellPath/test2.xml && -f $cellPath/test3xml && -f $cellPath/test4.xml ]];
then
echo "All Files exists"
STATUS="SUCCESS"
else
echo "one or more files misssing.......can not proceed "
STATUS="FAIL"
fi
}
clear
selection=
until [ "$selection" = "0" ]; do
echo ""
echo "Files backup program"
echo "1 - Enable backup"
echo "2 - Back-out "
echo "0 - exit program"
echo ""
echo -n "Enter selection: "
read selection
echo ""
case $selection in
1 )
echo "Enter Cell Name : - ";
read cellName;
echo "You have entered name :" $cellName;
echo "Enter Cell Config Path : -";
read cellPath;
echo "you have entered path :" $cellPath;
echo " >>>>> Validating cell path <<<<<< "
if [ -d $cellPath ]
then
echo "Directory /path/to/dir exists."
echo " >>>>> Validating now files and taking backup<<<<<< "
backup_files $cellPath $cellName
if [ "$STATUS" == "SUCCESS" ];
then
$cellPath/../../../calling_python.py
else
echo "backup function failed......can not continue configuring LDAP security"
exit
fi
else
echo "Error: Cell Directory Path does not exists."
fi
press_enter ;;
2 )
echo "enter cell config path";
read cellPath;
echo "you have entered path :" $cellPath;
backout_security ;
if [ "$STATUS" == "SUCCESS" ];
then
echo "backout compelted....please validate the security now"
else
echo "backout function faied......can not continue"
exit
fi
press_enter ;;
0 ) exit ;;
* ) echo "Please enter 1, 2 or 0"; press_enter
esac
done