Reputation: 19
I am very new to shell scripting and i need to write a script, the conditions are pretty straight fwd:
i have 2 dir location
First Dir: $ABC_123/cert
which contains 3 files:
file1.pem file2.pem file3.pem
Second Dir: $ABC_123/private
which contains 2 files:
file4.pem file5.pem
these files needs to be replaced/appended with files from a user input dir location depending on below conditions
i want to write a shell function for back up and replace/append conditions.
Below is the shell i wrote:
#!/bin/sh
$ABC_123=/ABC/123/XCOM
export ABC_123
# input the change number
printf 'Enter the NUMBER number\n'
read NUMBER
file1="$ABC_123/certs/fil2cert.pem"
file2="$ABC_123/certs/fil2cert.pem"
file3="$ABC_123/certs/fil3cert.pem"
file4="$ABC_123/private/fil4key.pem"
file5="$ABC_123/private/fil5key.pem"
file6="$ABC_123/private/fil6key.pem"
file7="/tmp/$NUMBER/fil3cert.pem"
# Begining of the implementation
if [ -f $file1 ] && [ -f $file2 ] && [ -f $file3 ] && [ -f $file4 ] && [ -f $file5 ] && [ ! -f $file6 ] && [ -f $file7 ]
then
#
#BACKUP
#
cp -p $file1 $file1.$(date +%Y-%m-%d).bkp
cp -p $file2 $file2.$(date +%Y-%m-%d).bkp
cp -p $file3 $file3.$(date +%Y-%m-%d).bkp
cp -p $file4 $file4.$(date +%Y-%m-%d).bkp
cp -p $file5 $file5.$(date +%Y-%m-%d).bkp
#
#REPLACE
#
cp /tmp/$NUMBER/*key.pem $ABC_123/private/
cp /tmp/$NUMBER/*cert.pem $ABC_123/certs/
cat /tmp/$NUMBER/fil3cert.pem >> $ABC_123/certs/fil3cert.pem
#
#CHANGE PERMISSIONS
#
chmod 644 $ABC_123/private/*key.pem
chmod 644 $ABC_123/certs/*cert.pem
chmod 644 $ABC_123/certs/fil3cert.pem
chown root:root $ABC_123/private/*key.pem
chown root:root $ABC_123/certs/*cert.pem
chown root:root $ABC_123/certs/fil3cert.pem
elif [ -f $file1 ] && [ -f $file2 ] && [ -f $file3 ] && [ -f $file4 ] && [ -f $file5 ] && [ ! -f $file6 ] && [ ! -f $file7 ]
then
#
#BACKUP
#
cp -p $file1 $file1.$(date +%Y-%m-%d).bkp
cp -p $file2 $file2.$(date +%Y-%m-%d).bkp
cp -p $file3 $file3.$(date +%Y-%m-%d).bkp
cp -p $file4 $file4.$(date +%Y-%m-%d).bkp
#
#REPLACE
#
cp /tmp/$NUMBER/*key.pem $ABC_123/private/
cp /tmp/$NUMBER/*cert.pem $ABC_123/certs/
#
#CHANGE PERMISSIONS
#
chmod 644 $ABC_123/private/*key.pem
chmod 644 $ABC_123/certs/*cert.pem
chown root:root $ABC_123/private/*key.pem
chown root:root $ABC_123/certs/*cert.pem
elif [ ! -f $file1 ] && [ ! -f $file2 ] && [ ! -f $file3 ] && [ ! -f $file4 ] && [ ! -f $file5 ] && [ ! -f $file6 ]
then
#
#REPLACE
#
cp /tmp/$NUMBER/*key.pem $ABC_123/private/
cp /tmp/$NUMBER/*cert.pem $ABC_123/certs/
cp /tmp/$NUMBER/fil3cert.pem $ABC_123/certs/
#
#CHANGE PERMISSIONS
#
chmod 644 $ABC_123/private/*key.pem
chmod 644 $ABC_123/certs/*cert.pem
chmod 644 $ABC_123/certs/fil3cert.pem
chown root:root $ABC_123/private/*key.pem
chown root:root $ABC_123/certs/*cert.pem
chown root:root $ABC_123/certs/fil3cert.pem
elif [ -f $file6 ]
then
#
# BACKUP
#
find $ABC_123/certs/* -type f -exec mv {} {}.$(date +%Y-%m-%d).bkp \;
find $ABC_123/private/* -type f -exec mv {} {}.$(date +%Y-%m-%d).bkp \;
#
# REPLACE
#
cp /tmp/$NUMBER/*key.pem $ABC_123/private/
cp /tmp/$NUMBER/*cert.pem $ABC_123/certs/
cp /tmp/$NUMBER/fil3cert.pem $ABC_123/certs/
#
# CHANGE PERMISSIONS
#
chmod 644 $ABC_123/private/*key.pem
chmod 644 $ABC_123/certs/*cert.pem
chmod 644 $ABC_123/certs/fil3cert.pem
chown root:root $ABC_123/private/*key.pem
chown root:root $ABC_123/certs/*cert.pem
chown root:root $ABC_123/certs/fil3cert.pem
elif [ ! -f "$file1" ] || [ ! -f "$file2" ] || [ ! -f "$file3" ] || [ ! -f "$file4" ] || [ ! -f "$file5" ] && [ ! -f "$file6" ] ; then
# At least one file is missing, writing the list of missing file(s):
echo -n "Missing file(s): "
for i in "$file1" "$file2" "$file3" "$file4" "$file5" ; do
if [ ! -f "$i" ] ; then
echo -n "$i "
fi
done
echo
fi
i want to use shell function instead of repeating the BACKUP,REPLACEMENT and CHANGE PERMISSION command again and again
Upvotes: 0
Views: 39
Reputation: 22237
Your shell function could be something like
all_present() {
for file
do
[ -f $file ] || return 1
done
return 0
}
and it would be used like this:
if ! all_present "$file1" "$file2" "$file3" "$file4" "$file5" "$file6"
then
echo at least one is missing
...
BTW: In case you can switch from shell to bash or zsh or ksh, you can hold all files in an array and the whole expression would be simpler.
Upvotes: 0