Thursday, October 6, 2022

[SOLVED] Bash script to cancel a backup task process ID

Issue

I am getting error(canpr.sh: line 15: syntax error near unexpected token `fi') while executing below script to cancel a particular process ID of a backup job. Can someone please help to check this code and help me to identify the issue or can suggest a better way to perform this task.

#!/bin/bash
while true;
do
PROC=`dsmadmc -se=user -id=XXX -password=XXXXX -dataonly=yes "q proc" | grep "Backup Storage Pool" | awk '{print $1}'`
if ["${PROC}Test" == "Test"]
then
echo "Process list is empty. Exiting from program";
break;
else
  for pid in $PROC
do
  dsmadmc -se=user -id=XXX -password=XXXXX -dataonly=yes "cancel proc $pid"
sleep 30;
fi;
done
echo "Script execution completed"

Result"canpr.sh: line 15: syntax error near unexpected token `fi'


Solution

Indenting your code not only makes it easier to read, but also aids in easily identifying bugs such as not closing a loop properly as mentioned by Orion.

You also have a space issue after the opening if statement that needs to be addressed. Please be sure to use shellcheck as mentioned in the bash description as part of your troubleshooting process.

#!/bin/bash
while true; do
    PROC=$(dsmadmc -se=user -id=XXX -password=XXXXX -dataonly=yes "q proc" | grep "Backup Storage Pool" | awk '{print $1}')
    if [ "${PROC}Test" == "Test" ]; then
        echo "Process list is empty. Exiting from program"
        break
    else
        for pid in $PROC; do
            dsmadmc -se=user -id=XXX -password=XXXXX -dataonly=yes "cancel proc $pid"
            sleep 30
        done
    fi
done
echo "Script execution completed"



Answered By - HatLess
Answer Checked By - Pedro (WPSolving Volunteer)